150 likes | 293 Views
PHP Part 1. PHP Hypertext Preprocessor. Widely-used open-source scripting language Scripts are executed on the Server not the Client Runs on almost all webservers PHP files end with . php Can contain HTML, CSS, JavaScript and PHP code
E N D
PHP Hypertext Preprocessor • Widely-used open-source scripting language • Scripts are executed on the Server not the Client • Runs on almost all webservers • PHP files end with .php • Can contain HTML, CSS, JavaScript and PHP code • Are executed on the server, the resulting HTML is sent to the browser • Can collect form data • Can open, read, write, and close files on server • Can add, delete, modify data in a database • Can send and receive cookies
PHP Syntax • PHP scripts start with “<?php” and end with “?>” • PHP files should end with “.php” • <!DOCTYPE html> • <html> • <body> • <h1>Header</h1> • <?php • echo “Hello World!”; • ?> • </body> • </html>
PHP Syntax • Uses //, #, /*…*/ for comments • All user defined functions, classes, and keywords are case-insensitive • All variables are case-sensitive • $color=“red”;echo “My car is “ . $color . “<br>”;echo “My house is “ . $COLOR . “<br>”;echo “My boat is “ . $coLOR . “<br>”;
PHP Variables • Start with the $ sign followed by variable name • Variable name must start with a letter or _ • Variable name cannot start with a number • Variable name can only contain (A-z, 0-9, _) • Variables are loosely typed • Three scopes for variables: • global – declared outside functions • local – declared inside functions • static – inside functions, but not deleted
Data Types • Strings • $x = “Hello world!”;echo $x;$y = ‘Hello world!’;echo $y; • Integers • $x = 4; • $y = 0x8C; // hex number • $z = 047; // octal number • Floating Point Numbers • $x = 3.14; • $y = 2.4e5; • $z = 4E-2;
Data Types • Boolean • $x = true; • $y = false; • Arrays • $x = array(“foo”, “bar”, “baz”); • $y = array(“a”=>23, “answer”=>42); • Objects • class Person {var $name;function Person($name=“Fred”) { $this->name = $name;}function get_name() { return $this->name;}
PHP Operators • The similar to JavaScript • +, -, *, /, % • =, +=, -=, *=, /=, %= • ++, -- • ==, ===, !=, <>, !==, >, <, >=, <= • and, or, xor, &&, ||, ! • Different from JavaScript • String: ‘.’, ‘.=‘ String concatenation • Array: +, ==, ===, !=, <>, !==
Conditionals • if (condition) {…} • if (condition) {…} else {…} • if (condition) {…} elseif (condition) {…} else {…} • switch (n) {case 1: … break; • case 2: … break; • default: … • }
Loops • while (condition) {…} • do {…} while(condition); • for (init counter; test counter; increment counter) {…} • foreach($array as $value) {…}
Functions • Use the ‘function’ keyword • Are case-insensitive • function functionName(param*) {…} • functionName must start with letter or _ • param are variables ($x, $y) • parameters may have default values ($x=4) • may return value using ‘return’ key word
PHP Superglobals • Built-in variables that are always available • $GLOBALS – stores global variables • $_SERVER – information about the server • $_REQUEST – access to data in the request • $_POST – access to data in the POST • $_GET – access to data in the GET • $_FILES • $_ENV • $_COOKIE • $_SESSION
Handling Forms • PHP has three ways to get the data in an HTML Request • $_REQUEST – is an associative array of key, value pairs for the data • $_POST – is an associative array of the data in a POST request • $_GET – is an associative array of the data in a GET request • The htmlspecialchars function converts special characters into HTML entities • &, “, ‘, <, >, etc
Example • <form method=“post” action=“<?php echo $_SERVER[‘PHP_SELF’];?> • Name: <input type=“text” name=“fname”> • <input type=“submit” name=“Submit”> • </form> • <?php • echo “REQUEST: “; var_dump($_REQUEST); • echo “<br/>POST: “; var_dump($_POST); • echo “<br/>GET: “; var_dump($_GET); • ?>
PHP File I/O • fopen(filename,mode) – opens a file on the server • ‘r’ – read only at beginning of file • ‘r+’ – read plus write • ‘w’ – write only at beginning of file • ‘w+’ – read and write • ‘a’ – read only at end of file • ‘a+’ – read and write at end of file • intfwrite(handle, string) – writes string to file • string fread(handle, length) – reads length bytes • array file(filename) – reads file into an array • boolfclose(handle) – closes an open file pointer