Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to PHP


Published on

Published in: Education, Technology

Introduction to PHP

  1. 1. Introduction to PHP Jussi Pohjolainen TAMK University of Applied Sciences
  3. 3. Three-tiered Web Site: LAMP Client User-agent: Firefox Server Apache HTTP Server example request GET / HTTP/1.1 Host: User-Agent: Mozilla/5.0 (Mac..) ... response Database MySQL PHP
  4. 4. Java EE Architecture (x)html / xml Applet Client Application J2EE Application Server Web Container Servlets JSP JSPs Servlets EJB Container RMI/IIOP JNDI JTA JDBC JMS JavaMail JAF Session Beans Entity Beans Msg-Driven Beans RMI/IIOP JNDI JTA JDBC JMS JavaMai l JAF HTTP(S) JDBC JavaMail RMI IIOP JNDI JMS DB Java-Application CORBA Server Message Queue Directory Service Client Mail Server
  5. 5. Server Side Techniques <ul><li>Server side scripting requires installation on the server side </li></ul><ul><li>Typically client siis only xhtml and it unaware that the xhtml was produced by a server side script </li></ul><ul><li>Does not require any installations or add-ons on the client </li></ul>
  6. 6. Server Side Techniques <ul><li>PHP </li></ul><ul><li>Java EE: Servlet, JSP </li></ul><ul><li>.NET </li></ul><ul><li>CGI / Perl (Very old) </li></ul><ul><li>Ruby </li></ul><ul><li>… </li></ul>
  7. 7. Client Side Techniques <ul><li>Requires that the client supports the technique </li></ul><ul><li>JavaScript, Applet, Flash… </li></ul>
  8. 8. Web Application Frameworks <ul><li>A web application framework is a software framework that is designed to support the development of dynamic websites, Web applications and Web services. </li></ul><ul><li>Numerous frameworks available for many languages </li></ul>
  9. 9. Web App vs. Web Site? <ul><li>What’s the difference between Web App and Web Site? </li></ul><ul><li>Rich Internet Application?, AJAX?, Thin Client? </li></ul><ul><li>Full application running in your browser or just a web site? </li></ul>
  11. 11. Introduction to PHP <ul><li>PHP is a computer scripting language. </li></ul><ul><li>Originally designed for producing dynamic web pages </li></ul><ul><li>Appeared in 1995 </li></ul><ul><li>PHP Group is responsible for the language, no formal specification </li></ul><ul><li>Free software </li></ul><ul><li>Runs on most operating systems and platforms </li></ul><ul><li>URL: </li></ul>
  12. 12. Response
  13. 13. Introduction to PHP Syntax <ul><li>PHP has quite easy syntax, if you are familiar with any c-type language </li></ul><ul><li>It has all the same structures that you are familiar with other programming languages </li></ul><ul><li>PHP is designed to output to browser, but it is possible to do also CLI apps. </li></ul>
  14. 14. Example <ul><li><?php </li></ul><ul><li>print &quot;What is your name? &quot;; </li></ul><ul><li>$name = trim(fgets(STDIN)); </li></ul><ul><li>print &quot;Hello &quot; . $name; </li></ul><ul><li>?> </li></ul>
  15. 15. Variables <ul><li>Variables in PHP are represented by a dollar sign </li></ul><ul><li>PHP supports eight types: </li></ul><ul><ul><li>boolean, integer, float, double, array, object, resource and NULL </li></ul></ul>
  16. 16. Example ( <ul><li><?php </li></ul><ul><li>$a_bool = TRUE; // a boolean </li></ul><ul><li>$a_str = &quot;foo&quot;; // a string </li></ul><ul><li>$a_str2 = 'foo'; // a string </li></ul><ul><li>$an_int = 12; // an integer </li></ul><ul><li>echo gettype($a_bool); // prints out: boolean </li></ul><ul><li>echo gettype($a_str); // prints out: string </li></ul><ul><li>// If this is an integer, increment it by four </li></ul><ul><li>if (is_int($an_int)) { </li></ul><ul><li>$an_int += 4; </li></ul><ul><li>} </li></ul><ul><li>// If $bool is a string, print it out </li></ul><ul><li>// (does not print out anything) </li></ul><ul><li>if (is_string($a_bool)) { </li></ul><ul><li>echo &quot;String: $a_bool&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  17. 17. Naming Variables <ul><li>Case-sensitivity </li></ul><ul><li>Start with letter or _ </li></ul><ul><li>After that you can have numbers, letters and _ </li></ul><ul><ul><li>$var = 'Bob'; </li></ul></ul><ul><ul><li>$Var = 'Joe'; </li></ul></ul><ul><ul><li>print &quot;$var, $Var&quot;;      </li></ul></ul><ul><ul><li>$4site = 'not yet';    </li></ul></ul><ul><ul><li>$_4site = 'not yet';    </li></ul></ul>
  18. 18. Constants <ul><li>You cannot alter the value of constant after declaration </li></ul><ul><ul><li>define(CONSTANT, &quot;value&quot;); </li></ul></ul><ul><ul><li>print CONSTANT; </li></ul></ul>
  19. 19. Magic Constants <ul><li>PHP has lot of predefined variables </li></ul><ul><li>Also predefined constants: </li></ul><ul><ul><li>__LINE__ </li></ul></ul><ul><ul><li>__FILE__ </li></ul></ul><ul><ul><li>__FUNCTION__ </li></ul></ul><ul><ul><li>__CLASS__ </li></ul></ul><ul><ul><li>__METHOD__ </li></ul></ul><ul><li>See: </li></ul>
  20. 20. Scope <ul><li><?php </li></ul><ul><li>$a = &quot;Pekka&quot;; </li></ul><ul><li>print ”My name is &quot; . $a; </li></ul><ul><li>?> </li></ul>
  21. 21. Scope <ul><li><?php </li></ul><ul><li>$a = &quot;Pekka&quot;; </li></ul><ul><li>function Test() { </li></ul><ul><li>print $a; </li></ul><ul><li>} </li></ul><ul><li>print ”My name is ”; </li></ul><ul><li>Test(); </li></ul><ul><li>?> </li></ul>
  22. 22. Scope <ul><li><?php </li></ul><ul><li>$a = &quot;Pekka&quot;; </li></ul><ul><li>function Test() { </li></ul><ul><li>global $a; </li></ul><ul><li>print $a; </li></ul><ul><li>} </li></ul><ul><li>print ”My name is ”; </li></ul><ul><li>Test(); </li></ul><ul><li>?> </li></ul>
  23. 23. Control Structures <ul><li>If, else, elseif, switch </li></ul><ul><li>while, do-while, for </li></ul><ul><li>foreach </li></ul><ul><li>break, continue </li></ul>
  24. 24. PHP BASICS
  25. 25. Strings <ul><li>Single quoted: 'this is a $variable' </li></ul><ul><li>Double quoted: &quot;this is a $variable&quot; </li></ul><ul><li>Heredoc: </li></ul><ul><ul><li>$str = <<<EOD </li></ul></ul><ul><ul><li>Example of string </li></ul></ul><ul><ul><li>spanning multiple lines </li></ul></ul><ul><ul><li>using heredoc syntax. </li></ul></ul><ul><ul><li>EOD; </li></ul></ul>
  26. 26. Modifying the String <ul><li>$mj = &quot;moi&quot;; </li></ul><ul><li>print $mj[0]; </li></ul><ul><li>$mj[0] = 'x'; </li></ul><ul><li>print $mj; </li></ul><ul><li>$mj = $mj . &quot; hei&quot;; </li></ul><ul><li>print $mj; </li></ul><ul><li>$mj .= &quot; terse&quot;; </li></ul><ul><li>print $mj; </li></ul>
  27. 27. String functions <ul><li>A lot of functions… </li></ul><ul><ul><li> </li></ul></ul>
  28. 28. Statements <ul><li>Every statement ends with ; </li></ul><ul><ul><li>$a = 5; </li></ul></ul><ul><ul><li>$a = function(); </li></ul></ul><ul><ul><li>$a = ($b = 5); </li></ul></ul><ul><ul><li>$a++; ++$a; </li></ul></ul><ul><ul><li>$a += 3; </li></ul></ul>
  29. 29. Operators <ul><li>Arithmethic: +,-,*,% </li></ul><ul><li>Setting variable: = </li></ul><ul><li>Bit: &, |, ^, ~, <<, >> </li></ul><ul><li>Comparison: ==, ===, !=, !==, <, > <=, >= </li></ul>
  30. 30. Ternary Operator <ul><li>$variable = (1 < $x) ? 'value1' : 'value2'; </li></ul><ul><li>Equals </li></ul><ul><li>if(1 < $x) { </li></ul><ul><li>$variable = 'value1'; </li></ul><ul><li>} </li></ul><ul><li>else { </li></ul><ul><li>$variable = 'value1'; </li></ul><ul><li>} </li></ul>
  31. 31. Execution Operator <ul><li>Execute command in shell </li></ul><ul><ul><li>$result = `ls -al`; </li></ul></ul><ul><ul><li>print $result; </li></ul></ul><ul><li>Does the same than shell_exec() - function </li></ul>
  32. 32. Logical Operators <ul><li>$a and $b </li></ul><ul><li>$a or $b </li></ul><ul><li>$a xor $b </li></ul><ul><li>!$a; </li></ul><ul><li>$a && $b; </li></ul><ul><li>$a || $b; </li></ul>
  33. 33. String Operators <ul><li>Two operators for strings: '.' and '.=' </li></ul><ul><li>'.' – combining strings. </li></ul><ul><li>'.=' – appends string to the end. </li></ul><ul><li>Example: </li></ul><ul><li>$v= &quot;Hello&quot; . $b; </li></ul><ul><li>$v.= &quot;Hello&quot;; </li></ul>
  34. 34. Arrays <ul><li>See </li></ul><ul><ul><li> </li></ul></ul>
  36. 36. IF <ul><li><?php </li></ul><ul><li>if ($a > $b) { </li></ul><ul><li>   echo &quot;a is bigger than b&quot;; </li></ul><ul><li>} else { </li></ul><ul><li>   echo &quot;a is NOT bigger than b&quot;; </li></ul><ul><li>} </li></ul><ul><li>if ($a > $b) { </li></ul><ul><li>   echo &quot;a is bigger than b&quot;; </li></ul><ul><li>} elseif ($a == $b) { </li></ul><ul><li>   echo &quot;a is equal to b&quot;; </li></ul><ul><li>} else { </li></ul><ul><li>   echo &quot;a is smaller than b&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  37. 37. While and Do-While <ul><li><?php </li></ul><ul><li>$a=0; </li></ul><ul><li>while($a<10){ </li></ul><ul><li>print $a; $a++; </li></ul><ul><li>} </li></ul><ul><li>$i = 0; </li></ul><ul><li>do { </li></ul><ul><li>   print $i; </li></ul><ul><li>} while ($i > 0); </li></ul><ul><li>?> </li></ul>
  38. 38. For <ul><li>for ($i = 1; $i <= 10; $i++) { </li></ul><ul><li>   print $i; </li></ul><ul><li>} </li></ul>
  39. 39. Foreach <ul><li>$arr = array(1, 2, 3, 4); </li></ul><ul><li>foreach ($arr as $value) { </li></ul><ul><li>   echo $value; </li></ul><ul><li>} </li></ul>
  40. 40. Switch <ul><li>switch ($i) { </li></ul><ul><li>case 0: </li></ul><ul><li>   echo &quot;i equals 0&quot;; </li></ul><ul><li>   break; </li></ul><ul><li>case 1: </li></ul><ul><li>   echo &quot;i equals 1&quot;; </li></ul><ul><li>   break; </li></ul><ul><li>case 2: </li></ul><ul><li>   echo &quot;i equals 2&quot;; </li></ul><ul><li>   break; </li></ul><ul><li>} </li></ul>
  42. 42. Response
  43. 43. Example: spaghetti-way <ul><li><!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;;> </li></ul><ul><li><html xmlns=&quot;;> </li></ul><ul><li><head> </li></ul><ul><li><title>xhtml-doku</title> </li></ul><ul><li><meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /> </li></ul><ul><li></head> </li></ul><ul><li><body> </li></ul><ul><li><h1>Title</h1> </li></ul><ul><li><?php </li></ul><ul><li> print &quot;<p>Hello from PHP!</p>&quot;; </li></ul><ul><li>?> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  44. 44. Better ways <ul><li>Use functions </li></ul><ul><li>Use OO </li></ul>
  46. 46. PHP and User Input via Forms <ul><li>See </li></ul><ul><ul><li> </li></ul></ul>