diwe - fundamentals of php

45
Diploma in Web Engineering Module VI: Fundamentals of PHP Rasan Samarasinghe ESOFT Computer Studies (pvt) Ltd. No 68/1, Main Street, Pallegama, Embilipitiya.

Upload: rasan-samarasinghe

Post on 19-Feb-2017

30 views

Category:

Engineering


0 download

TRANSCRIPT

Page 1: DIWE - Fundamentals of PHP

Diploma in Web Engineering

Module VI: Fundamentals of PHP

Rasan SamarasingheESOFT Computer Studies (pvt) Ltd.No 68/1, Main Street, Pallegama, Embilipitiya.

Page 2: DIWE - Fundamentals of PHP

Contents1. Introduction to PHP2. What PHP Can Do?3. PHP Environment Setup4. What a PHP File is?5. PHP Syntax6. Comments in PHP7. echo and print Statements8. PHP Variables9. PHP Data Types10. Changing Type by settype()11. Changing Type by Casting12. PHP Constants13. Arithmetic Operators14. String Operators15. Assignment Operators16. Comparison Operators17. Logical Operators18. Operators Precedence 19. If Statement20. If… Else Statement

21. If… Else if… Else Statement22. Switch Statement23. The ? Operator24. While Loop25. Do While Loop26. For Loop27. break Statement28. continue Statement29. Functions30. User Defined Functions31. Functions - Returning values32. Default Argument Value33. Arguments as Reference34. Existence of Functions 35. Variable Local and Global Scope36. The global Keyword37. GLOBALS Array38. Superglobals39. Static Variables

Page 3: DIWE - Fundamentals of PHP

Introduction to PHP

• PHP is a server-side scripting language widely used for web development.

• PHP is an acronym for "PHP Hypertext Preprocessor“.

• Originally created by Rasmus Lerdorf in 1994.

• PHP is open source and free to download and use.

Page 4: DIWE - Fundamentals of PHP

What PHP Can Do?

• PHP can generate dynamic page content.• PHP can create, open, read, write, delete, and close

files on the server.• PHP can collect form data.• PHP can send and receive cookies.• PHP can add, delete, modify data in your database.• PHP can restrict users to access some pages on your

website.• PHP can encrypt data.

Page 5: DIWE - Fundamentals of PHP

PHP Environment Setup

Get a web host with PHP and MySQL support

Or

Install a web server on your own PC, and then install PHP and MySQL

Page 6: DIWE - Fundamentals of PHP

What a PHP File is?

• PHP files can contain HTML, CSS, JavaScript, and PHP code.

• PHP files have extension ".php“

• PHP code are executed on the server, and the result is returned to the browser as plain HTML.

Page 7: DIWE - Fundamentals of PHP

PHP Syntax

<html><head><title>PHP Demo</title></head><body>

<?phpecho "Hello World!";?>

</body></html>

index.php

Page 8: DIWE - Fundamentals of PHP

Comments in PHP

// This is a single line comment

# This is also a single line comment

/*This is amultiple linecomment */

Page 9: DIWE - Fundamentals of PHP

echo and print Statements

echo can output one or more stringsecho "Hello world!<br>";echo "Hello”, “ world!”;

print can only output one string, and returns 1print "Hello world!<br>";

Page 10: DIWE - Fundamentals of PHP

PHP Variables

Variables are temporary memory locations storing data

$science = 50;$maths = 60;$total = $science + $maths;echo “Total is: $total”;

Page 11: DIWE - Fundamentals of PHP

PHP Variables

• A variable starts with the $ sign

• A variable name must start with a letter or the underscore character

• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

• Variable names are case sensitive

Page 12: DIWE - Fundamentals of PHP

PHP Data Types

• PHP is a Loosely Typed Language

• PHP automatically converts the variable to the correct data type, depending on its value.

• It supports with String, Integer, Floating point, Boolean, Array, Object and NULL data types.

Page 13: DIWE - Fundamentals of PHP

PHP Data types

$pi = 3.14;$week = 7;$name = “Roshan Perera";$city= ‘kandy’;$valid = 10 > 5;

var_dump($pi);var_dump($week);var_dump($name);var_dump($city);var_dump($valid);

Page 14: DIWE - Fundamentals of PHP

Changing Type by settype()

Syntax:settype(mixed var, string type)

Ex:

$x = 3.14;settype($x,"integer");var_dump($x);

Page 15: DIWE - Fundamentals of PHP

Changing Type by Casting

Syntax:var = (datatype) var

Ex:

$x = 3.14;$y = (integer)$x;var_dump($y);

Page 16: DIWE - Fundamentals of PHP

PHP Constants

Once they are defined they cannot be changed

