Php Intermediate

31
PHP Intermediate Jamshid Hashimi Trainer, Cresco Solution http://www.jamshidhashimi.com [email protected] @jamshidhashimi ajamshidhashimi Afghanistan Workforce Development Program

Transcript of Php Intermediate

Page 1: Php Intermediate

PHP Intermediate

Jamshid HashimiTrainer, Cresco Solution

http://www.jamshidhashimi.com [email protected] @jamshidhashimi ajamshidhashimi

Afghanistan Workforce Development Program

Page 2: Php Intermediate

Agenda

• Arrays• Loop– For statement– Foreach statement– While statement– Do While statement

• PHP Functions• Get & Post Variable• Difference between PHP 4 & PHP 5• Exercise!

Page 3: Php Intermediate

Arrays

• An array is a special variable, which can hold more than one value at a time.

• array — Create an array

• Multidimensional Arrays

$arr = array("a" => "orange", "b" => "banana", "c" => "apple");

$fruits = array (    "fruits"  => array("a" => "orange", "b" => "banana", "c" => "apple"),    "numbers" => array(1, 2, 3, 4, 5, 6),    "holes"   => array("first", 5 => "second", "third"));

Page 4: Php Intermediate

Arrays: count()

• count — Count all elements in an array, or something in an object

$arr = array("Kabul", "Balkh", "Herat", "Qandahar");echo count($arr);> 4

Page 5: Php Intermediate

Arrays: array_slice()

• array_slice — Extract a slice of the array

$arr = array("Kabul", "Balkh", "Herat", "Qandahar");$new_arr = array_slice($arr,1,2);print_r($new_arr);> Array ( [0] => Balkh [1] => Herat )

Page 6: Php Intermediate

Arrays: array_reverse()

• array_reverse — Return an array with elements in reverse order

$arr = array("Kabul", "Balkh", "Herat", "Qandahar");$new_arr = array_reverse($arr);print_r($new_arr);> Array ( [0] => Qandahar [1] => Herat [2] => Balkh [3] => Kabul )

Page 7: Php Intermediate

Arrays: array_keys()

• array_keys — Return all the keys or a subset of the keys of an array

$arr = array("Kabul", "Balkh", "Herat", "Qandahar");$new_arr = array_keys($arr);print_r($new_arr);> Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )

Page 8: Php Intermediate

Arrays: in_array()

• in_array — Checks if a value exists in an array

$arr = array("Kabul", "Balkh", "Herat", "Qandahar");

if(in_array("Kabul",$arr)){ echo "Found";}else{ echo "Not Found";}> Found

Page 9: Php Intermediate

Arrays: is_array()

• is_array — Finds whether a variable is an array

$arr = array("Kabul", "Balkh", "Herat", "Qandahar");

if(is_array($arr)){ echo "YES";}else{ echo "NO";}> YES

Page 10: Php Intermediate

Arrays: shuffle()

• shuffle — Shuffle an array

$arr = array("Kabul", "Balkh", "Herat", "Qandahar");shuffle($arr);print_r($arr);

> Array ( [0] => Balkh [1] => Herat [2] => Qandahar [3] => Kabul ) > Array ( [0] => Herat [1] => Qandahar [2] => Kabul [3] => Balkh ) > ….

Page 11: Php Intermediate

Arrays: array_unique()

• array_unique — Removes duplicate values from an array

$arr = array("Kabul", "Balkh", "Herat", "Qandahar","Kabul");$new_arr = array_unique($arr);print_r($new_arr);> Array ( [0] => Kabul [1] => Balkh [2] => Herat [3] => Qandahar )

Page 12: Php Intermediate

LOOPS

• In computer programming, a loop is a sequence of instructions that is continually repeated until a certain condition is reached.

• An infinite loop is one that lacks a functioning exit routine . The result is that the loop repeats continually until the operating system senses it and terminates the program with an error or until some other event occurs (such as having the program automatically terminate after a certain duration of time).

Page 13: Php Intermediate

LOOPS

• for – Syntax

• foreach - Syntax

for (init; condition; increment){

code to be executed;}

foreach ($array as $value){

code to be executed;}

Page 14: Php Intermediate

LOOPS

• while – Syntax

• do-while – Syntax

while (condition){

code to be executed;}

do{

code to be executed;}while (condition);

Page 15: Php Intermediate

PHP Functions

• The real power of PHP comes from its functions.

• In PHP, there are more than 700 built-in functions.

