Php examples

PHP is a simple, yet very powerful server-side scripting language. You write HTML pages with a "php" extension, sprinkle little bits of PHP code in between "<?php" and "?>" and magic happens.

  1. Simple include:

    To include the contents of one file into another:

    <?php include "myfile.php"; ?>
    
  2. The "echo" command - how to write text to the browser

    <p><?php echo "Hello World"; ?></p> 
    
  3. Comments

    All text between "//" or "#" and the end of the line is considered a comment. The text between "/*" and "*/" are also comments and can span multiple lines.

    <?php 
    // I'm a comment
    echo "Hello World"; #so am i
    /* this is
    also
    a comment */
    ?>
    
  4. How to set variables

    Variable names consist of only letters, numbers, and underscores and must not start with a number (it'd be a wee bit confusing to the parser).

    <?php $majorSection="Contact Us"; $minorSection="Overview"; ?>
    
  5. How to write variables to the browser

    <?php 
    $name = "Rachel";
    echo "Hello $name"; 
    ?>
    
  6. How to join text together

    Using the "." character you can concatenate strings and variables

    <?php
    echo "Hello" . " Sarah";
    ?>
    
  7. Types

    PHP has four types: Boolean, Integer, double, and string. The gettype() method will tell you the type.

    $cost = 12.95;
    $title = "Gates of Fire";
    $inStock = true;
    $numberOfCopies = 12;
    
    echo "type of \$cost is " . gettype($cost); #prints: type of $cost is double
    echo "
    type of \$title is " . gettype($title); #prints: type of $title is string
  8. Converting types

    You can convert between types with the "settype()" method. Interestingly, if PHP is converting a string to an integer it will convert as much as it can but stop when it hits any non-numeric character.

    $cost = "11 dollars";
    settype($cost, "integer");
    echo $cost # prints: 11
    
  9. Variable variables

    Sometimes you want to know the value of the value of a variable. PHP has got you covered. PHP gets the value of a variable and then gets the value of that variable when using ${$var}} syntax.

    $Lassie = "good dog";
    $dog = "Lassie";
    echo "${$dog}"  #prints:  good dog
    
  10. Flow of Control
    1. If

      PHP has standard "if" statement syntax:

      <?php 
      $temperature = 101.4;
      if($temperature > 80.0) {
        echo "It's hot!";
      } else if($temperature > 68.0) {
        echo "It's fine!";
      } else {
        echo "It's cold!";
      }
      ?>
      
      
    2. Looping

      PHP supports three looping types - while, do, and for. The "break" statement can be used to exit a loop, and "continue" skips to the end of the loop and go to the top of the loop again.

      <?php 
      $number = 1;
      while ($number <= 10) {
         $square = $number * $number;
         echo "<br />the square of $number is $square";
         $number++;
      }
      ?>
      <?php 
      $number = 1;
      do {
         $square = $number * $number;
         echo "<br />the square of $number is $square";
         $number++;
      } while ($number <= 10);
      
      ?>
      <?php 
      for($number = 1; $number <= 10; $number++) {
         $square = $number * $number;
         echo "<br />the square of $number is $square";
      }
      ?>
      
  11. How to define a method and invoke it from a page

    
    <?php
    function writeSubLink($current,$minor,$target) {
    echo "\n<li class=\"leftpane\">";
    if($current == $minor) {
        echo "<span class=\"navlinks_sub\">$current</span>";
    } else {
        echo "<a href=\"$target\" class=\"navlinks_sub\">$current</a>";
    }
    echo "</li>";
    }
    
    
    if($majorSection == "Our Services"){
    echo "<ul>";
    writeSubLink("Options Counseling",$minorSection,"counseling.php");
    writeSubLink("Support Services",$minorSection,"services.php");
    echo "</ul>";
    }
    ?>
    
    
  12. How to access name value parameters on the url

    You can use "$_GET[]". For example if the url is "http://www.fincher.org/news/photo.php?pic=2007/originals/2007-08-30-2151-IMG_5649.JPG". Then the following will pluck the variable "pic" from the url and replace its value as the src for the image.

    <img src="
    <?php echo $_GET['pic']; ?>
    " />
    
    
  13. How to send mail

    <?php 
    echo "<br />" . mail("bill.gates@microsoft.com","Vista feedback","Vista so totally rocks!  Thanks.");
    ?>
    
  14. printf

    For formatting text, PHP has a function called 'printf()' like C.

    printf("
    The temperature is %d in %s",89.2,"Austin, TX"); printf("
    The gas price today is %.3f ", rand(300,600)/100+0.009);
  15. Web Sites for more info:

    Visit www.php.net.