CGS 3066 Web Programming and Design Spring 2017

  • Slides: 26
Download presentation
CGS 3066: Web Programming and Design Spring 2017 PHP

CGS 3066: Web Programming and Design Spring 2017 PHP

PHP ● PHP is a server scripting language, and is a powerful tool for

PHP ● PHP is a server scripting language, and is a powerful tool for making dynamic and interactive Web pages quickly. ● PHP is a widely used, free, and efficient alternative to competitors such as Microsoft’s ASP. ● Manage cookies and sessions. ● Where is it used? ● It is powerful enough to be at the core of the biggest blogging system in the web (Word. Press) ● It is deep enough to run the largest social network (Facebook) ● It is also easy enough to be a beginner’s first server side language

PHP Capabilities Generate dynamic page content Create, open, read, write, delete, and close files

PHP Capabilities Generate dynamic page content Create, open, read, write, delete, and close files on the server. Collect form data. Manage cookies and sessions. Add, delete, modify data in database. User management (i. e. restrict users to access some specific webpages) ● PHP can encrypt data. ● ● ●

PHP files ● PHP files can contain text, HTML, CSS, Java. Script, and PHP

PHP files ● PHP files can contain text, HTML, CSS, Java. Script, and PHP code ● PHP code are executed on the server, and the result is returned to the browser as plain HTML ● PHP files have extension ". php" Image Source: https: //en. wikipedia. org/wiki/File%3 a. Scheme_dynamic_page_en. svg

PHP Syntax ● A PHP script can be placed anywhere in the document ●.

PHP Syntax ● A PHP script can be placed anywhere in the document ●. . even before <!DOCTYPE html> ● A PHP script starts with <? php and ends with ? >: ● contains commands separated by ; ● A PHP file normally contains HTML tags, and some PHP scripting code. <html> <body> <h 1>My first PHP page</h 1> <? php echo "Hello World!"; ? > </body></html>

PHP Comments <html> <body> <? php // This is a single line comment #

PHP Comments <html> <body> <? php // This is a single line comment # This is also a single line comment /* This is a multiple lines comment block that spans over more than one line */ ? > </body></html>

PHP Case Sensitivity (php_keywords. php) In PHP, all user-defined functions, classes, and keywords NOT

PHP Case Sensitivity (php_keywords. php) In PHP, all user-defined functions, classes, and keywords NOT case sensitive (unlike Javascript) <html><body> <? php ECHO "Hello World! "; echo "Hello World! "; Ec. Ho "Hello World! "; ? > </body> </html>

PHP Variable Case Sensitivity (php_variable. php) However, in PHP, all variables are case-sensitive. <html><body>

PHP Variable Case Sensitivity (php_variable. php) However, in PHP, all variables are case-sensitive. <html><body> <? php $color = “red”; echo “May car is ”. $color. “ ”; echo “May car is ”. $COLOR. “ ”; echo “May car is ”. $co. Lor. “ ”; ? > </body> </html>

Variables in PHP ● A variable starts with the $ sign, followed by the

Variables in PHP ● A variable starts with the $ sign, followed by the name of the variable. ● A variable name must start with a letter or the underscore character. ● A variable name cannot start with a number. ● A variable name can only contain alphanumeric characters and underscores (A-z, 0 -9, and _ ). ● Variable names are case sensitive ($y and $Y are two different variables).

Data Types PHP supports the following data types ● ● ● ● String Integer

Data Types PHP supports the following data types ● ● ● ● String Integer Float (also double) Boolean Array Object NULL Resource

PHP Constants ● A constant is an identifier (name) for a simple value. The

PHP Constants ● A constant is an identifier (name) for a simple value. The value cannot be changed during the script. ● A valid constant name starts with a letter or underscore (no $ sign before the constant name). ● Unlike variables, constants are automatically global across the entire script. ● To set a constant, use the define() function – it takes three parameters: § § § The first parameter defines the name of the constant, The second parameter defines the value of the constant The optional third parameter specifies whether the constant name should be case- sensitive. Default is false. § define("MAXSIZE", 100);

PHP Operators Various operators can be used in PHP ● ● ● ● Arithmetic:

PHP Operators Various operators can be used in PHP ● ● ● ● Arithmetic: +, -, *, /, **, % Assignment: =, +=, -=, *=, /=, %= String: . (concatenation), . = Increment/decrement: ++ and -- (post and pre) Relational: ==, ===, !==, <, <=, >, >=, <> Logical: and, &&, or, ||, xor, ! Array: +, ===, !=, <>, !==

Conditional Statements ● ● if. . . if … else … Switch <? php

Conditional Statements ● ● if. . . if … else … Switch <? php if ($a > $b) { echo "a is bigger than b"; } elseif ($a == $b) { echo "a is equal to b"; } else { echo "a is smaller than b"; } ? > ● additional control structures(If necessary): http: //php. net/manual/en/language. control-structures. php

