diff options
Diffstat (limited to 'php.html.markdown')
| -rw-r--r-- | php.html.markdown | 216 | 
1 files changed, 143 insertions, 73 deletions
| diff --git a/php.html.markdown b/php.html.markdown index 1a8dea2c..20923548 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -2,6 +2,7 @@  language: php  author: Malcolm Fell  author_url: http://emarref.net/ +filename: learnphp.php  ---  This document describes PHP 5+. @@ -9,6 +10,9 @@ This document describes PHP 5+.  ```php  <?php // PHP code must be enclosed with <?php ? > tags +// If your php file only contains PHP code, it is best practise +// to omit the php closing tag. +  // Two forward slashes start a one-line comment.  # So will a hash (aka pound symbol) but // is more common @@ -22,7 +26,7 @@ This document describes PHP 5+.  print('Hello '); // Prints "Hello " with no line break  // () are optional for print and echo -echo 'World\n'; // Prints "World" with a line break +echo "World\n"; // Prints "World" with a line break  // (all statements must end with a semicolon)  // Anything outside <?php tags is echoed automatically @@ -39,13 +43,13 @@ echo 'World\n'; // Prints "World" with a line break  // followed by any number of letters, numbers, or underscores.  // Boolean values are case-insensitive -$boolean = true; // or TRUE or True +$boolean = true;  // or TRUE or True  $boolean = false; // or FALSE or False  // Integers -$int1 = 19; // => 19 -$int2 = -19; // => -19 -$int3 = 019; // => 15 (a leading 0 denotes an octal number) +$int1 = 19;   // => 19 +$int2 = -19;  // => -19 +$int3 = 019;  // => 15 (a leading 0 denotes an octal number)  $int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal)  // Floats (aka doubles) @@ -54,26 +58,26 @@ $float = 1.2e3;  $float = 7E-10;  // Arithmetic -$sum = 1 + 1; // 2 +$sum        = 1 + 1; // 2  $difference = 2 - 1; // 1 -$product = 2 * 2; // 4 -$quotient = 2 / 1; // 2 +$product    = 2 * 2; // 4 +$quotient   = 2 / 1; // 2  // Shorthand arithmetic  $number = 0; -$number += 1; // Add 1 to $number -echo $number++; // Prints 1 (increments after evaluation) -echo ++$number; // Prints 3 (increments before evalutation) +$number += 1;      // Increment $number by 1 +echo $number++;    // Prints 1 (increments after evaluation) +echo ++$number;    // Prints 3 (increments before evalutation)  $number /= $float; // Divide and assign the quotient to $number  // Strings should be enclosed in single quotes;  $sgl_quotes = '$String'; // => '$String'  // Avoid using double quotes except to embed other variables -$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String' +$dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'  // Special characters are only escaped in double quotes -$escaped = "This contains a \t tab character."; +$escaped   = "This contains a \t tab character.";  $unescaped = 'This just contains a slash and a t: \t';  // Enclose a variable in curly braces if needed @@ -109,7 +113,7 @@ $associative = array('One' => 1, 'Two' => 2, 'Three' => 3);  // PHP 5.4 introduced a new syntax  $associative = ['One' => 1, 'Two' => 2, 'Three' => 3]; -echo $associative['One']; // prints "1" +echo $associative['One']; // prints 1  // List literals implicitly assign integer keys  $array = ['One', 'Two', 'Three']; @@ -132,8 +136,8 @@ print 'Hello World!'; // So is print  $paragraph = 'paragraph'; -echo 100; -echo $paragraph; +echo 100;        // Echo scalar variables directly +echo $paragraph; // or variables  // If short open tags are configured, or your PHP version is  // 5.4.0 or greater, you can use the short echo syntax @@ -143,10 +147,11 @@ echo $paragraph;  $x = 1;  $y = 2; -$x = $y; // A now contains the same value sa $y +$x = $y; // $x now contains the same value as $y  $z = &$y; -// $x now contains a reference to $y. Changing the value of -// $x will change the value of $y also, and vice-versa. +// $z now contains a reference to $y. Changing the value of +// $z will change the value of $y also, and vice-versa. +// $x will remain unchanged as the original value of $y  echo $x; // => 2  echo $z; // => 2 @@ -167,8 +172,8 @@ $d = '1';  // These comparisons will always be true, even if the types aren't the same.  assert($a == $b); // equality -assert($b != $a); // inequality -assert($a <> $b); // alternative inequality +assert($c != $a); // inequality +assert($c <> $a); // alternative inequality  assert($a < $c);  assert($c > $b);  assert($a <= $b); @@ -215,7 +220,7 @@ if (true) {  }  if (false) { -    print "I don't"; +    print 'I don\'t';  } else {      print 'I get printed';  } @@ -251,6 +256,7 @@ switch ($x) {      case '0':          print 'Switch does type coercion';          break; // You must include a break, or you will fall through +               // to cases 'two' and 'three'      case 'two':      case 'three':          // Do something if $variable is either 'two' or 'three' @@ -275,16 +281,16 @@ do {  echo "\n";  for ($x = 0; $x < 10; $x++) { -    echo $x; // Will echo 0 - 9 -}// Prints "0123456789" +    echo $x; +} // Prints "0123456789"  echo "\n";  $wheels = ['bicycle' => 2, 'car' => 4];  // Foreach loops can iterate over arrays -foreach ($wheels as $wheel_count){ -    echo "$wheel_count"; +foreach ($wheels as $wheel_count) { +    echo $wheel_count;  } // Prints "24"  echo "\n"; @@ -302,9 +308,9 @@ while ($i < 5) {          break; // Exit out of the while loop      }      echo $i++; -}// Prints "012" +} // Prints "012" -for($i = 0; $i < 5; $i++){ +for ($i = 0; $i < 5; $i++) {      if ($i === 3) {          continue; // Skip this iteration of the loop      } @@ -317,7 +323,7 @@ for($i = 0; $i < 5; $i++){   */  // Define a function with "function": -function my_function() { +function my_function () {    return 'Hello';  } @@ -326,7 +332,7 @@ echo my_function(); // => "Hello"  // A valid function name starts with a letter or underscore, followed by any  // number of letters, numbers, or underscores. -function add($x, $y = 1) { // $y is optional, and defaults to 2 +function add ($x, $y = 1) { // $y is optional and defaults to 1    $result = $x + $y;    return $result;  } @@ -338,7 +344,7 @@ echo add(4, 2); // => 6  // print $result; // Gives a warning.  // Since PHP 5.3 you can declare anonymous functions; -$inc = function($x){ +$inc = function ($x) {    return $x + 1;  }; @@ -357,78 +363,133 @@ function bar ($x, $y) {  }  $bar = bar('A', 'B'); -$bar('C'); +$bar('C'); // Prints "A - B - C"  // You can call named functions using strings  $function_name = 'add';  echo $function_name(1, 2); // => 3 -// But, you should probably use anonymous functions instead. +// Useful for programatically determining which function to run. +// Or, use call_user_func(callable $callback [, $parameter [, ... ]]); + +/******************************** + * Includes + */ + +/* +``` +```php +<?php +// Included files must also begin with a PHP open tags. + +include 'my-file.php'; +// The code in my-file.php is now available in the current scope. +// If the file cannot be included (e.g. file not found), a warning is emitted. + +include_once 'my-file.php'; +// If the code in my-file.php has been included elsewhere, it will +// not be included again. This prevents multiple class declaration errors + +require 'my-file.php'; +require_once 'my-file.php'; +// Same as include(), except require() will cause a fatal error if the +// file cannot be included. + +// Contents of my-include.php: +<?php + +return 'Anything you like.'; +// End file + +// Includes and requires may also return a value. +$value = include 'my-include.php'; + +// Files are included based on the file path given or, if none is given, +// the include_path configuration directive. If the file isn't found in +// the include_path, include will finally check in the calling script's +// own directory and the current working directory before failing. +/* */  /********************************   * Classes   */ -//Classes are defined with the class keyword +// Classes are defined with the class keyword + +class MyClass +{ +    const MY_CONST      = 'value'; // A constant -class MyClass { -    const MY_CONST = 'value'; // A constant -    static $staticVar = 'static'; -    public $property = 'public'; // Properties must declare their visibility -    private $privprop = 'private'; // Accessible within the class only -    protected $protprop = 'protected'; // Accessible within the class and subclasses +    static $staticVar   = 'static'; + +    // Properties must declare their visibility +    public $property    = 'public';      public $instanceProp; +    protected $prot = 'protected'; // Accessible from the class and subclasses +    private $priv   = 'private';   // Accessible within the class only      // Create a constructor with __construct -    public function __construct($instanceProp){ +    public function __construct($instanceProp) {          // Access instance variables with $this          $this->instanceProp = $instanceProp;      } +      // Methods are declared as functions inside a class -    public function myMethod() { -        print "MyClass"; +    public function myMethod() +    { +        print 'MyClass';      } -    final function youCannotOverrideMe() { +    final function youCannotOverrideMe() +    {      } -    public static function myStaticMethod() { -        print "I am static"; +    public static function myStaticMethod() +    { +        print 'I am static';      }  } -echo MyClass::MY_CONST; // Outputs "value"; -echo MyClass::$staticVar; // Outputs 'static'; -MyClass::myStaticMethod(); // Outputs "I am static"; +echo MyClass::MY_CONST;    // Outputs 'value'; +echo MyClass::$staticVar;  // Outputs 'static'; +MyClass::myStaticMethod(); // Outputs 'I am static'; -// Access class members using ->. -$my_class = new MyClass("An instance property"); // The parentheses are optional. -echo $my_class->property; // => "public" +// Instantiate classes using new +$my_class = new MyClass('An instance property'); +// The parentheses are optional if not passing in an argument. + +// Access class members using -> +echo $my_class->property;     // => "public"  echo $my_class->instanceProp; // => "An instance property" -$my_class->myMethod(); // => "MyClass" +$my_class->myMethod();        // => "MyClass"  // Extend classes using "extends" -class MyOtherClass extends MyClass{ -    function printProtectedProperty(){ -        echo $this->protprop; +class MyOtherClass extends MyClass +{ +    function printProtectedProperty() +    { +        echo $this->prot;      }      // Override a method -    function myMethod() { +    function myMethod() +    {          parent::myMethod(); -        print " > MyOtherClass"; +        print ' > MyOtherClass';      }  } -$my_other_class = new MyOtherClass("Instance prop"); +$my_other_class = new MyOtherClass('Instance prop');  $my_other_class->printProtectedProperty(); // => Prints "protected" -$my_other_class->myMethod(); // Prints "MyClass > MyOtherClass" +$my_other_class->myMethod();               // Prints "MyClass > MyOtherClass" -final class YouCannotExtendMe { +final class YouCannotExtendMe +{  }  // You can use "magic methods" to create getters and setters -class MyMapClass { +class MyMapClass +{      private $property;      public function __get($key) @@ -462,16 +523,19 @@ interface InterfaceTwo  abstract class MyAbstractClass implements InterfaceOne  { -    public $x = "doSomething"; +    public $x = 'doSomething';  }  class MyConcreteClass extends MyAbstractClass implements InterfaceTwo  { -    public function doSomething(){ +    public function doSomething() +    {          echo $x;      } -    public function doSomethingElse(){ -        echo "doSomethingElse"; + +    public function doSomethingElse() +    { +        echo 'doSomethingElse';      }  } @@ -479,11 +543,14 @@ class MyConcreteClass extends MyAbstractClass implements InterfaceTwo  // Classes can implement more than one interface  class SomeOtherClass implements InterfaceOne, InterfaceTwo  { -    public function doSomething(){ -        echo "doSomething"; +    public function doSomething() +    { +        echo 'doSomething';      } -    public function doSomethingElse(){ -        echo "doSomethingElse"; + +    public function doSomethingElse() +    { +        echo 'doSomethingElse';      }  } @@ -492,12 +559,13 @@ class SomeOtherClass implements InterfaceOne, InterfaceTwo   * Traits   */ -//Traits are available since PHP 5.4.0 and are declared using the trait keyword. +// Traits are available from PHP 5.4.0 and are declared using "trait" -trait MyTrait { +trait MyTrait +{      public function myTraitMethod()      { -        print "I have MyTrait"; +        print 'I have MyTrait';      }  } @@ -565,3 +633,5 @@ Visit the [official PHP documentation](http://www.php.net/manual/) for reference  If you're interested in up-to-date best practices, visit [PHP The Right Way](http://www.phptherightway.com/).  If you're coming from a language with good package management, check out [Composer](http://getcomposer.org/). + +For common standards, visit the PHP Framework Interoperability Group's [PSR standards](https://github.com/php-fig/fig-standards). | 
