Published on

php workshop

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. PHP Workshop Day1Prepared by :Ahmed Mahmoud Saad
  2. 2. Agenda 1. Introduction 2. Install 2. Getting started 3. Example
  3. 3. 3Introduction to PHP PHP: Hypertext Preprocessor Originally called “Personal Home Page Tools” Popular server-side scripting technology Open-source  Anyone may view, modify and redistribute source code  Supported freely by community Platform independent G. Ochoa, G64HLL
  4. 4. Brief History of PHP As of August 2004, PHP is used on 16,946,328 Domains, 1,348,793 IP Addresses This is roughly 32% of all domains on the web.
  5. 5. Why is PHP used?3. Cost Benefits PHP is free. Open source code means that the entire PHP community will contribute towards bug fixes. There are several add-on technologies (libraries) for PHP that are also free. PHP Software Free Platform Free (Linux) Development Tools Free PHP Coder, jEdit
  6. 6. Getting Started How to escape from HTML and enter PHP mode .1 PHP parses a file by looking for one of the special tags that  tells it to start interpreting the text as PHP code. The parser then executes all of the code it finds until it runs into a PHP closing tag. HTML PHP CODE HTML <?php echo “Hello World”; ?>Starting tag Ending tag Notes<?php ?> Preferred method as it allows the use of PHP with XHTML<? ?> Not recommended. Easier to type, but has to be enabled and may conflict with XML<script language="php"> ?> Always available, best if used when FrontPage is the HTML editor<% %> Not recommended. ASP tags support was added in 3.0.4
  7. 7. 7PHP reserved wordsKeywords Reserved for language features if…elseif…elsePHP keywordsand do for include require truebreak else foreach list return varcase elseif function new static virtualclass extends global not switch xorcontinue false if or this whiledefault G. Ochoa, G64HLL
  8. 8. 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"2 "">34 <!-- Fig. 26.1: first.php -->5 <!-- Our first PHP script -->6 Scripting delimiters7 <?php8 $name = "LunaTic"; // declaration9 ?> Declare variable $name1011 <html xmlns = "">12 <head>13 <title>A simple PHP document</title>Single-line comment14 </head>1516 <body style = "font-size: 2em">17 <p>18 <strong>1920 <!-- print variable name’s value -->21 Welcome to PHP, <?php print( "$name" ); ?>!22 </strong>23 </p>24 </body> Function print outputs the value of variable $name25 </html>
  9. 9. Install If your server supports php you don‟t need to install php . You need only to create [*.php] files in the web directory of the server . Most of servers nowadays, support php parsing.
  10. 10. Install We will use Wamp Server Install Wamp x64 or x32 . Install it in the default directory “c:Wamp” Within the install .. Installer will ask you the browser you want .. Choose it if you want or click open to choose the default action (as the image in the next slide). Run WampServer.exe from C:Wamp
  11. 11. Hello World Make a new folder in the directory “C:WampWWW” named “my first project” Create a new file “Hello world.php” and open it using any text editor (notepad++ recommended) Write this line then save:<?php echo “Hello World” ?>
  12. 12. Server Parses the file Go to local host (using wamp tray icon)
  13. 13. Variables in php Variables are used for storing a values, like text strings, numbers or arrays. A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with All variables in PHP start with a $ sign symbol. $myText= “the text” ; $myNumber= 6 ; $myDate= date("d/m/Y"); echo $myText."<br>".$myNumber."<br>".$myDate."<br>"."Hello!!";
  14. 14. Operations on variablesStrings: Strlen(“mystring”):returns the length of a string strpos(“hello world”,”hello”):returns the position of a substring in a string. Use „.‟ to concatenate 2 or more strings :echo ”hello ”. “world”; $mystring[0]:returns first character
  15. 15. Operations on variablesDate: Date(“m”):returns month number(“d”day,”y”year in 2 digits (98 stands for 1998)) Date(“M”):returns month name („‟D”day name Ynormal 4 digits) Formatting date :Date(“d/m/Y”) , Date(“d-M- Y”) , Date(“D.M.Y”),… $tomorrow=mktime(0,0,0,date("m"),date("d")+ 1,date("Y")); echo “tomorrow is ”. Date(“d/M/Y”);
  16. 16. Operations on variables Arithmetic operation on numbers
  17. 17. Operations on variables Assignment operation
  18. 18. Operations on variables Comparing operation Comparing operation on numbers
  19. 19. ArraysWhat is an array? When working with PHP, sooner or later, you might want to create many similar variables. Instead of having many similar variables, you can store the data as elements in an array. Numeric array - An array with a numeric ID key Associative array - An array where each ID key is associated with a value Multidimensional array - An array containing one or more arrays
  20. 20. Numeric arrays $names = array("Peter","Quagmire","Joe");assign the ID key manually: $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe";Example echo “first name is " . $names[0];Output: first name is Peter
  21. 21. Associative array $ages array("Peter"=>32,"Quagmire"=>30,"Joe"=>34);assign the ID key manually: $ages[Peter] = "32"; $ages[Quagmire] = "30"; $ages[Joe] = "34"Example:echo "Peter is " . $ages[Peter] . " years old.";Output: Peter is 32 " years old.
  22. 22. Multidimensional array $families = array("Griffin"=>array("Peter","Lois","Megan"),"Quagmire"=>array("Glenn"),"Brown"=>array("Cleveland", "Loretta","Junior"));assign the ID key manually: $ families["Griffin“][1]="Lois"; $ families[" Quagmire“][0]=" Glenn“; $ families[" Brown“][2]=" Junior“;Example: echo "Is " . $families[Griffin][2] ." a part of the Griffin family?";
  23. 23. If statementif (condition1) code to be executed if condition1 is true;Elseif (condition2) code to be executed if condition2 is true;..else code to be executed if neither condition1 nor condition2 is true;Example:<?php$d=date("D");if ($d=="Fri") echo "Have a nice weekend!";elseif ($d=="Sun") echo "Have a nice Sunday!";else echo "Have a nice day!";?>Output if today is Friday:Have a nice weekend!Output if today is Sunday:Have a nice Sunday!Else Output is: Have a nice day!
  24. 24. While loop while (condition) code to be executed;Example:<?php$i=1;while($i<=5){ echo "The number is " . $i . “ , "; $i++;}?>Output: 1 , 2 , 3 , 4 , 5 ,
  25. 25. For loop for (init; cond; incr){ code to be executed;}Example:<?phpfor ($i=1; $i<=5; $i++){ echo "Hello World!<br />";}?>//Prints Hello world 5 timesPrinting array:<?phpfor ($i=0; $i<=5; $i++){ echo arr[i] . "<br />";}?>//Prints first 5 items in the array ‘arr’
  26. 26. functions A function is a block of code that can be executed whenever we need it. Creating PHP functions: All functions start with the word "function()" Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number) after "{" - The function code starts after "}" - The function is finished
  27. 27. Function Examples Void function (no return value):function printHello(){echo “Hello”;}printHello();Output: Hellofunction printName($name){echo “Hello ”.$name.”!!”;}printName(“Hamada”);Output: Hello Hamada!!
  28. 28. Function Examples Function returns value:function add($first,$second){$sum= $first+$second ;return $sum;}$K=add (1,2);Output: K = 3function concatenate($firstString,$secondString){$newString= $firstString.$ secondString;return $newString;}$S = concatenate(“Hello ”,”World”);Output: S =3
  29. 29. $_GET used to retrieve information from forms, like user input. The $_GET variable is used to collect values from a form with method="get". Information sent from a form with the GET method is visible to everyone (it will be displayed in the browsers address bar) and it has limits on the amount of information to send (max. 100 characters).
  30. 30. $_GET HTML file (Welcome.htm)<form action="welcome.php" method="get">Name: <input type="text" name="name" />Age: <input type="text" name="age" /><input type="submit" /></form> PHP file (Welcome.php)Welcome <?php echo $_GET["name"]; ?>.<br />You are <?php echo $_GET["age"]; ?> years old!
  31. 31. $_POST used to retrieve information from forms, like user input. The $_POST variable is used to collect values from a form with method=“post". Information sent from a form with the POST method is invisible to everyone.
  32. 32. Cookies What is a Cookie? A cookie is often used to identify a user. A cookie is a small file that the server embeds on the users computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.How to Create a Cookie? The setcookie() function is used to set a cookie.Note: The setcookie() function must appear BEFORE the <html> tag.Syntax:setcookie(name, value, expire, path, domain);
  33. 33. Cookies Setting cookie:<?phpsetcookie("user", "Alex Porter", time()+3600);?><html>..Getting cookie:<?php// Print a cookieecho $_COOKIE["user"];// A way to view all cookiesprint_r($_COOKIE);?> Checking if Cookies is set :if( isset($_COOKIE["user"] ) Deleting cookie: setcookie("user", "", time()-3600);
  34. 34. $_REQUEST The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.
  35. 35. Example (1):reversing a stringReverse.htm: <form action="Reverse.php" method="get"> the word: <input type="text" name=" TheWord" /> <input type="submit" value = "reverse"/> </form>Reverse.php The word Reversed is: <?php $myWord = $_GET["TheWord"]; $newWord=""; for ($i = strlen($myWord)-1 ;$i >= 0 ; $i--){ $newWord .= $myWord[$i]; } echo $newWord ; ?>
  36. 36. Example (1):reversing a string(inside JavaScript) Reverse.htm: <form action="Reverse.php" method="get"> the word: <input type="text" name=" TheWord" /> <input type="submit" value = "reverse"/> </form> Reverse.php <script type = " text/javascript"> window.alert("The word Reversed is: <?php $myWord = $_GET["TheWord"]; $newWord=""; for ($i = strlen($myWord)-1 ;$i >= 0 ; $i--){ $newWord .= $myWord[$i]; } echo $newWord ; ?>"); </script> ..ThanQ
  37. 37. todo Write a form that user enter his name and birthday and php code that receives data and verify it (month number <=12 and so on ) Calculate age (years ,days) Print all the leap years the user live in (year divisible by 4). Print a pyramid represents the user age in years Example:if user is 3 years old (after calculation)Output is:...…