1 / 25

PHP - 1h

PHP - 1h. How it works. Client requests document Server loads document in memory Server processes document with relevant module (PHP) Server sends XHTML document to client Client displays document. Hello World!. <html> <body> <?php echo 'Hello World';?> </body> </html>. Output 1.

Download Presentation

PHP - 1h

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP - 1h

  2. How it works • Client requests document • Server loads document in memory • Server processes document with relevant module (PHP) • Server sends XHTML document to client • Client displays document

  3. Hello World! <html> <body> <?php echo 'Hello World';?> </body> </html>

  4. Output 1 <html> <body> Hello World </body> </html>

  5. Output 2 Hello World

  6. PHP Syntax basics • PHP code embedded in <?php … ?> • Files containing PHP code must have a .php extension (or php3, etc) • Syntax very similar to C and Java • Comments as in C/Java: • /* comment */ • // comment • But also: # comment

  7. PHP Syntax basics • Syntax very similar to C and Java • Statements delimited by ; • Variables preceded by $, for example: • $x = 2; • $first_name = joe;

  8. Data types • Integer – for whole numbers • Float – for real numbers • String – for strings of characters • Boolean – for true or false values • Array – For collections of data • Object – For OO programming

  9. Strings <?php $first_name = ‘John’; $last_name = ‘Doe’; echo $first_name; ?> Output: John

  10. Strings <?php $first_name = ‘John’; $last_name = ‘Doe’; echo ‘$first_name’; ?> Output: $first_name

  11. Strings <?php $first_name = ‘John’; $last_name = ‘Doe’; echo “$first_name”; ?> Output: John

  12. Strings <?php $first_name = ‘John’; $last_name = ‘Doe’; echo "You said: \"my name is $first_name $last_name\"";?> Output: You said: “my name is John Doe”

  13. Strings <?php $first_name = ‘John’; $last_name = ‘Doe’; echo $first_name{0}; ?> Output: J

  14. Strings <?php $first_name = ‘John’; $last_name = ‘Doe’; echo substr('abcdef', 1, 4); ?> Output: bcde

  15. Strings <?php $first_name = ‘John’; $last_name = ‘Doe’; echo substr($first_name, 0, 6); ?> Output: ??? you tell me!!

  16. Integers and Floats <?php $a = 1234; $b = 12.02; ?> • Integer type: http://uk.php.net/manual/en/language.types.integer.php • Float type: http://uk.php.net/manual/en/language.types.float.php

  17. Boolean • Logical operators: • And: && • Or: ¦¦ • Not: ! • Boolean type: http://uk.php.net/manual/en/language.types.boolean.php

  18. Array

  19. Numerically indexed array <?php $names = array(‘Joe’, ‘John’, ‘Jack’, ‘Jim’ ); echo $names[1]; ?> Output: John

  20. if, else, elseif <?php $value = 24; if ($value < 20) echo “not enough!”; elseif ($value < 30) echo “reasonable.”; elseif ($value < 40) echo “ok!”; else echo “Too much!”; ?>

  21. switch <?php switch ($value){ case 24: echo "Correct!"; break; case 25: echo "Almost correct!"; break; default: echo "Too much!"; }?>

  22. for <?php for ( ; ; ) { } ?> ($i = 1 $i <= 10 $i++ echo $i; Output: 12345678910

  23. for <?php for ($i = 1; $i <= 10; $i++) { echo $i; } ?> Output: 12345678910

  24. foreach <?php $arr = array("one", "two", "three"); echo "Using a for loop:<br />\n"; for ($i = 0; $i < sizeof($arr); $i++) { echo "Value: $arr[$i]<br />\n"; } echo "Using a foreach loop:<br />\n"; foreach ($arr as $value) { echo "Value: $value<br />\n"; } ?>

  25. while <?php $i = 1;while ($i <= 10) {   echo $i i++;  } Output: 12345678910

More Related