Top Banner
PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH
23

PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Dec 26, 2015

Download

Documents

Meghan Chapman
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

PHP

MOHAMMED SHURRABTO

MISS/ RASHA ATTALLAH

Page 2: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

What is PHP?

• Stands for "PHP Hypertext Preprocessor"

• Server-side scripting language• HTML-embedded• Supports 15 different databases, POP3, IMAP, COM, CORBA

• Website: http://www.php.net

Page 3: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

PHP alternatives

• CGI (+ back-end program)• ASP (MS Active server pages)• JSP (Sun's Java server pages)• Cold Fusion

• Most of these are not free!

Page 4: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

PHP Operation

• PHP interpreter (on server) reads PHP file

• As it is processed, it outputs HTML to the server

• Embedded HTML code (including JavaScript) is passed to the server unchanged

• Result: "view source" sees only generated HTML, not PHP source

Page 5: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Embedding PHP Code

<h1>My exciting page!</h1>

<? //start php

//php code goes here

?>

Page 6: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

PHP variables

• All variables begin with $• Variables are not declared• Unassigned variables have value NULL– IsSet($var) returns true if var is not NULL

• Variable names are case sensitive• Reserved words (like true, while, etc.) are not

Page 7: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Comments

• Three styles– # (as in Perl)– // (as in JavaScript)– /* … */ (as in C)

Page 8: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Numbers and conversions

• Integers and floating point numbers (autotyped when value set)$num = 3; //integer$num = 3.5; //now floating point$num = (int) num ; //back to integer 3 again

$num2 = intval(num); //another way to do that

$num3 = 3.5; settype($num3, "integer");//.. And a third way

Page 9: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Output statements

• Echo– With no parentheses, multiple parameters, otherwise only one

– echo "First line <br />", "Second line <br />";

• Print– One parameter, coerced to string, returns result

– print("The value is "); print(47);• Printf (next slide)

Page 10: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Printf

• Stands for "print formatted"• Takes multiple parameters

– A format string, containing directives such as $d (decimal), $f (float), $s (string)• Formatting directives can include parameters between $ and letter, e.g. $10d = decimal integer of length 10

– Values to insert for the $ directives

• Example– printf("Your purchase of $d $s (s) costs $.2f", $quantity, $item, $total_price);

Page 11: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Relational & Boolean Operators

• Relational operators (for all types)== Same type, same value!= Opposite of ==>, <, >=, <= Numeric (preferred) or string comparison of values with coercion

• Boolean operators&&, and (Precedence of and is higher, otherwise same)

||, or (precedence of or is higher, otherwise same)

xor (exclusive or)! (not)

Page 12: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Control Structures

• If– Like C++ or Java– Can include else if clauses (not elsif)

• While, for, do-while– Like javascript

• Foreach– Like Perl

Page 13: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Arrays

• Combine arrays with Perl hashes• Each element has a key and a value– Typical array: key is the integer index

– Hash-like array: key is a string

• Array names begin with $ like other variables

Page 14: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Creating arrays

• $list1[0] = 17; //creates list unless it exists

• $list1[] = "hi"; //adds new last element

• $list2 = array(10,20,30,40); //creates a traditional array of 4 elements

• $hash = array("a"=>"apple", "b"=>"banana"); //creates a hash with 2 elements

• $list3 = array(); //creates an empty array

Page 15: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Accessing Array elements

• $x = $hash['a'] //gets "apple" from the hash

• $y = $list1[1] //gets "hi" from the traditional array

• $list ($first, $second, $third, $fourth) = $list2; //assigns list2 to 4 individual variables

Page 16: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Keys and Values

• Array_keys – Make a traditional array of all the keys

• Array_values – Make a traditional array of all the values

Page 17: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Sequential access

• current($array): current element in the array– (initially element 0)

• next($array): increments the current element and returns the value

$city = current($cities);print("$city <br / >");while($city = next($cities)) print("$city <br />");

Page 18: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Each (array)

• Like next but returns 2-element array of key and value (keys are "key" and "value")

• Does not return FALSE for NULL value

while($data=each($salaries)){ $name = $data["key"]; $sal = $data["value"]; …}

Page 19: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Foreach

• Shorthand for each• Use for traditional or hash arraysforeach($salaries as $name=>$sal)){ //process names and salaries…}

foreach($salaries as $sal){//process salaries without names..

}

Page 20: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Forms

• Forms (HTML, not PHP) collect input

• <form action=URL method={get or post}– Get: parameters in URL– Post: parameters not visible– <input> and <select> statements collect data

Page 21: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Input tags

• Create a box that you type in:– <input type=“text” name=“lname”>

• What you type will be passed as “parameter” lname

• Many other types of input (checkbox, radio buttons, etc.) -- see documentation

Page 22: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Select

• Create a menu– <select name=“ssn”>– <option value=“123456789> Mickey Mouse </option>

– …– <select>

• If you select an option, it is passed as parameter “ssn”

Page 23: PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH. What is PHP? Stands for "PHP Hypertext Preprocessor" Server-side scripting language HTML-embedded Supports.

Forms and PHP

• If method=post– $_POST is hash with names and values from widgets

• If method=get– $_GET is hash with names and values from widgets

• Example:•$type = $_POST[‘cuporcone’]