Hosted By:
Lunarpages.com Web Hosting
Choose Language:
Program Structure
                   <html>
                   <head>
                       <title> Example</title>
                   </head>
                   <body>
                       <?php 
                           echo "Hi, I'm a PHP script!"; 
                           class Base
                           {
                               function takes($input)                                 
                                   for ($input = 1; $input <= 10; $input++) {
                                       echo $input;
                                   }
                               } 
                           }
                           $b = new Base();
                           $b->takes();
                       ?>
                   </body>
                   </html> 

                
Comments
                    // This is a one-line c++ style comment
                    
                    /*Multi
                      line Comment*/
                    
                    # This is a one-line shell-style comment

                
Data Types
                    boolean
                    integer
                    float
                    string
                    array
                    object
                    resource
                    NULL
                    mixed
                    number
                    callback

                
Arrays
                    
                    <?php
                    $arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
                    echo $arr["somearray"][6];   // print 5
                    echo $arr["somearray"][13];  // print 9
                    echo $arr["somearray"]["a"]; // print 42
                    ?> 

                    <?php
                    // This array is the same as ...
                    array(5 => 43, 32, 56, "b" => 12);
                    // ...this array
                    array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
                    ?> 

                    <?php
                    $arr = array(5 => 1, 12 => 2);
                    $arr[] = 56; // This is the same as $arr[13] = 56;
                    // at this point of the script
                    $arr["x"] = 42; // This adds a new element to
                    // the array with key "x"
                    unset($arr[5]); // This removes the element from the array
                    unset($arr); // This deletes the whole array
                    ?> 

                    //Recursive and multi-dimensional arrays
                    <?php
                    $fruits = array ( "fruits"  => array ( "a" => "orange","b" => "banana",
                                                           "c" => "apple"),
                                                           "numbers" => array ( 1,2,3,4,5,6),
                                                           "holes"   => array ("first",
                                                                                5 => "second",
                                                                                "third")
                                                         );
                    // Some examples to address values in the array above 
                    echo $fruits["holes"][5]; // prints "second"
                    echo $fruits["fruits"]["a"]; // prints "orange"
                    unset($fruits["holes"][0]); // remove "first"

                    // Create a new multi-dimensional array
                    $juices["apple"]["green"] = "good"; 
                    ?> 

                
Operators
                    //Arithmetic
                    * / % + -
                    
                    //Assignment
                    = += -= *= /= .= %= &= |= ^= <<= >>=

                    //Bitwise
                    & | ^ ~ << >>

                    //Comparison
                    < <= > >= <> == != === !==

                    //Increment/Decrement
                    ++ --

                    //Logical
                    && || ! and or xor

                    //Conditional operator
                    ?:

                
Decisions
                    <?php
                    if ($a > $b) {
                        echo "a is bigger than b";
                    } else {
                          echo "a is NOT bigger than b";
                    }
                    ?>

                    <?php
                    if ($a > $b) {
                        echo "a is bigger than b";
                    } elseif ($a == $b) {
                          echo "a is equal to b";
                    } else {
                          echo "a is smaller than b";
                    }
                    ?>  

                    //jump out of php tag
                    <?php
                    if ($a == 5): ?>
                        A is equal to 5
                    <?php endif; ?> 
                      
                    <?php
                    if ($a == 5):
                        echo "a equals 5";
                        echo "...";
                    elseif ($a == 6):
                        echo "a equals 6";
                        echo "!!!";
                    else
                        echo "a is neither 5 nor 6";
                    endif;
                    ?> 

                    switch ($i) {
                        case 0:
                            echo "i equals 0";
                            break;
                        case 1:
                             echo "i equals 1";
                             break;
                        case 2:
                             echo "i equals 2";
                             break;
                        default:
                             echo "i is not equal to 0, 1 or 2";
                    }
                    ?> 
                
