Upcoming SlideShare
×

# Php Intermediate

1,318 views

Published on

Published in: Technology
1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
1,318
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
38
0
Likes
1
Embeds 0
No embeds

No notes for slide

### Php Intermediate

1. 1. PHP IntermediateJamshid HashimiTrainer, Cresco Solutionhttp://www.jamshidhashimi.comjamshid@netlinks.af@jamshidhashimiajamshidhashimiAfghanistan WorkforceDevelopment Program
2. 2. Agenda• Arrays• Loop– For statement– Foreach statement– While statement– Do While statement• PHP Functions• Get & Post Variable• Difference between PHP 4 & PHP 5• Exercise!
3. 3. Arrays• An array is a special variable, which can holdmore 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"));
4. 4. Arrays: count()• count — Count all elements in an array, orsomething in an object\$arr = array("Kabul", "Balkh", "Herat","Qandahar");echo count(\$arr);> 4
5. 5. 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 )
6. 6. Arrays: array_reverse()• array_reverse — Return an array withelements 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 )
7. 7. Arrays: array_keys()• array_keys — Return all the keys or a subset ofthe 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 )
8. 8. 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
9. 9. 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
10. 10. 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 )> ….
11. 11. Arrays: array_unique()• array_unique — Removes duplicate valuesfrom 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 )
12. 12. LOOPS• In computer programming, a loop is a sequenceof instructions that is continually repeated until acertain condition is reached.• An infinite loop is one that lacks a functioning exitroutine . The result is that the loop repeatscontinually until the operating system senses itand terminates the program with an error or untilsome other event occurs (such as having theprogram automatically terminate after a certainduration of time).
13. 13. LOOPS• for – Syntax• foreach - Syntaxfor (init; condition; increment){code to be executed;}foreach (\$array as \$value){code to be executed;}
14. 14. LOOPS• while – Syntax• do-while – Syntaxwhile (condition){code to be executed;}do{code to be executed;}while (condition);
15. 15. PHP Functions• The real power of PHP comes from itsfunctions.• In PHP, there are more than 700 built-infunctions.• A function will be executed by a call to thefunction.• You may call a function from anywhere withina page.
16. 16. PHP Functions: Syntax• PHP function guidelines:– Give the function a name that reflects what thefunction does– The function name can start with a letter orunderscore (not a number)function functionName(){code to be executed;}
17. 17. PHP Functions: Sample<html><body><?phpfunction writeName(){echo “Ahmad Mohammad Salih";}echo "My name is ";writeName();?></body></html>
18. 18. PHP Functions - Adding parameters• To add more functionality to a function, wecan add parameters. A parameter is just like avariable.• Parameters are specified after the functionname, inside the parentheses.
19. 19. PHP Functions – Sample 2<html><body><?phpfunction writeName(\$fname){echo \$fname . " Ahmadi.<br>";}echo "My name is ";writeName(“Ahmad");echo "My sisters name is ";writeName(“Saliha");echo "My brothers name is ";writeName(“Mohammad");?></body></html>
20. 20. PHP Functions - Return values• To let a function return a value, use the returnstatement.<html><body><?phpfunction add(\$x,\$y){\$total=\$x+\$y;return \$total;}echo "1 + 16 = " . add(1,16);?></body></html>
21. 21. 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 dealingwith sensitive data• GET requests have length restrictions– 1,024 characters is a safe upper limit• GET requests should be used only to retrieve data
22. 22. POST & GET• POST requests are never cached• POST requests do not remain in the browserhistory• POST requests cannot be bookmarked• POST requests have no restrictions on datalength
23. 23. POST & GET<form action=”” method=”get”><form action=”” method=”post”>
24. 24. Difference between PHP4 & PHP5• PHP4 was powered by Zend Engine 1.0, whilePHP5 was powered by Zend Engine II.• PHP4 is more of a procedure language whilePHP5 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()
25. 25. 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 objectsare passed by reference.• PHP5 introduces interfaces. All the methodsdefined in an interface must be public.• PHP5 introduces new functions.
26. 26. Difference between PHP4 & PHP5• PHP5 introduces some new reservedkeywords.• PHP5 includes additional OOP concepts thanphp4, like access specifiers , inheritance etc.• PHP5 includes reduced consumption of RAM.• PHP5 introduces increased security againstexploitation of vulnerabilities in PHP scripts.• PHP5 introduces easier programming throughnew functions and extensions.
27. 27. Difference between PHP4 & PHP5• PHP5 introduces a brand new built-in SOAPextension for interoperability with WebServices.• PHP5 introduces a new SimpleXML extensionfor easily accessing and manipulating XML asPHP objects.
28. 28. Exercise!• Write a program which reverse the order ofthe given string• Write a function which returns the sum of allelements in a given integer array.• Write a program which returns the middleelement of a given array• Write a program which find the duplicates of agiven array and return both the duplicatevalues and the new duplicate-free array.
29. 29. 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 programis palindrome or not• Write a function which takes an array(key=>value formatted) as a parameter andoutputs the keys and values in separatecolumns of a table.Key value
30. 30. Exercise!• A factorial of any given integer, n, is the productof all positive integers between 1 and n inclu-sive.So the factorial of 4 is 1 × 2 × 3 × 4 = 24, and thefactorial of 5 is 1 × 2 × 3 × 4 × 5 = 120. This can beexpressed 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 todisplay the factorials of a given number.
31. 31. QUESTIONS?