PHP Overview CS 3520 1 PHP PHP PHP

  • Slides: 20
Download presentation
PHP Overview CS 3520 1

PHP Overview CS 3520 1

PHP • PHP = PHP: Hypertext Preprocessor • Server-side scripting language that may be

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, Java. Script, other client-side code! 2

PHP --- and its output PHP <html> <head> <title> PHP Introduction </title> </head> <body>

PHP --- and its output PHP <html> <head> <title> PHP Introduction </title> </head> <body> This is HTML! <? php echo 'This is PHP! '; ? > </body> </html> Output <html> <head> <title> PHP Introduction </title> </head> <body> This is HTML! This is PHP! </body> </html> 3

More PHP <html> <head> <title> PHP Introduction </title> </head> <body> This is HTML! <?

More PHP <html> <head> <title> PHP Introduction </title> </head> <body> This is HTML! <? php echo 'This is PHP! '; // 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 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

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 7

Longer example 7

Output previous example Try it 47 Some html goes here. . $jake has the

Output previous example Try it 47 Some html goes here. . $jake has the value 4. This is more HTML. My URL is: : http: //csweb 01. csueastbay. edu/~grew e/CS 3520/PHP/phpvariables. php Your browser is: Mozilla/5. 0 (Windows NT 6. 3; WOW 64) Apple. Web. Kit/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

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 a number of pre-defined variables in PHP.

Some Pre-defined variables--see PHP documentation • There a number of pre-defined variables in PHP. See http: //php. net for a complete listing. Below is a subset of predefined variables/arrays : (http: //php. net/manual/en/reserved. variables. php ) • $GLOBALS • $_SERVER • $_GET • $_POST • $_FILES • $_COOKIE • $_SESSION • $_REQUEST • $_ENV 10

Arrays • 2 kinds • Indexed are numerically indexed starting from 0. • Associative

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 • +, -, *, /,

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

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

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. w 3 schools. com/php_ref_string. asp 14

Functions • keyword funciton. • no return type for a function • parameters are

Functions • keyword funciton. • no return type for a function • parameters are defined without type • In the first generic example, you see a function (my. Function) defined with n arguments and the last line of code returns a value. • You would simply call this function via: $the_value = my. Function($a 1, $a 2, . . . $an); • Defining the funciton <? php function my. Function($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] +

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 arguments • DEFAULT – pass by value 17

Functions and passing by reference • by default arguments are passed by value to

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

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

More to learn…. • On our website we have a number of Ph. P

More to learn…. • On our website we have a number of Ph. P examples and even have a shopping cart example –go there and read them. • There is much more to learn about Ph. P but, this is enough for our to do our Project and gives you some exposure to server side code using PHP. • Other lectures on our website for PHP cover: form processing, database and more… 20