PHP Comprehensive Overview

52
PHP MOHAMED LOEY

Transcript of PHP Comprehensive Overview

Page 1: PHP Comprehensive Overview

PHPMOHAMED LOEY

Page 2: PHP Comprehensive Overview

Agenda

Part I: Introduction

Part II: PHP Syntax

Part III: PHP Form Handling

Page 3: PHP Comprehensive Overview

Agenda

Part I: Introduction

What is PHP?

Why we use PHP?

PHP Can

Set Up PHP on Your Own PC

Page 4: PHP Comprehensive Overview

What is PHP?

PHP is a server scripting language, and

is a powerful tool for making dynamic

and interactive Web pages quickly.

PHP is a widely-used, open source

scripting language

PHP scripts are executed on the server

PHP costs nothing, it is free to

download and use

Page 5: PHP Comprehensive Overview

Why we use PHP?

PHP runs on various platforms (Windows, Linux,Unix, Mac OS X, etc.)

PHP is compatible with almost all servers usedtoday (Apache, IIS, etc.)

PHP supports a wide range of databases

PHP is easy to learn and runs efficiently on theserver side

It is powerful enough to be at the core of thebiggest blogging system on the web(WordPress)!

It is deep enough to run the largest socialnetwork (Facebook)!

Page 6: PHP Comprehensive Overview

PHP Can

Generate dynamic page content

Create, open, read, write, delete, andclose files on the server

Send and receive cookies

Add, delete, modify data in yourdatabase

Encrypt data

Output images, PDF files, and evenFlash movies

Page 7: PHP Comprehensive Overview

Set Up PHP on Your Own PC

Install a web server (Apache)

Install PHP

Install a database, such as MySQL

Example: XAMPP (Apache + MySQL +

PHP + Perl)

XAMPP is the most popular PHP

development environment

Page 8: PHP Comprehensive Overview

Agenda

Part II: PHP Syntax

PHP Syntax

Comments in PHP

PHP Case Sensitivity

PHP Variables

PHP echo and print Statements

PHP Operators

PHP Conditional Statements

PHP Loops

PHP Functions

PHP Arrays

Page 9: PHP Comprehensive Overview

PHP Syntax

PHP script can be placed anywhere inthe document.

A PHP script starts with <?php andends with ?>

The default file extension for PHP filesis ".php".

Page 10: PHP Comprehensive Overview

Simple PHP Code

Page 11: PHP Comprehensive Overview

Comments in PHP

A comment in PHP code is a line that isnot read/executed as part of theprogram.

PHP supports three ways ofcommenting:

Page 12: PHP Comprehensive Overview

PHP Case Sensitivity

In PHP, all user-defined functions,classes, and keywords (e.g. if, else,while, echo, etc.) are NOT case-sensitive.

Example:

Page 13: PHP Comprehensive Overview

PHP Case Sensitivity

However; in PHP, all variables are case-sensitive.

$color, $COLOR, and $coLOR aretreated as three different variables

Example:

Page 14: PHP Comprehensive Overview

PHP Variables

Variables are "containers" for storinginformation.

A variable starts with the $ sign, followed bythe name of the variable

A variable name must start with a letter orthe underscore character

A variable name cannot start with a number

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

Variable names are case sensitive ($y and $Yare two different variables)

Page 15: PHP Comprehensive Overview

PHP Variables

PHP has no command for declaring a variable.

Example:

Page 16: PHP Comprehensive Overview

PHP echo and print Statements

In PHP there are two basic ways to get output:echo and print.

There are some differences between echo andprint:

echo - can output one or more strings

print - can only output one string, and returns always1

Example:

Page 17: PHP Comprehensive Overview

PHP Operators

PHP Arithmetic Operators

Page 18: PHP Comprehensive Overview

PHP Operators

PHP Arithmetic Operators Example:

Page 19: PHP Comprehensive Overview

PHP Operators

PHP Assignment Operators

Page 20: PHP Comprehensive Overview

PHP Operators

PHP Assignment Operators Example:

Page 21: PHP Comprehensive Overview

PHP Operators

PHP String Operators

Page 22: PHP Comprehensive Overview

PHP Operators

PHP String Operators Example:

Page 23: PHP Comprehensive Overview

PHP Operators

PHP Increment / Decrement Operators

Page 24: PHP Comprehensive Overview

PHP Operators

PHP Increment / Decrement OperatorsExample:

