Web Programming CSC 242 Professor John Carelli Kutztown
Web Programming CSC 242 Professor John Carelli Kutztown University Computer Science Department
Introduction to PHP • A scripting language used to allow for dynamic exchange of between a client and a server • runs on the server side (not on the client machine!) • interpreted • all major web servers contain interpreters • loosely-typed language • variables are not declared, they are created when used • variable type is context dependent and can change Professor John Carelli Kutztown University Computer Science Department
PHP Basics • PHP code is embedded in HTML • • • contained within a php tag may be multi-line code is executed when encountered statements end with a semicolon ‘; ’ supports C++ style comments (both // and /* … */) • Basic output with either print or echo commands • they are very similar to each other • in both cases, the output is interpreted by the browser as HTML Professor John Carelli First. Page. php – use a “. php” extension Kutztown University print or echo? • either can be used with or without parenthesis • print has a return value of “ 1” (can be used in expressions) • print allows only one argument • echo can take more than one argument Computer Science Department
phpinfo() • Command for retrieving PHP-related information from the server • since PHP is installed on the server, not the client • Including • • • version variable settings directives environmental settings … Professor John Carelli php. INFO. php Kutztown University Computer Science Department
PHP Variables • All PHP variables begin with a dollar sign ‘$’ (from perl) • variables do not need to be declared • they come into existence when first used • $x= 2; • type is context dependent • string literals can be created with either single or double quotes • single vs. double quotes • variables in double quotes are expanded $x=2; echo “$x”; // will produce output of 2 variables. php • variables in single quotes are not expanded $x=2; echo ‘$x’; // will produce output of $x Professor John Carelli Kutztown University Computer Science Department
PHP, more basics • Strings • dot is the string concatenation operator • “John”. “ “. “Doe” -> “John Doe” • substr(), strtok(), trim() …. various string related functions • Numerical functions • round(), number_format() … some. Basics. php • HTML tag functions • htmlspecialcharacters(), strip_tags() … Professor John Carelli Kutztown University Computer Science Department
Looping and testing PHP offline testing and verification • from C++ • • php command: • php –a • Interactive shell – execute php commands • php –l file. php • “lint” option • check syntax of php code in a file if … elseif (one word) … else switch for while do while break, continue exit (like ‘C’), die (like perl) – either can print a message some. Basics. php Professor John Carelli Kutztown University Computer Science Department
PHP Arrays • Associative arrays • index can be either a string or an integer • a string containing a valid integer will be cast to an integer • floats and bools get cast to integers (floats get truncated) • index enclosed in square brackets • $my. Array[“one”] = 1; • Printing • individual elements must be enclosed in braces for printing print "Name= {$Student['name']}"; // error if no braces!!!! • print_r() • print an entire array: print_r($Student); • enclose in <pre> tags to make it pretty: Professor John Carelli Kutztown University first. Array. php Computer Science Department
PHP file access • Basic syntax: $f=fopen($filename, $mode); $line= fgets($f); // reads one line fwrite($f, $string); // write string feof($f) // detect end of file fclose($f); File access modes "r“ "r+“ "w+“ "a" "a+" "x+" read only; starts at the beginning of the file read and write; starts at the beginning of the file write only; clears file contents or creates a new file read and write; clears file contents or creates a new file write only; writes to end of file or creates a new file read and write; writes to end of file or creates a new file write only; creates a new file - error if file exists read and write; creates a new file - error if file exists file. Access. php • File() function: $lines = file ("file. txt"); // read the entire file into an array With optional arguments: $trimmed= file ("file. txt”, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); Professor John Carelli Kutztown University Computer Science Department
Sending Information to the Server • Use an HTML form to collect user information • set the method attribute in the form tag to either “post” or “get” • set the action attribute to the name of the php page to execute upon submission • Form information gets stored in either the $_POST or $_GET array, which accessible in the targeted php page Professor John Carelli Kutztown University Computer Science Department
post vs. get • post • transferred information is hidden from the user • more secure • get • • transferred information is visible in the URL sent to the server appended to URL after an @ keyword/value pairs separated by & can be sent directly via a link (no form) Examples: Sending Information to the Server Professor John Carelli Kutztown University Computer Science Department
Still more… • Arrays examples and sorting • array. Examples. php and sort. Arrays. php • Arrays can be used in a form… • …and passed as a single entity • isset() and is_array() functions • array. In. Form. php Professor John Carelli Kutztown University Computer Science Department
Functions • use function keyword to declare a function • use return keyword to return a value, is desired • arguments and internally defined variables have local scope • use global keyword to access variables defined outside of function • arguments may have default values • list() and array() functions. php Professor John Carelli Kutztown University Computer Science Department
Controlling settings and reporting • ini_set() • changes PHP settings • error_reporting() • changed PHP error reporting levels • check phpinfo() to see current settings… php. Settings. php Professor John Carelli Kutztown University Computer Science Department
- Slides: 14