Web Technology Introduction: Server-Side Script: PHP

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Web Technology Introduction

Server-side script: PhP


Hieu Dinh Vo

Web Client - Server


1.
2. Execute

3.

Server-side Scripts
Scripts (programs) developed to run
at server side (result will be sent to
clients)
Examples: JSP, ASP, Python, Perl, PhP.
etc

PhP
PHP is an open-source technology
PHP is platform independent
PHP also supports many databases

First PhP Script


(example)

PhP Syntax
Whitespace insensitive
Statements ended by semicolon
Comments
/*.*/ for multiple lines
// or # for a line

PhP Syntax
Variables
All variables in PHP are denoted with a
leading dollar sign ($).
Variables can, but do not need, to be
declared before assignment
Variables used before they are assigned
have default values.

Types
Integers are whole numbers, without a decimal point, like
495.
Doubles are floating-point numbers, like 3.14159 or 49.0.
Booleans have only two possible values: TRUE and FALSE.
NULL is a special type that only has one value: NULL.
Strings are sequences of characters, like PHP 4.0 supports
string operations.
Arrays are named and indexed collections of other values.
Objects are instances of programmer-defined classes,
which can package up both other kinds of values and
functions that are specific to the class.
Resources are special variables that hold references to
resources external to PHP (such as database connections).

Output
echo
print

Control Structures
Similar to JavaScript
if, if else
for
while
switch case

10

Functions
Similar to JavaScript

11

String
$my_string=This is a string;
$another_string=This is another string;
$statement = everything I say;
$question_1 = Do you have to take statement so
literally?\n<BR>;
$question_2 = Do you have to take $statement so
literally?\n<BR>;
echo $question_1;
echo $question_2;
Do you have to take everything I say so literally?
Do you have to take $statement so literally?\n

12

String
$sport = volleyball;
$plan1 = I will play $sport in the
summertime;
$plan2 = I will play {$sport} in the
summertime;

13

String index
$str=abcde
$str[0] a
$str[3] d

14

Concatenation
$my_two_cents = I want to give you a piece of my
mind ;
$third_cent = And another thing;
print($my_two_cents . ... . $third_cent);
I want to give you a piece of my mind ... And another
thing

15

String Functions
$short_string = This string has 29
characters;
strlen($short_string)
strpos($short_string,9)
strrpos($short_string,a)
To compare: ==
strstr($short_string,has)
substr($short_string,5)
substr($short_string,5,6)
16

Forms

orms are means for convey data from users (via Web browse
o server

<body>
<form name="string" action="reverse.php"
method="GET">
Original string: <input type="text"
name="string"/>
<input type="submit" value="Reverse"/>
</form>
</body>

17

POST vs. GET


GET: query data sent as a part of URL
POST: data is hidden

18

Obtaining Data
<?php
$str=$_GET['string'];
$res="";
for($i=0;$i<strlen($str);$i++){
$res=$str[$i].$res;
}
echo $res;
?>

19

HTML & PhP


Two ways to use HTML in PhP
Use echo or print to produce HTML tags
Put HTML tags outside of <?php ?>
segment

20

21

You might also like