diff options
Diffstat (limited to 'php.html.markdown')
| -rw-r--r-- | php.html.markdown | 78 | 
1 files changed, 66 insertions, 12 deletions
| diff --git a/php.html.markdown b/php.html.markdown index 93066284..13cc83eb 100644 --- a/php.html.markdown +++ b/php.html.markdown @@ -12,7 +12,7 @@ This document describes PHP 5+.  <?php // PHP code must be enclosed with <?php tags  // If your php file only contains PHP code, it is best practice -// to omit the php closing tag. +// to omit the php closing tag to prevent accidental output.  // Two forward slashes start a one-line comment. @@ -103,6 +103,8 @@ END;  // String concatenation is done with .  echo 'This string ' . 'is concatenated'; +// Strings can be passed in as parameters to echo +echo 'Multiple', 'Parameters', 'Valid';  /********************************   * Constants @@ -141,6 +143,8 @@ echo $array[0]; // => "One"  // Add an element to the end of an array  $array[] = 'Four'; +// or +array_push($array, 'Five');  // Remove element from array  unset($array[3]); @@ -379,7 +383,7 @@ for ($i = 0; $i < 5; $i++) {  // Define a function with "function":  function my_function () { -  return 'Hello'; +    return 'Hello';  }  echo my_function(); // => "Hello" @@ -388,8 +392,8 @@ echo my_function(); // => "Hello"  // number of letters, numbers, or underscores.  function add ($x, $y = 1) { // $y is optional and defaults to 1 -  $result = $x + $y; -  return $result; +    $result = $x + $y; +    return $result;  }  echo add(4); // => 5 @@ -400,21 +404,21 @@ echo add(4, 2); // => 6  // Since PHP 5.3 you can declare anonymous functions;  $inc = function ($x) { -  return $x + 1; +    return $x + 1;  };  echo $inc(2); // => 3  function foo ($x, $y, $z) { -  echo "$x - $y - $z"; +    echo "$x - $y - $z";  }  // Functions can return functions  function bar ($x, $y) { -  // Use 'use' to bring in outside variables -  return function ($z) use ($x, $y) { -    foo($x, $y, $z); -  }; +    // Use 'use' to bring in outside variables +    return function ($z) use ($x, $y) { +        foo($x, $y, $z); +    };  }  $bar = bar('A', 'B'); @@ -426,6 +430,21 @@ echo $function_name(1, 2); // => 3  // Useful for programatically determining which function to run.  // Or, use call_user_func(callable $callback [, $parameter [, ... ]]); + +// You can get the all the parameters passed to a function +function parameters() { +    $numargs = func_num_args(); +    if ($numargs > 0) { +        echo func_get_arg(0) . ' | '; +    } +    $args_array = func_get_args(); +    foreach ($args_array as $key => $arg) { +        echo $key . ' - ' . $arg . ' | '; +    } +} + +parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World | +  /********************************   * Includes   */ @@ -515,7 +534,7 @@ class MyClass      }  } -// Class constants can always be accessed statically  +// Class constants can always be accessed statically  echo MyClass::MY_CONST;    // Outputs 'value';  echo MyClass::$staticVar;  // Outputs 'static'; @@ -693,8 +712,43 @@ use My\Namespace as SomeOtherNamespace;  $cls = new SomeOtherNamespace\MyClass(); +/********************** +*  Error Handling +*    */ +// Simple error handling can be done with try catch block + +try { +    // Do something +} catch ( Exception $e) { +    // Handle exception +} + +// When using try catch blocks in a namespaced enviroment use the following + +try {  +    // Do something +} catch (\Exception $e) {  +    // Handle exception +} + +// Custom exceptions + +class MyException extends Exception {} + +try { +     +    $condition = true;  +     +    if ($condition) { +        throw new MyException('Something just happend'); +    } +     +} catch (MyException $e) { +    // Handle my exception +} +  ```  ## More Information @@ -709,4 +763,4 @@ 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).
\ No newline at end of file +[PSR standards](https://github.com/php-fig/fig-standards). | 