Loops ● while : $x = 2; while ($x < 1000) { echo $x.

Loops ● while : $x = 2; while ($x < 1000) { echo $x. “n”; // n is newline character $x = $x * $x; } ● do. . . while: do { echo $x. “n”; $x = $x * $x; } while ($x < 1000); // note the semicolon

Loops(Contd. ) ● for: for ($i = 1; $i <= 10; $i++) { echo

Loops(Contd. ) ● for: for ($i = 1; $i <= 10; $i++) { echo $i. “n”; //prints 1 through 10, one number per line } ● foreach (works only on arrays and objects): $arr = array(1, 2, 3, 4); foreach ($arr as $value) { //$value corresponds to each element in $arr echo $value “n”; }

PHP Functions ● The real power of PHP comes from its functions; it has

PHP Functions ● The real power of PHP comes from its functions; it has more than 1000 built-in functions. ● Besides the built-in PHP functions, we can create our own functions. ● A user defined function declaration starts with the word “function” ● Example: <? php function foo($x, $y) { echo “example function n”; echo $x + $y. n”; return $x + $y; //optional } ? > … <? php foo(3, 4); //prints 7 $retval = foo(5, 6); // prints 11, copies 11 to $retval ? >

Indexed Arrays in PHP • Maintains a list of values using the same variable

Indexed Arrays in PHP • Maintains a list of values using the same variable name and unique index • Array are three types • Indexed array – Array with a numeric index. • Arrays with named keys. • Multidimensional Array- Arrays containing one or more arrays. • array() function is used to create an array $colors = array(“blue”, ”yellow”, “pink”); echo $colors[0]; //prints ‘blue’ echo $colors[2]; //prints ‘pink’ • To append an element to the array: $colors[] = “purple”; • // adds purple at $color[3] To remove an element from the array, use unset(): unset($colors[2]); // also removes the index 2 from array

Associative Arrays in PHP • keep track of a set of unique keys and

Associative Arrays in PHP • keep track of a set of unique keys and the values that they associate to – called an associative array • Same array()function, different syntax $myarray = array( "foo" => "bar", "bar" => "foo", ); • To add an element to the array, use a new key value: $myarray[“newkey”] = “new value”; // adds purple at $color[3] • Use isset() function to test if a variable/keyed value exists of not: isset($myarray[“bar”]); //function returns true isset($myarray[“barr”]); //function returns false

The for-each loop with PHP arrays • Easy way to iterate over all elements

The for-each loop with PHP arrays • Easy way to iterate over all elements of an array • Example, iterate and print all elements of an indexed array: $colors = array(“blue”, ”yellow”, “pink”); foreach ($colors as $color) { echo $color; // simply prints each color } //indexed array foreach ($colors as $indexno => $color) { echo “$indexno => $color ”; // prints color with index } $myarray = array("foo" => "bar", "bar" => "foo"); //associative array foreach ($myarray as $key => $value) { echo “$key => $value ”; // prints values with key }

Collect Form data in PHP

Collect Form data in PHP

Superglobals • A collection of associative arrays those can be accessed from anywhere in

Superglobals • A collection of associative arrays those can be accessed from anywhere in PHP file • • • $GLOBALS $_SERVER $_GET $_POST $_FILES $_COOKIE $_SESSION $_REQUEST $_ENV //array of all variables in ‘global scope’( not belonging to any function scope ) // Server and execution environment information // HTTP GET variables //HTTP POST variables // HTTP File Upload variables // HTTP Cookies // Session variables // contains a copy of contents of $_GET, $_POST and $_COOKIE //environment variables (information passed from web server shell environment) Source: http: //php. net/manual/en/reserved. variables. php

Forms with PHP • Form data is sent to the server when the user

Forms with PHP • Form data is sent to the server when the user clicks “Submit”. • The PHP superglobals $_GET and $_POST are used to collect form-data (depending on the method attribute of the submitted form) • $_GET array populated from the URL of the submitted form. • Example: www. example. com/form. php? fname=john&lname=doe maps $_GET[“fname”] to “john”, $_GET[“lname”] to “doe” • $_POST is an array of variables passed to the current script via the HTTP POST method.

$_GET vs. $_POST • $_GET: • HTTP GET requests can be cached, bookmarked •

$_GET vs. $_POST • $_GET: • HTTP GET requests can be cached, bookmarked • GET requests are limited by the length of the URL (2000 characters) • No privacy, not suitable for user authentication • $_POST: • Cannot be cached , browser must make request to the server with form data • Cannot be bookmarked • Used to submit sensitive information • No limit on the posted data volume

Example: client. html <html> <head><title>Executing on Client Side</title></head> <body> <form action=“server. php" method="post"> Name:

Example: client. html <html> <head><title>Executing on Client Side</title></head> <body> <form action=“server. php" method="post"> Name: <input type="text" name="name"> <input type="submit" name="submit" value="Submit Form"> </form> </body> </html>

Example: server. php <html> <head><title>Executing on the Server Side</title></head> <body> The name that was

Example: server. php <html> <head><title>Executing on the Server Side</title></head> <body> The name that was submitted was: <? php echo $_POST['name']; ? > </body> </html>

Example: self-submit form value (php_self_submit. php) <html> <body> <!– form submits to same page

Example: self-submit form value (php_self_submit. php) <html> <body> <!– form submits to same page by default, no need to set action --> <form method="post"> Name <input type="text" name="name"> <input type="submit" name="submit" value="Submit Form"> </form> <? php if(isset($_POST['submit'])) //tests if the submit button is clicked or not { $name = $_POST['name']; echo “From Server: The name that was submitted was: <b> $name </b>"; echo " You can use the above form again to enter a new name. "; } ? > </body> </html> http: //www. html-form-guide. com/php-form-action-self. html