1 / 15

PHP Part 1

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

borna
Download Presentation

PHP Part 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Part 1

  2. 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

  3. 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>

  4. 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>”;

  5. 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

  6. 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;

  7. 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;}

  8. PHP Operators • The similar to JavaScript • +, -, *, /, % • =, +=, -=, *=, /=, %= • ++, -- • ==, ===, !=, <>, !==, >, <, >=, <= • and, or, xor, &&, ||, ! • Different from JavaScript • String: ‘.’, ‘.=‘ String concatenation • Array: +, ==, ===, !=, <>, !==

  9. Conditionals • if (condition) {…} • if (condition) {…} else {…} • if (condition) {…} elseif (condition) {…} else {…} • switch (n) {case 1: … break; • case 2: … break; • default: … • }

  10. Loops • while (condition) {…} • do {…} while(condition); • for (init counter; test counter; increment counter) {…} • foreach($array as $value) {…}

  11. 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

  12. 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

  13. 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

  14. 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); • ?>

  15. 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

More Related