1 / 26

Chap 3 – PHP Quick Start

Chap 3 – PHP Quick Start. COMP268-800RL Professor Mattos. PHP Syntax and Constructs – The Tag. <?php -> opening tag ?> -> closing tag PHP script is placed between PHP open tag “<?php” and PHP close tag “?>”. The code between these two tags is what the PHP module processes.

landry
Download Presentation

Chap 3 – PHP Quick Start

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. Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

  2. PHP Syntax and Constructs – The Tag • <?php -> opening tag • ?> -> closing tag • PHP script is placed between PHP open tag “<?php” and PHP close tag “?>”. • The code between these two tags is what the PHP module processes. • Covered in Chapter 2, Week 1

  3. PHP Syntax and Constructs – Comments • There are three styles of comments: • C -> /* This is a C style comment. */ • C++ -> // This is a C++ style comment. • Shell -> # This is a shell style comment. • Covered in Chapter 2, Week 1

  4. PHP Syntax and Constructs – Printing Output • “print” displays a string and returns an integer value. • “echo” prints comma-separated strings and does not return anything. • You also have printf(), sprintf() and fprintf(). Covered in Chapter 6, Week 5

  5. PHP Syntax and Constructs – Data Types • Core: integer, float, string and boolean • Special: null, array, object and resource. • Covered in Chapter 4, Week 3

  6. PHP Syntax and Constructs – Variables • Start with a “$” sign, followed by a letter and any number of alphanumeric characters, including underscore. • $first_name = “Roberto” • $last_name = “Mattos” • echo $first_name, “ “, $last_name;

  7. PHP Syntax and Constructs – Predefined Variables • Also called “superglobals”, as they are available anywhere in the script. $_GLOBALS $_FILES $_ENV $_GET $_POST $_SESSION $_COOKIE $_REQUEST $_SERVER

  8. PHP Syntax and Constructs – Constants • Defined with the define() function. • They are global in scope. • define(“PI”, 3.141592);

  9. PHP Syntax and Constructs – Numbers • PHP supports integers as well as floating-point, scientific notation, booleans and null. • $year = 2013; • $price = 29.95; • $color = 0x33CC99; • $distance_to_moon = 3.844e+5;

  10. PHP Syntax and Constructs – Strings and quotes • Sequence of characters enclosed in quotes. • Quotes must match-> “string” or ‘string’ • Variables and backslash sequences are interpreted within double quotes. • here-doc -> block of text embedded between user-defined tags, first preceded by <<<. Entire block is treated as surrounded by double quotes. print <<<NOTE It is raining. <br> NOTE;

  11. PHP Syntax and Constructs – Boolean Values • Exactly one bit with two possible values: • 0 or 1 • True or False • Off or On

  12. PHP Syntax and Constructs – Null • Null means no value assigned (not a blank space, not an empty string, not a zero). • unset() function assigns Null to a variable. • unset($var); // now $var is Null

  13. PHP Syntax and Constructs – Operators • Assignment: =, +=, -=, *=, %=, ^=, &=, |=, .= • Equality: ==, != • Identical: ===, !=== • Relational: >, >=, <, <= • Logical: &&, ||, ! • Auto increment/auto decrement: ++, -- • Bitwise: ~, &, |, ^, <<, >> • String Concatenation: . • Arithmetic: *, /, -, +, % • Casting: (int), (float), (string), (bool), (array), (object)

  14. PHP Syntax and Constructs – Arrays • Indexed collection of data. • PHP supports traditional and associative arrays. • Traditional-> indexed by integers starting at 0. • Associative-> indexed by strings. • Covered on Chapter 8, Week 7

  15. PHP Syntax and Constructs – Conditionals 1/2 • if, if/else, if/elseif -> If expression is evaluated to true, block following the expression is executed. if (expression) { statements; } elseif (expression){ statements; } else { statements; }

  16. PHP Syntax and Constructs – Conditionals 2/2 • Switch -> expression is evaluated and matched against a series of case values until one matches. switch ($variable_name) { case valueA: {statements;} break; case valueB: {statements;} break; default: {statements;} }

  17. PHP Syntax and Constructs – Conditional Operator • Short form of the if/else syntax (condition) ? statement_if_true : statement_if_false

  18. PHP Syntax and Constructs – Loops 1/4 • while -> as long as the expression tests true, the loop continues to iterate. while (conditional expression) { statements; }

  19. PHP Syntax and Constructs – Loops 2/4 • do-while -> checks looping expression at the end of the loop block. do { statements; } while (expression);

  20. PHP Syntax and Constructs – Loops 3/4 • for -> has three expressions to evaluate, separated by semicolon. First initializes a variable. Second tests whether the value is true, and if so, the block of statements is executed. After execution of the block, third expression is executed, changing value to be tested again. for (initialization; conditional expression; increment/decrement){ statements; }

  21. PHP Syntax and Constructs – Loops 4/4 • foreach -> iterate through an array. foreach($array_name as $value){ statements; } foreach($array_name as $name=>$value){ statements; }

  22. PHP Syntax and Constructs – Loop control • “break” statement -> break out of a loop from within the loop block. • “continue” statement -> skip over the remaining statements within the loop and start back at the top of the loop for a new iteration

  23. PHP Syntax and Constructs – Functions • Functions -> block of code that performs a task and can be invoked from another part of the program. function function_name (argument1, argument2, argument3, …){ statements; } • Covered on Chapter 9, week 8

  24. PHP Syntax and Constructs – Classes and Objects • PHP support objects. • Class is a collection of variables and functions, called properties and methods. • Objects are created with the “new” operator. • $this is a special pseudo-variable that references the current object. • PHP supports inheritance.

  25. PHP Syntax and Constructs – Files • PHP comes with a set of built-in functions to work with files. require -> replaced by contents of file. require_once -> replaced with contents of file. include -> same as require, but happens only during program execution. include_once -> same as require_once, but happens only during program execution. • To open a file, filename must be assigned to a filehandle. • Covered on Chapter 11, week 10.

  26. PHP Syntax and Constructs – Regular Expressions • PHP supports pattern matching with regular expressions and regular expression metacharacters. • Covered in Chapter 12, Week 11.

More Related