Page 25: PHP Comprehensive Overview

PHP Operators

PHP Comparison Operators

Page 26: PHP Comprehensive Overview

PHP Operators

PHP Comparison Operators Example:

Page 27: PHP Comprehensive Overview

PHP Operators

PHP Logical Operators

Page 28: PHP Comprehensive Overview

PHP Operators

PHP Array Operators

Page 29: PHP Comprehensive Overview

PHP Operators

PHP Array Operators Example:

Page 30: PHP Comprehensive Overview

PHP Conditional Statements

In PHP we have the following conditionalstatements:

if statement - executes some code only if a specifiedcondition is true

if...else statement - executes some code if acondition is true and another code if the condition isfalse

if...elseif....else statement - selects one of severalblocks of code to be executed

switch statement - selects one of many blocks ofcode to be executed

Page 31: PHP Comprehensive Overview

PHP Conditional Statements

If else Example:

Page 32: PHP Comprehensive Overview

PHP Conditional Statements

Switch example:

Page 33: PHP Comprehensive Overview

PHP Loops

In PHP, we have the following loopingstatements:

while - loops through a block of code as long as thespecified condition is true

do...while - loops through a block of code once, andthen repeats the loop as long as the specifiedcondition is true

for - loops through a block of code a specifiednumber of times

foreach - loops through a block of code for eachelement in an array

Page 34: PHP Comprehensive Overview

PHP Loops

PHP while Loop example:

Page 35: PHP Comprehensive Overview

PHP Loops

PHP do...while Loop example:

Page 36: PHP Comprehensive Overview

PHP Loops

PHP for Loop example:

Page 37: PHP Comprehensive Overview

PHP Loops

PHP foreach Loop example:

Page 38: PHP Comprehensive Overview

PHP Functions

A function is a block of statements that can beused repeatedly in a program.

A function will not execute immediately when apage loads.

A function will be executed by a call to thefunction.

Page 39: PHP Comprehensive Overview

PHP Functions

PHP function examples:

Page 40: PHP Comprehensive Overview

PHP Arrays

An array is a special variable, which can holdmore than one value at a time.

Example:

Page 41: PHP Comprehensive Overview

PHP Associative Arrays

Associative arrays are arrays that use namedkeys that you assign to them.

Example:

Page 42: PHP Comprehensive Overview

PHP Sorting Arrays

PHP array sort functions:

sort() - sort arrays in ascending order

rsort() - sort arrays in descending order

asort() - sort associative arrays in ascending order,according to the value

ksort() - sort associative arrays in ascending order,according to the key

arsort() - sort associative arrays in descending order,according to the value

krsort() - sort associative arrays in descending order,according to the key

Page 43: PHP Comprehensive Overview

Agenda

Part III: PHP Form Handling

PHP GET vs. POST

PHP $_GET

PHP $_POST

Page 44: PHP Comprehensive Overview

PHP Form Handling

One of the most powerful features of PHP isthe way it handles HTML forms. The basicconcept that is important to understand isthat any form element will automatically beavailable to your PHP scripts.

The PHP $_GET and $_POST are used tocollect form-data.

Page 45: PHP Comprehensive Overview

PHP GET vs. POST

Both GET and POST create an array (e.g. array( key=> value, key2 => value2, key3 => value3, ...)). Thisarray holds key/value pairs, where keys are thenames of the form controls and values are the inputdata from the user.

When to use GET?

Information sent from a form with the GET methodis visible to everyone

facebook.com?welcome.php?name=Mohamed

When to use POST?

Information sent from a form with the POSTmethod is invisible to others

facebook.com?welcome.php

Page 46: PHP Comprehensive Overview

PHP $_GET

Example:

Page 47: PHP Comprehensive Overview

PHP $_GET

When the user fills out the form above andclicks the submit button, the form data is sentfor processing to a PHP file named"welcome.php".

welcome.php

Page 48: PHP Comprehensive Overview

PHP $_GET

Result:

Welocome.php Output

Page 49: PHP Comprehensive Overview

PHP $_POST

Example:

Page 50: PHP Comprehensive Overview

PHP $_POST

When the user fills out the form above andclicks the submit button, the form data is sentfor processing to a PHP file named"welcome.php".

welcome.php

Page 51: PHP Comprehensive Overview

PHP $_POST

Result:

Welocome.php Output

Page 52: PHP Comprehensive Overview

THANK U