26
Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

Embed Size (px)

Citation preview

Page 1: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

Chap 3 – PHP Quick Start

COMP268-800RLProfessor Mattos

Page 2: 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.• Covered in Chapter 2, Week 1

Page 3: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 4: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 5: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

PHP Syntax and Constructs – Data Types

• Core: integer, float, string and boolean

• Special: null, array, object and resource.

• Covered in Chapter 4, Week 3

Page 6: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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;

Page 7: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 8: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

PHP Syntax and Constructs – Constants

• Defined with the define() function.• They are global in scope.

• define(“PI”, 3.141592);

Page 9: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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;

Page 10: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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 <<<NOTEIt is raining. <br>NOTE;

Page 11: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

PHP Syntax and Constructs – Boolean Values

• Exactly one bit with two possible values:– 0 or 1– True or False– Off or On

Page 12: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 13: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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)

Page 14: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 15: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 16: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 17: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

PHP Syntax and Constructs – Conditional Operator

• Short form of the if/else syntax

(condition) ? statement_if_true : statement_if_false

Page 18: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

PHP Syntax and Constructs – Loops 1/4

• while -> as long as the expression tests true, the loop continues to iterate.

while (conditional expression) {statements;}

Page 19: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

PHP Syntax and Constructs – Loops 2/4

• do-while -> checks looping expression at the end of the loop block.

do {statements;} while (expression);

Page 20: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 21: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 22: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 23: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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

Page 24: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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.

Page 25: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

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.

Page 26: Chap 3 – PHP Quick Start COMP268-800RL Professor Mattos

PHP Syntax and Constructs – Regular Expressions

• PHP supports pattern matching with regular expressions and regular expression metacharacters.

• Covered in Chapter 12, Week 11.