Loops
                    //Pre-test
                    <?php
                    $i = 1;
                    while ($i <= 10):
                        echo $i;
                        $i++;
                    endwhile;
                    ?> 

                    <?php
                    for ($i = 1; $i <= 10; $i++) {
                        echo $i;
                    }
                    ?>
               
                    //Post-test
                    <?php
                    $i = 0;
                    do {
                        echo $i;
                    } while ($i > 0);
                    ?> 

                    <?php

                    //Loop control
                    for($i = 1; $i <= 10; $i++ )
                    {
                        if($i == 2)
                            continue;       //Skip to next iteration
                        elseif($i==5)
                            break;          //Exit loop
                    }
                    ?> 
                    //Collection iteration
                    <?php
                    $arr = array(1, 2, 3, 4);
                    foreach ($arr as &$value) {
                        $value = $value * 2;
                    }
                    // $arr is now array(2, 4, 6, 8)
                    ?> 
                
Casting & Conversion
                    //PHP does not require (or support) explicit type definition in variable
                    // declaration; a variable's type is determined by the context in which 
                    //that variable is used.
                    <?php
                    $foo = "0";  // $foo is string (ASCII 48)
                    $foo += 2;   // $foo is now an integer (2)
                    $foo = $foo + 1.3;  // $foo is now a float (3.3)
                    $foo = 5 + "10 Little Piggies"; // $foo is integer (15)
                    $foo = 5 + "10 Small Pigs";     // $foo is integer (15)
                    ?>

                    <?php
                    $foo = 10;                      // $foo is an integer
                    $str = "$foo";        // $str is a string
                    $fst = (string) $foo; // $fst is also a string
                    ?>
			    
Enumeration
                    <?php
                    enum MyEnum {a,b,c,d,e};
                    $enum = new OrbitEnum ("MyEnum");
                    echo $enum->a; /* write 0 */
                    echo $enum->c; /* write 2 */
                    echo $enum->e; /* write 4 */
                    ?>
                
Namespaces
                    /*You can think of the objects as something similar to 
                    directories in a filesystem. In a filesystem you can have two different files 
                    README.TXT, as long as they are in different directories.  Just like with 
                    directories where you'll have to type the full pathname in order to reach 
                    each file from the toplevel directory, you have to specify the complete name 
                    of the function you want to call: in PHP terms, the toplevel directory would 
                    be the global namespace, and the pathname separator would be ->.*/ 
                
Classes & Structs
                    //Accessibility
                    public
                    protected
                    private     
                    static
					
                    //Inheritance
                    <?php
                    class Base
                    {
                    }
                   
                    class Child extends Base
                    {
                    }
                    ?>
                
Interfaces
                    //Interface definition
                    <?php
                    interface iTemplate
                    {
                        public function setVariable($name, $var);
                    }

                    // Implement the interface
                    class Template implements iTemplate
                    {
                        private $vars = array();
                        public function setVariable($name, $var)
                        {
                            $this->vars[$name] = $var;
                        }
                    }
                    ?>
				
Functions
                    //PHP supports passing arguments by value, passing by reference, and default 
                    //argument values
                    <?php
                    function takes($input, &$string, $type = "cappuccino")
                    {
                        function body
                    }
                    ?>
                
Exception Handling
                    <?php
                    try {
                        //Throwing an exeption
                        $error = 'Always throw this error';
                        throw new Exception($error);

                        // Code following an exception is not executed.
                        echo 'Never executed';
                     } 
                    catch (Exception $e) {
                        echo 'Caught exception: ', $e->getMessage(), "\n";
                    }
                    // Continue execution
                    echo 'Hello World';
                    ?>
                
Constructors/Desctructors
                    <?php
                    class MyClass {
                        //Constructor
                        function __construct() {
                            print "In constructor\n";
                            $this->name = "MyClass";
                        }
                        //Destructor
                        function __destruct() {
                            print "Destroying " . $this->name . "\n";
                        }
                    }
                    ?>
			    
Properties
                    public $my_static = 'foo';

                    public function getValue() {
                        return self::$my_static;
                    }