Your SlideShare is downloading. ×
0
Php
Php
Php
Php
Php
Php
Php
Php
Php
Php
Php
Php
Php
Php
Php
Php
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Php

528

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
528
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. PHP Introduction Mohamed Ashraf
  • 2. PHP Hypertext Preprocessor <ul><li>No joke, that’s what it stands for </li></ul><ul><li>Now very widely used for websites </li></ul><ul><li>Only three years ago it was considered a risky alternative for development </li></ul><ul><li>Has a lot of community support </li></ul>
  • 3. Simple PHP <ul><li>PHP is meant to be invoked inline with content </li></ul><ul><li>Page “escapes” into and out of a regular html document </li></ul><ul><li>File extension is .php (was .php3 for version 3)‏ </li></ul><ul><li>Initial use was control flow and simple scripting </li></ul>
  • 4. A quick example <ul><li><html> </li></ul><ul><li><head>Test page</head> </li></ul><ul><li><body> </li></ul><ul><li>The time is now </li></ul><ul><li><?php </li></ul><ul><li>echo date(); </li></ul><ul><li>?> </li></ul><ul><li><hr> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  • 5. A quick example <ul><li><html> </li></ul><ul><li><head>Test page</head> </li></ul><ul><li><body> </li></ul><ul><li>The time is now </li></ul><ul><li><?php here we “jump into” php </li></ul><ul><li>echo date(); </li></ul><ul><li>?> here we “jump” back out </li></ul><ul><li><hr> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  • 6. Another example <ul><li><?php </li></ul><ul><li>include “utilities.php”; </li></ul><ul><li>?> </li></ul><ul><li><html> </li></ul><ul><li><head>Test page</head> </li></ul><ul><li><body> </li></ul><ul><li><?php </li></ul><ul><li>if ($utils->isFriendly()) { </li></ul><ul><li>echo “The time is now “ . date(); </li></ul><ul><li>} else { </li></ul><ul><li>echo “I will not give you the time of day”; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li><hr> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  • 7. Another example – harder to read <ul><li><?php </li></ul><ul><li>include “utilities.php”; </li></ul><ul><li>?> </li></ul><ul><li><html> </li></ul><ul><li><head>Test page</head> </li></ul><ul><li><body> </li></ul><ul><li><?php </li></ul><ul><li>if ($utils->isFriendly()) { </li></ul><ul><li>echo “The time is now “ . date(); </li></ul><ul><li>} else { </li></ul><ul><li>?> </li></ul><ul><li><i>I will not give you the time of day!</i> </li></ul><ul><li><?php </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li><hr> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul>
  • 8. More PHP language details <ul><li>Variables are implicitly typed </li></ul><ul><ul><li>This is good </li></ul></ul><ul><ul><li>This is bad </li></ul></ul><ul><li>Variables start with $ </li></ul><ul><li>All get/post variables automatically defined </li></ul><ul><ul><li>With most default server settings </li></ul></ul><ul><ul><li>With an inline directive if need be </li></ul></ul>
  • 9. Defined variable example <ul><li>foo.html: </li></ul><ul><li><html><head></head><body> </li></ul><ul><li><form submit=“getFoo.php”> </li></ul><ul><li>Enter your name: </li></ul><ul><li><input type=“text” name=“username”> </li></ul><ul><li><input type=“submit”> </li></ul><ul><li></body></html> </li></ul><ul><li>getFoo.php: </li></ul><ul><li><html><head></head></body> </li></ul><ul><li>Your name is </li></ul><ul><li><?php </li></ul><ul><li>if (!strcmp($username)) { </li></ul><ul><li>echo $username; </li></ul><ul><li>} else { </li></ul><ul><li>echo “not given”; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li>!<br> </li></ul><ul><li></body></html> </li></ul>
  • 10. Function list examples <ul><li>http://www.php.net/manual/en/function.strlen.php </li></ul><ul><ul><li>All string functions </li></ul></ul><ul><ul><li>Some are “obvious” to c programmers </li></ul></ul><ul><ul><ul><li>strlen, printf, fprintf, strpos </li></ul></ul></ul><ul><ul><li>Some are web tailored </li></ul></ul><ul><ul><ul><li>htmlentities, htmlspecialchars </li></ul></ul></ul><ul><ul><li>Others are new (hacky)‏ </li></ul></ul><ul><ul><ul><li>addcslashes, explode, soundex, quotemeta, … </li></ul></ul></ul>
  • 11. Classes <ul><li>OOP </li></ul><ul><ul><li>Class structures will be defined, helping integration with other apps and work together </li></ul></ul><ul><ul><li>APIs followed by implementation </li></ul></ul><ul><li>Inheritance </li></ul><ul><li>Object serialization </li></ul><ul><ul><li>“ Magic functions” </li></ul></ul>
  • 12. Class example <ul><li>class Cart </li></ul><ul><li>{ </li></ul><ul><li>var $items; // Items in our shopping cart </li></ul><ul><li>// Add $num articles of $artnr to the cart </li></ul><ul><li>function add_item ($artnr, $num)‏ </li></ul><ul><li>{ </li></ul><ul><li>$this->items[$artnr] += $num; </li></ul><ul><li>} </li></ul><ul><li>// Take $num articles of $artnr out of the cart </li></ul><ul><li>function remove_item ($artnr, $num)‏ </li></ul><ul><li>{ </li></ul><ul><li>if ($this->items[$artnr] > $num) { </li></ul><ul><li>$this->items[$artnr] -= $num; </li></ul><ul><li>return true; </li></ul><ul><li>} else { </li></ul><ul><li>return false; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  • 13. Inheritance example <ul><li>Class ParentObject {    var $value;   function ParentObject() {        $this->value = 42;   } } class MemberObject extends ParentObject {    var $string;    function MemberObject() {       $this->string = &quot;This is a test string.&quot;; $this->ParentObject();   } } class ObjTest {    var $ObjPointer;   function ObjTest() {        $tmp = new MemberObject; $this->ObjPointer = $tmp;   } } $object = new ObjTest; echo &quot;String Contents: &quot; . $object->ObjPointer->string . &quot; &quot;; echo &quot;Value  Contents: &quot; . $object->ObjPointer->value . &quot; &quot;; </li></ul>
  • 14. Back to being hacky… <ul><li>“ ->” is NOT the same thing as it is in c++ </li></ul><ul><ul><li>No pointers in PHP </li></ul></ul><ul><ul><li>ONLY a member operator </li></ul></ul><ul><li>Oh, you wanted pointers? </li></ul><ul><ul><li>Variable variables </li></ul></ul><ul><ul><ul><li>Yeah, you heard right </li></ul></ul></ul><ul><ul><ul><ul><li>Don’t get me started… </li></ul></ul></ul></ul>
  • 15. Variable variables and classes example <ul><li>class a { var $b; } $object = new a; $object->b = &quot;hello&quot;; $member_name = 'b'; echo $object->$member_name; $object->$member_name = &quot; world&quot;; echo $object->$member_name; </li></ul>
  • 16. Resources <ul><li>http://www.php.net </li></ul><ul><li>http://www.w3schools.com </li></ul>

×