• A function will be executed by a call to the function.

• You may call a function from anywhere within a page.

Page 16: Php Intermediate

PHP Functions: Syntax

• PHP function guidelines:– Give the function a name that reflects what the

function does– The function name can start with a letter or

underscore (not a number)

function functionName(){

code to be executed;}

Page 17: Php Intermediate

PHP Functions: Sample<html><body>

<?phpfunction writeName(){echo “Ahmad Mohammad Salih";}

echo "My name is ";writeName();?>

</body></html>

Page 18: Php Intermediate

PHP Functions - Adding parameters

• To add more functionality to a function, we can add parameters. A parameter is just like a variable.

• Parameters are specified after the function name, inside the parentheses.

Page 19: Php Intermediate

PHP Functions – Sample 2<html><body>

<?phpfunction writeName($fname){echo $fname . " Ahmadi.<br>";}

echo "My name is ";writeName(“Ahmad");echo "My sister's name is ";writeName(“Saliha");echo "My brother's name is ";writeName(“Mohammad");?>

</body></html>

Page 20: Php Intermediate

PHP Functions - Return values

• To let a function return a value, use the return statement.

<html><body><?phpfunction add($x,$y){$total=$x+$y;return $total;}echo "1 + 16 = " . add(1,16);?></body></html>

Page 21: Php Intermediate

POST & GET

• GET requests can be cached• GET requests remain in the browser history• GET requests can be bookmarked• GET requests should never be used when dealing

with sensitive data• GET requests have length restrictions– 1,024 characters is a safe upper limit

• GET requests should be used only to retrieve data

Page 22: Php Intermediate

POST & GET

• POST requests are never cached• POST requests do not remain in the browser

history• POST requests cannot be bookmarked• POST requests have no restrictions on data

length

Page 23: Php Intermediate

POST & GET

<form action=”” method=”get”>

<form action=”” method=”post”>

Page 24: Php Intermediate

Difference between PHP4 & PHP5

• PHP4 was powered by Zend Engine 1.0, while PHP5 was powered by Zend Engine II.

• PHP4 is more of a procedure language while PHP5 is object oriented.

• In PHP5 one can declare a class as Abstract.• PHP5 incorporates static methods and

properties.• PHP5 introduces a special function called

__autoload()

Page 25: Php Intermediate

Difference between PHP4 & PHP5

• In PHP5, there are 3 levels of visibilities: Public, private and protected.

• PHP5 introduced exceptions.• In PHP4, everything was passed by value,

including objects. Whereas in PHP5, all objects are passed by reference.

• PHP5 introduces interfaces. All the methods defined in an interface must be public.

• PHP5 introduces new functions.

Page 26: Php Intermediate

Difference between PHP4 & PHP5

• PHP5 introduces some new reserved keywords.• PHP5 includes additional OOP concepts than

php4, like access specifiers , inheritance etc.• PHP5 includes reduced consumption of RAM.• PHP5 introduces increased security against

exploitation of vulnerabilities in PHP scripts.• PHP5 introduces easier programming through

new functions and extensions.

Page 27: Php Intermediate

Difference between PHP4 & PHP5

• PHP5 introduces a brand new built-in SOAP extension for interoperability with Web Services.

• PHP5 introduces a new SimpleXML extension for easily accessing and manipulating XML as PHP objects.

Page 28: Php Intermediate

Exercise!

• Write a program which reverse the order of the given string

• Write a function which returns the sum of all elements in a given integer array.

• Write a program which returns the middle element of a given array

• Write a program which find the duplicates of a given array and return both the duplicate values and the new duplicate-free array.

Page 29: Php Intermediate

Exercise!

• Find the biggest item of an integer array (Without using any built-in function e.g. max())

• Write a program that check if a given program is palindrome or not

• Write a function which takes an array (key=>value formatted) as a parameter and outputs the keys and values in separate columns of a table.

Key value

Page 30: Php Intermediate

Exercise!

• A factorial of any given integer, n, is the product of all positive integers between 1 and n inclu-sive. So the factorial of 4 is 1 × 2 × 3 × 4 = 24, and the factorial of 5 is 1 × 2 × 3 × 4 × 5 = 120. This can be expressed recursively as follows: – If n == 0, return 1. (This is the base case) – If n > 0, compute the factorial of n–1, multiply it by n,

and return the resultWrite a PHP script that uses a recursive function to display the factorials of a given number.

Page 31: Php Intermediate

QUESTIONS?