define("GREETING", “Hello world!");echo GREETING;

//Case insensitive constantdefine("GREETING", “Hello world!", true);echo greeting;

Page 17: DIWE - Fundamentals of PHP

Arithmetic Operators

Operator Description Example+ Addition A + B will give 30

- Subtraction A - B will give -10

* Multiplication A * B will give 200

/ Division B / A will give 2

% Modulus B % A will give 0

++ Increment B++ gives 21

-- Decrement B-- gives 19

A = 10, B = 20

Page 18: DIWE - Fundamentals of PHP

String Operators

Operator Name Example

. Concatenation$str1 = "Hello"$str2 = $txt1 . " world!"

.= Concatenation assignment

$str1 = "Hello"$str1 .= " world!"

Page 19: DIWE - Fundamentals of PHP

Assignment Operators

Operator Example

= C = A + B will assign value of A + B into C

+= C += A is equivalent to C = C + A

-= C -= A is equivalent to C = C - A

*= C *= A is equivalent to C = C * A

/= C /= A is equivalent to C = C / A

%= C %= A is equivalent to C = C % A

Page 20: DIWE - Fundamentals of PHP

Comparison Operators

Operator Name Example== Equal (A == B) is false.

=== Identical (A == B) is false.

!= Not equal (A != B) is true.

<> Not equal (A <> B) is true

!== Not identical (A != B) is true.

> Greater than (A > B) is false.

< Less than (A < B) is true.

>= Greater than or equal (A >= B) is false.

<= Less than or equal (A <= B) is true.

A = 10, B = 20

Page 21: DIWE - Fundamentals of PHP

Logical Operators

Operator Name Exampleand And (A and B) is False

or Or (A or B) is True

&& And (A && B) is False

|| Or (A || B) is True

xor Exclusive Or (A xor B) is True

! Not !(A && B) is True

A = True, B = False

Page 22: DIWE - Fundamentals of PHP

Operators Precedence

++, --/, *, %+, -<, <=, >=, >==, ===, !=&&||=, +=, -=, /=, *=, %=, .=andxoror

Prio

rity

Page 23: DIWE - Fundamentals of PHP

If Statement

if(Boolean_expression){ //Statements will execute if the

Boolean expression is true}

Boolean Expression

Statements

True

False

Page 24: DIWE - Fundamentals of PHP

If… Else Statement

if(Boolean_expression){ //Executes when the Boolean expression is

true}else{ //Executes when the Boolean

expression is false}

Boolean Expression

Statements

True

False

Statements

Page 25: DIWE - Fundamentals of PHP

If… Else if… Else Statement

if(Boolean_expression 1){ //Executes when the Boolean expression 1 is true}else if(Boolean_expression 2){ //Executes when the Boolean expression 2 is true}else if(Boolean_expression 3){ //Executes when the Boolean expression 3 is true}else { //Executes when the none of the above condition is true.}

Page 26: DIWE - Fundamentals of PHP

If… Else if… Else Statement

Boolean expression 1

False

Statements

Boolean expression 2

Boolean expression 3

Statements

Statements

False

False

Statements

True

True

True

Page 27: DIWE - Fundamentals of PHP

Switch Statement

switch (value) { case constant: //statements break; case constant: //statements break; default: //statements}

Page 28: DIWE - Fundamentals of PHP

The ? Operator

If condition is true ? Then Value one : Otherwise Value two

$h = 10;$x = $h<12 ? "morning" : "afternoon";

echo $x;

Page 29: DIWE - Fundamentals of PHP

While Loop

while(Boolean_expression){ //Statements}

Boolean Expression

Statements

True

False

Page 30: DIWE - Fundamentals of PHP

Do While Loop

do{ //Statements}while(Boolean_expression);

Boolean Expression

Statements

True

False

Page 31: DIWE - Fundamentals of PHP

For Loop

for(initialization; Boolean_expression; update){ //Statements}

Boolean Expression

Statements

True

False

Update

Initialization

Page 32: DIWE - Fundamentals of PHP

break Statement

Boolean Expression

Statements

True

False

break

Page 33: DIWE - Fundamentals of PHP

continue Statement

Boolean Expression

Statements

True

False

continue

Page 34: DIWE - Fundamentals of PHP

Functions

A function is a block of statements which performing a specific task that can be executed by a call to the function.

Two Categories of Functions:

1. PHP Built-in Functions2. User Defined Functions

Page 35: DIWE - Fundamentals of PHP

User Defined Functions

function functionName(parameters…) { //statements}

functionName(arguments…); //calling function

Page 36: DIWE - Fundamentals of PHP

Functions - Returning values

function functionName(parameters…) { //statements return value;}

//calling function$variableName = functionName(arguments…);

Page 37: DIWE - Fundamentals of PHP

Functions – Default Argument Value

function functionName($parameter = value) {  //statements}

functionName(argument); //calling function

functionName(); //calling function without argument

Page 38: DIWE - Fundamentals of PHP

Functions – Arguments as Reference

function functionName(&$parameters…) { //statements}

functionName($arguments…); //calling function

Page 39: DIWE - Fundamentals of PHP

Existence of Functions

function testFunction(){//statements}

$exist = function_exists("testFunction");

var_dump($exist);

Page 40: DIWE - Fundamentals of PHP

Variable Local and Global Scope

$x = 10; // global scope

function testScope() {$y = 20; // local scopeecho "Variable x: $x <br/>";echo "Variable y: $y <br/>";}

testScope();echo "Variable x: $x <br/>";echo "Variable y: $y <br/>";

Page 41: DIWE - Fundamentals of PHP

The global Keyword

$x = 10; // global scope

function testScope() {global $x;$x = 20;}

testScope();echo "Variable x: $x <br/>";

Page 42: DIWE - Fundamentals of PHP

GLOBALS Array

$x = 10; // global scope

function testScope() {$GLOBALS['x'] = 20;}

testScope();echo "Variable x: $x <br/>";

Page 43: DIWE - Fundamentals of PHP

Superglobals

Superglobals are built-in variables that are always available in all scopes.

$GLOBALS$_SERVER$_REQUEST$_POST$_GET$_FILES$_ENV$_COOKIE$_SESSION

Page 44: DIWE - Fundamentals of PHP

Static Variables

function testScope() {static $x = 0;$x++;echo "$x<br/>";}

testScope();testScope();testScope();

Page 45: DIWE - Fundamentals of PHP

The End

http://twitter.com/rasansmn