Download presentation
Presentation is loading. Please wait.
Published byMeghan Chapman Modified over 9 years ago
1
PHP MOHAMMED SHURRAB TO MISS/ RASHA ATTALLAH
2
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
3
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!
4
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
5
Embedding PHP Code My exciting page! <? //start php //php code goes here ?>
6
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
7
Comments Three styles –# (as in Perl) –// (as in JavaScript) –/* … */ (as in C)
8
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
9
Output statements Echo –With no parentheses, multiple parameters, otherwise only one –echo "First line ", "Second line "; Print –One parameter, coerced to string, returns result –print("The value is "); print(47); Printf (next slide)
10
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);
11
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)
12
Control Structures If –Like C++ or Java –Can include else if clauses (not elsif) While, for, do-while –Like javascript Foreach –Like Perl
13
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
14
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
15
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
16
Keys and Values Array_keys –Make a traditional array of all the keys Array_values –Make a traditional array of all the values
17
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 "); while($city = next($cities)) print("$city ");
18
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"]; … }
19
Foreach Shorthand for each Use for traditional or hash arrays foreach($salaries as $name=>$sal)){ //process names and salaries… } foreach($salaries as $sal){ //process salaries without names.. }
20
Forms Forms (HTML, not PHP) collect input <form action=URL method={get or post} –Get: parameters in URL –Post: parameters not visible – and statements collect data
21
Input tags Create a box that you type in: – What you type will be passed as “parameter” lname Many other types of input (checkbox, radio buttons, etc.) -- see documentation
22
Select Create a menu – – Mickey Mouse –… – If you select an option, it is passed as parameter “ssn”
23
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’]
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.