PHP Overview CS3520 1. PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may...

19
PHP Overview CS3520 1

Transcript of PHP Overview CS3520 1. PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may...

Page 1: 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.

PHP OverviewCS3520

1

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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>

4

PHP tags: <?php and ?> The echo command single line comment ( // ) Multiple line comment (/* and */)

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

Longer example

7

<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>

Page 8: 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.

Output previous example

My URL is:: http://puzzle.sci.csueastbay.edu:/~grewe/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.

Page 9: 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.

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

9

Page 11: 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.

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

Page 12: 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.

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

Page 13: 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.

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

Page 14: 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.

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

Page 15: 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.

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

Page 16: 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.

Function with array as a parameter <?php function takes_array($input) { echo "$input[0] + $input[1] = ", $input[0]+$input[1]; }?>

16

Page 17: 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.

Functions and arguments• DEFAULT – pass by value

17

Page 18: 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.

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

Page 19: 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.

Functions with default parameter values<?phpfunction doit($type = "txt"){ return "Default type is $type.\n";}

//now lets use the functionecho doit();echo doit(null);echo doit("jpg");?>

19