PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH
Dec 26, 2015
PHP
MOHAMMED SHURRABTO
MISS/ RASHA ATTALLAH
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
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!
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
Embedding PHP Code
<h1>My exciting page!</h1>
<? //start php
//php code goes here
?>
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
Comments
• Three styles– # (as in Perl)– // (as in JavaScript)– /* … */ (as in C)
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
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)
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);
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)
Control Structures
• If– Like C++ or Java– Can include else if clauses (not elsif)
• While, for, do-while– Like javascript
• Foreach– Like Perl
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
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
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
Keys and Values
• Array_keys – Make a traditional array of all the keys
• Array_values – Make a traditional array of all the values
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 />");
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"]; …}
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..
}
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
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
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”
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’]