PHP_Scripts.txt anne.dawson@gmail.com Last updated: Saturday 23rd April 2016, 10:47 PT, AD ************************************* IMPORTANT: READ ALL OF THESE COMMENTS BEFORE STUDYING THE PHP SCRIPTS BELOW ************************************* PHP statements can be embedded within HTML (or XHTML). HTML files with embedded PHP statements are saved with a filename ending in .php e.g. 01-01.php Note: lines starting with a // are comments in PHP In the HTML part of a PHP script, you must use HTML comments which start with the characters HTML comments are ignored by the browser. The example PHP scripts (shown below) are saved on the web server at http://annedawson.comlu.com For convenience, all scripts are reproduced here because you will not be able to view their content by using the View -> Source menu option of your browser. Using the View -> Source menu option of your browser, you WILL see the HTML source, but only the **output** of the PHP script. [Go to the PHP Tutorial for more detailed explanations: http://www.w3schools.com/php/default.asp] The web server at http://annedawson.comlu.com is a Linux Apache web server, and the PHP language (as well as MySQL) is installed on the same machine. The following PHP scripts can be run by typing the filename after this address in a browser window: http://annedawson.comlu.com Therefore, to run the first script (01-01.php), type the following line in the address box of your browser: http://annedawson.comlu.com/01-01.php The first PHP file (01-01.php) has only one PHP statement: When you run this script, you will see the words Hello World displayed in the browser window. Example PHP scripts are shown below. For convenience, I have separated each PHP script with a line of * characters, but these are NOT part of the PHP script. NB: PHP scripts are executed on the web server. You will not be able to execute these scripts on a local hard drive unless the PHP language is installed on that hard drive. Go to the PHP Tutorial for a more detailed explanation of PHP features: http://www.w3schools.com/php/default.asp The following are example PHP scripts: *********************************************************** *********************************************************** *********************************************************** inside a string // forces any following text to be printed // on a new line $txt1 = "Good morning
"; $txt2 = "CSCI165A students!"; echo $txt1 . $txt2; // you may use print instead of echo to output text to the browser... print $txt1 . $txt2; ?> *********************************************************** "; print "y = " . strval($y) . "
"; print "x + y = " . strval($x + $y) . "
"; print "x - y = " . strval($x - $y) . "
"; print "x * y = " . strval($x * $y) . "
"; print "x / y = " . strval($x / $y) . "
"; // % is the modulus operator: gives the remainder after an integer division print "x % y = " . strval($x % $y) . "
"; // the following line adds one to the $x variable $x++; // the following line adds one to the $y variable $y++; print "x = " . strval($x) . "
"; print "y = " . strval($y) . "
"; print "x + y = " . strval($x + $y) . "
"; print "x - y = " . strval($x - $y) . "
"; print "x * y = " . strval($x * $y) . "
"; print "x / y = " . strval($x / $y) . "
"; // % is the modulus operator: gives the remainder after an integer division print "x % y = " . strval($x % $y) . "
"; ?> *********************************************************** < >= <= != == // see: http://www.w3schools.com/php/php_operators.asp $x = 10; $y = 5; // Note: strval is a function to convert a number to a string // used like the Python str() function // when used with strings, the . will join (concatenate) strings print "x = " . strval($x) . "
"; print "y = " . strval($y) . "
"; if ($x == $y) { print "x is equal to y" . "
"; print "y is equal to x" . "
"; } else { print "x is NOT equal to y" . "
"; print "y is NOT equal to x" . "
"; } $y = 10; print "x = " . strval($x) . "
"; print "y = " . strval($y) . "
"; if ($x == $y) { print "x is equal to y" . "
"; print "y is equal to x" . "
"; } else { print "x is NOT equal to y" . "
"; print "y is NOT equal to x" . "
"; } ?> *********************************************************** "; print "y = " . strval($y) . "
"; if (($x >= $y) && ($y == 5)) // both have to be true for the next statement to print { print "x is greater than or equal to y" . " AND " . "y is equal to 5" . "
"; } else // i.e. one of the above conditions must be false { print "Either x is less than y OR y is not equal to 5" . "
"; } $y = 10; print "x = " . strval($x) . "
"; print "y = " . strval($y) . "
"; if (($x >= $y) && ($y == 5)) // both have to be true for the next statement to print { print "x is greater than or equal to y" . " AND " . "y is equal to 5" . "
"; } else // i.e. one of the above conditions must be false { print "Either x is less than y OR y is not equal to 5" . "
"; } ?> *********************************************************** "; $i++; // this is the same as writing $i = $i + 1; } ?> *********************************************************** "; print "Hello World!
"; } ?> ***********************************************************
"; echo date("g:i A l, F j Y."); // g = the hour, in 12-hour format // i = minutes // A = print AM or PM, depending... // l = print the day of the week // F = print the month // j = print the day of the month // Y = print the year - all four digits ?> ***********************************************************