Hiram CPSC 356 - PHP CPSC 356

Unformatted text preview:

PHPWhat is PHP?PHP alternativesPHP OperationEmbedding PHP CodePHP variablesCommentsNumbers and conversionsOutput statementsPrintfRelational & Boolean OperatorsControl StructuresArraysCreating arraysAccessing Array elementsKeys and ValuesSequential accessEach (array)ForeachFormsInput tagsSelectForms and PHPPHPEllen WalkerCPSC 356What 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.netPHP 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 sourceEmbedding 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 notComments•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 wayOutput 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 PerlArrays•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 variablesCreating 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 arrayAccessing 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 variablesKeys and Values•Array_keys –Make a traditional array of all the keys•Array_values –Make a traditional array of all the valuesSequential 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 valuewhile($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 dataInput 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 documentationSelect•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 =


View Full Document

Hiram CPSC 356 - PHP CPSC 356

Download PHP CPSC 356
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view PHP CPSC 356 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view PHP CPSC 356 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?