Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 PHP Overview CS3520 1

2 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

3 PHP --- and its output PHP PHP Introduction This is HTML! '; ?> Output PHP Introduction This is HTML! This is PHP! 3

4 More PHP PHP Introduction This is HTML! <?php echo 'This is PHP! '; // prints to screen /* Here's a longer comment that spans multiple lines. */ ?> 4 PHP tags: The echo command single line comment ( // ) Multiple line comment (/* and */)

5 PHP file names and code End in php file extension (order.php, login.php …..) You start all PHP scripts with the. 5

6 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

7 Longer example 7

8 Output previous example 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 4 7 Some html goes here.... $jake has the value 4. This is more HTML.

9 Constants Constants can be simply defined as follows <?php // Works as of PHP 5.3.0 const CONSTANT = 'Hello World'; echo CONSTANT; ?> 9

10 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

11 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

12 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

13 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

14 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

15 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

16 Function with array as a parameter 16

17 Functions and arguments DEFAULT – pass by value 17

18 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

19 Functions with default parameter values 19


Download ppt "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."

Similar presentations


Ads by Google