PHP Part 1

What is PHP?


PHP - development

    print("Hello world!"); and PRINT("Hello world!");  produce the same results

    $num1 = 1; and $NUM1 = 1;  are different

(1) scan an HTML file, by default
(2) pass HTML code to the browser as usual, up until the server happens upon a PHP line of code
(3) switches over to PHP mode in anticipation of a PHP command
(4) closes out the PHP mode 
(5) resume its scanning in HTML mode.

Information flow of a server-side scripting language

 


PHP - compatibility


Embedding PHP code

<html>
<head><title>Plain HTML - hello.htm</title></head>
<body>Hello World!!</body>
</html>
<html>
<head><title>PHP - hello.php</title></head>
<body><?php echo "Hello World!!<p>";?></body>
</html>
<html>
<head><title>PHP - hello-em.php</title></head>
<body><?php echo "Hello ";?>World!!<p></body>
</html>
 

Comments

    PHP provides 3 different comment characters: 

Variable

Type Description
Integer integer number
Double floating point number
bool boolean (true of false)
Array hybrid of ordered array and associative array
object object with properties and methods

// PHP converts the variable real to string before passing it to the echo function
$real = 123.4;
echo $real;
<html><head><title>PHP samples</title></head>
<body>
<?php 
$number = 5; 
$string1 = "this is string 1\n"; 
$string2 = 'this is another "string"'; 
$real = 37.2;

$array1 = array(10, 20, 30, 40, 50); 
$array2 = array("one" => 10, "two" => 20, "three" => 30); 
$array3 = array(5 => "five", "six", "seven"); 

printf("number: %d; string1: %s; string2: %s; real: %6.2f", $number, $string1, $string2, $real);
?>
<p>

<table border="1" width="40%">
   <tr>
   <td width="40%">$array1[1]</td>
   <td width="60%"><?php print $array1[1];?></td>
   </tr>
   <tr>
   <td width="40%">$array2["one"]</td>
   <td width="60%"><?php print $array2["one"];?></td>
   </tr>
   <tr>
   <td width="40%">$array3[7]</td>
   <td width="60%"><?php print $array3[7];?></td>
   </tr>
</table>

</body></html>

        Display on browser:

       


Built-in Variables

<html><head><title>PHP Test</title></head>
<body>
<?php echo $_SERVER["HTTP_USER_AGENT"];
?>
</body></html>

        Display on browser :

Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)

        Note: $HTTP_USER_AGENT was used instead of $_SERVER["HTTP_USER_AGENT"] in previous version of PHP.

        Display on browser :

       

PHP 4 Credits are follows:
Configuration PHP Core, Standard, ...ftp, mysql, odbc, ..,session, xml, wddx, apache, Apache Environment, HTTP Headers Information, Environment, PHP Variables


Conditionals and Looping

<?php
//
 Conditionals
if
 ($a) {
    print "a is true<BR>\n";
}
 elseif ($b) {
    print "b is true<BR>\n";
}
 else {
    print "neither a or b is true<BR>\n";
}

// Do-while Loops
do
 {
    $c = function1();
}
 while ($c);

//
 While Loops
while
 ($d) {
    print "ok<BR>\n";
    $d = function1();
}

for
 ($i = 0; $i < 5; $i++) {
    print "i=$i<BR>\n";
}

?>

<html><head><title>PHP Test</title></head>
<body>

<?php if (strstr($_SERVER["HTTP_USER_AGENT"],"MSIE")) {
?>
<center><b>You are using Internet Explorer</b></center>
<?
} else {
?>
<center><b>You are not using Internet Explorer</b></center>
<?
}
?> 
</body></html>

Assignment, Equality and Identity

PHP defines 3 operation

$num1 = 1;
$num2 = 1;
$num3 = "1";
($num1 == $num2)  // true, they are equal
($num1 == $num3)  // true, they are equal
($num1 === $num2) // true, they are identical
($num1 === $num3) // false, they are equal, but not identical.


Functions

function add10(&$x) { // pass by reference
    $x = $x +10;
}
$x = 5;
echo $x;  // prints '5'
add10($x);
echo $x; // prints '15'

function add10($x) {
    return $x +10;
}
$before = 5;
$after = add10($before);  // $after = '15'


Dealing with form

<html>
<head><title>PHP sample 2</title></head>

<body>

<p>Please input name and age: </p>
<form method="post" action="sample2.php">
<p>Your name: <input type="text" name="name"></p>
<p>You age: <input type="text" name="age"></p>

<p>
<input type="submit" name="Submit" value="Submit">
</p>

</form>
</body>
</html>
sample2.php
<html>

<head><title>PHP Sample 2</title></head>
<body>
Hi, <?php echo $_POST["name"]; ?>.
You are <?php echo $_POST["age"]; ?> years old. 
</body>

</html>

        Note: $name and $age was used instead of $_POST["name"] and $_POST["age"] in previous version of PHP.

        Display on Browser if "Chan Tai Man" and "18" are input as name and age with respectively: 

Hi, Chan Tai Man. You are 18 years old.

References