PHP Introduction


   Mohamed Ashraf
PHP Hypertext Preprocessor

   No joke, that’s what it stands for
   Now very widely used for websites
   Only three ye...
Simple PHP

   PHP is meant to be invoked inline with
    content
   Page “escapes” into and out of a regular html
    d...
A quick example

   <html>
    <head>Test page</head>
    <body>
    The time is now
    <?php
        echo date();
     ...
A quick example

   <html>
    <head>Test page</head>
    <body>
    The time is now
    <?php              here we “jump...
Another example
 <?php
      include “utilities.php”;
 ?>
 <html>
 <head>Test page</head>
 <body>
 <?php
      if ($utils-...
Another example – harder to read
    <?php
            include “utilities.php”;
    ?>
    <html>
    <head>Test page</hea...
More PHP language details

   Variables are implicitly typed
       This is good
       This is bad
   Variables start...
Defined variable example
   foo.html:
    <html><head></head><body>
    <form submit=“getFoo.php”>
    Enter your name:
 ...
Function list examples

   http://www.php.net/manual/en/function.strlen.
    php
       All string functions
       Som...
Classes

   OOP
       Class structures will be defined, helping
        integration with other apps and work together
 ...
Class example
class Cart
{
   var $items; // Items in our shopping cart
   // Add $num articles of $artnr to the cart
   f...
Inheritance example
 Class ParentObject {
   var $value;
  function ParentObject() {
     $this->value = 42;
  }
 }
 class...
Back to being hacky…

   “->” is NOT the same thing as it is in c++
       No pointers in PHP
       ONLY a member oper...
Variable variables and classes example

 class a {
    var $b;
 }

 $object = new a;
 $object->b = "hello";
 $member_name ...
Resources

   http://www.php.net
   http://www.w3schools.com
Upcoming SlideShare
Loading in …5
×

Php

765 views
723 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
765
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Php

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

×