PHP Overview CS3520 1 PHP • PHP = PHP: Hypertext Preprocessor • Server-side scripting language that may be embedded into HTML • One goal is to get PHP files to generate client-side code • end up with HTML, CSS, JavaScript, other client-side code! 2 PHP --- and its output PHP <html> <head> <title> PHP Introduction </title> </head> <body> This is HTML! <br /> <?php echo 'This is PHP! <br />'; ?> </body> </html> Output <html> <head> <title> PHP Introduction </title> </head> <body> This is HTML! <br /> This is PHP! <br /> </body> </html> 3 More PHP <html> <head> <title> PHP Introduction </title> </head> <body> This is HTML! <br /> <?php echo 'This is PHP! <br />'; // prints to screen /* Here's a longer comment that spans multiple lines. */ ?> </body> </html> PHP tags: <?php and ?> The echo command single line comment ( // ) Multiple line comment (/* and */) 4 PHP file names and code • End in php file extension (order.php, login.php …..) • You start all PHP scripts with the <?php open tag and end the tag after your code with ?> . 5 Variables • PHP is a loosely-typed language • Do not need to declare the type of a variable • Type can change throughout the program • Must start with a letter, can contain numbers, no blank spaces • scope (unless defined as global) is the script block it appears inside of. $x = 42; // store the value 42 in $x echo $x; // prints 42 echo $x+1; // prints 43, value of $x is still 42 $x = ‘hello!’ // type of $x can change 6 Longer example <html> <body> <?php // PHP variables always start with $. $jake = 4; $allen = 3 + $jake; echo $jake, " ", $allen ?> <hr> Some html goes here.... <br> <script language="php"> // This is another wayt to enter PHP. And variable values survive between // PHP regions. echo '$jake has the value ', $jake, ".<br>"; </script> <i>This is more HTML.</i> <p> <table> <?php // There are many pre-defined variables which describe the script's // environment. echo "<tr><td><b>My URL is:</b>:</td><td>http://", $_SERVER["SERVER_NAME"], ":", $_SERVER["SCRIPT_NAME"], "</td></tr>"; echo "<tr><td><b>Your browser is</b>:</td><td>", $_SERVER["HTTP_USER_AGENT"], "</td></tr>"; echo "<tr><td><b>Your IP address is</b>:</td><td>", $_SERVER["REMOTE_ADDR"], "</td></tr>"; ?> </table></p> </body> </html> 7 Output previous example 47 Some html goes here.... $jake has the value 4. This is more HTML. My URL is:: http://puzzle.sci.csueastbay.edu:/~gre we/PHP/phpvariables.php Your browser is: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36 Your IP address is: 108.243.32.249 8 Constants • Constants can be simply defined as follows <?php // Works as of PHP 5.3.0 const CONSTANT = 'Hello World'; echo CONSTANT; ?> 9 Some Pre-defined variables--see PHP documentation • There are a number of pre-defined variables in PHP. See http://php.net for a cmoplete listing. Below is a subset of predefined variables/arrays : • $GLOBALS • $_SERVER • $_GET • $_POST • $_FILES • $_COOKIE • $_SESSION • $_REQUEST • $_ENV • 10 Arrays • 2 kinds • Indexed are numerically indexed starting from 0. • Associative arrays associate keys to their values and are indexed by their keys. • The following are examples of two associative arrays, $a, $b defined followed by a numerically index array, $c. $a = array("a" => "apple", "b" => "banana"); $b = array("a" => "pear", "b" => "strawberry", "c" => "cherry"); $c = array("apple", "banana"); $a['b']; //will have the value banana $c[1]; //will have the value of banana 11 Operations – similar to C++, Java • Arithmetic operators • +, -, *, /, % (modulus – remainder after division) • Logical AND (&&), OR (||), NOT (!) • Assignment operators • Shorthand for assignment operators: • $x += $y equivalent to $x = $x + $y • Also works with subtraction, multiplication, division, modulus, and string concatenation 12 Equality: == OR === • Two “equality” operators • == tests for “equality” in value but not necessarily type • === tests for “identity” in value AND type • == ignores the distinction between: • Integers, floating point numbers, and strings containing the same numerical value • Nonzero numbers and boolean TRUE • Zero and boolean FALSE • Empty string, the string ‘0’ and boolean FALSE • Any other non-empty string and boolean TRUE 13 Strings and operations • Concatenation of strings – the . operator $a = ‘hello’; $b = ‘world’; echo $a . ‘ ‘ . $b . ‘!’; // prints ‘hello world!’ • String functions • Length: strlen() • Position of substring: strpos() • More on string functions: http://www.w3schools.com/php/php_ref_string.asp 14 Functions • keyword funciton. • no return type for a function • parameters are defined without type • In the first generic example, you see a function (myFunction) defined with n arguments and the last line of code returns a value. • You would simply call this function via: $the_value = myFunction($a1,$a2,...$an); • Defining the funciton <?php function myFunction($arg_1, $arg_2, /* ..., */ $arg_n) { echo "Example function.\n"; return $retval; } ?> 15 Function with array as a parameter <?php function takes_array($input) { echo "$input[0] + $input[1] = ", $input[0]+$input[1]; }?> 16 Functions and arguments • DEFAULT – pass by value 17 Functions and passing by reference • by default arguments are passed by value to a function. The following code illustrates how to pass by reference: <?php function add_some_extra(&$string) { $string .= 'and something extra.'; } //how to call the function above $str = 'This is a string, '; add_some_extra($str); echo $str; // outputs 'This is a string, and something extra.' ?> 18 Functions with default parameter values <?php function doit($type = "txt") { return "Default type is $type.\n"; } //now lets use the function echo doit(); echo doit(null); echo doit("jpg"); ?> 19