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:/* for multiline
comment. But, they cannot be nested. */ |
// a single line comment $num1 = 234; // A single line comment |
# a single line comment $string1 = "Hello World!!" # A single line comment. |
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;
|
echo 100 + "100";
PHP:
$num
= (string) 12;
$num1
= 12; settype($num1, "double");
<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
$GLOBALS -
Variables set by the web server or directly related to the execution environment of the current script. Analogous to the old $HTTP_SERVER_VARS array (deprecated).
Variables provided to the script via HTTP GET. Analogous to the old $HTTP_GET_VARS array (deprecated).
Variables provided to the script via HTTP POST. Analogous to the old $HTTP_POST_VARS array (deprecated).
Variables provided to the script via HTTP cookies. Analogous to the old $HTTP_COOKIE_VARS array (deprecated).
Variables provided to the script via HTTP post file uploads. Analogous to the old $HTTP_POST_FILES array (deprecated).
Variables provided to the script via the environment. Analogous to the old $HTTP_ENV_VARS array (deprecated).
Variables provided to the script via any user input mechanism, and which therefore cannot be trusted.
Variables which are currently registered to a script's session. Analogous to the old $HTTP_SESSION_VARS array (deprecated)
<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.
<html><head><title>PHP
Test</title></head> <body> <?php phpinfo(); ?> </body></html> |
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 |
<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
|
|
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> </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