Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 19.62 KB | None | 0 0
  1. <?php // PHP code must be enclosed with <?php tags
  2.  
  3. // If your php file only contains PHP code, it is best practice
  4. // to omit the php closing tag to prevent accidental output.
  5.  
  6. // Two forward slashes start a one-line comment.
  7.  
  8. # So will a hash (aka pound symbol) but // is more common
  9.  
  10. /*
  11.      Surrounding text in slash-asterisk and asterisk-slash
  12.      makes it a multi-line comment.
  13. */
  14.  
  15. // Use "echo" or "print" to print output
  16. print('Hello '); // Prints "Hello " with no line break
  17.  
  18. // () are optional for print and echo
  19. echo "World\n"; // Prints "World" with a line break
  20. // (all statements must end with a semicolon)
  21.  
  22. // Anything outside <?php tags is echoed automatically
  23. ?>
  24. Hello World Again!
  25. <?php
  26.  
  27.  
  28. /************************************
  29.  * Types & Variables
  30.  */
  31.  
  32. // Variables begin with the $ symbol.
  33. // A valid variable name starts with a letter or underscore,
  34. // followed by any number of letters, numbers, or underscores.
  35.  
  36. // Boolean values are case-insensitive
  37. $boolean = true;  // or TRUE or True
  38. $boolean = false; // or FALSE or False
  39.  
  40. // Integers
  41. $int1 = 12;   // => 12
  42. $int2 = -12;  // => -12
  43. $int3 = 012;  // => 10 (a leading 0 denotes an octal number)
  44. $int4 = 0x0F; // => 15 (a leading 0x denotes a hex literal)
  45. // Binary integer literals are available since PHP 5.4.0.
  46. $int5 = 0b11111111; // 255 (a leading 0b denotes a binary number)
  47.  
  48. // Floats (aka doubles)
  49. $float = 1.234;
  50. $float = 1.2e3;
  51. $float = 7E-10;
  52.  
  53. // Delete variable
  54. unset($int1);
  55.  
  56. // Arithmetic
  57. $sum        = 1 + 1; // 2
  58. $difference = 2 - 1; // 1
  59. $product    = 2 * 2; // 4
  60. $quotient   = 2 / 1; // 2
  61.  
  62. // Shorthand arithmetic
  63. $number = 0;
  64. $number += 1;      // Increment $number by 1
  65. echo $number++;    // Prints 1 (increments after evaluation)
  66. echo ++$number;    // Prints 3 (increments before evaluation)
  67. $number /= $float; // Divide and assign the quotient to $number
  68.  
  69. // Strings should be enclosed in single quotes;
  70. $sgl_quotes = '$String'; // => '$String'
  71.  
  72. // Avoid using double quotes except to embed other variables
  73. $dbl_quotes = "This is a $sgl_quotes."; // => 'This is a $String.'
  74.  
  75. // Special characters are only escaped in double quotes
  76. $escaped   = "This contains a \t tab character.";
  77. $unescaped = 'This just contains a slash and a t: \t';
  78.  
  79. // Enclose a variable in curly braces if needed
  80. $apples = "I have {$number} apples to eat.";
  81. $oranges = "I have ${number} oranges to eat.";
  82. $money = "I have $${number} in the bank.";
  83.  
  84. // Since PHP 5.3, nowdocs can be used for uninterpolated multi-liners
  85. $nowdoc = <<<'END'
  86. Multi line
  87. string
  88. END;
  89.  
  90. // Heredocs will do string interpolation
  91. $heredoc = <<<END
  92. Multi line
  93. $sgl_quotes
  94. END;
  95.  
  96. // String concatenation is done with .
  97. echo 'This string ' . 'is concatenated';
  98.  
  99. // Strings can be passed in as parameters to echo
  100. echo 'Multiple', 'Parameters', 'Valid';  // Returns 'MultipleParametersValid'
  101.  
  102.  
  103. /********************************
  104.  * Constants
  105.  */
  106.  
  107. // A constant is defined by using define()
  108. // and can never be changed during runtime!
  109.  
  110. // a valid constant name starts with a letter or underscore,
  111. // followed by any number of letters, numbers, or underscores.
  112. define("FOO", "something");
  113.  
  114. // access to a constant is possible by calling the choosen name without a $
  115. echo FOO; // Returns 'something'
  116. echo 'This outputs ' . FOO;  // Returns 'This ouputs something'
  117.  
  118.  
  119.  
  120. /********************************
  121.  * Arrays
  122.  */
  123.  
  124. // All arrays in PHP are associative arrays (hashmaps),
  125.  
  126. // Associative arrays, known as hashmaps in some languages.
  127.  
  128. // Works with all PHP versions
  129. $associative = array('One' => 1, 'Two' => 2, 'Three' => 3);
  130.  
  131. // PHP 5.4 introduced a new syntax
  132. $associative = ['One' => 1, 'Two' => 2, 'Three' => 3];
  133.  
  134. echo $associative['One']; // prints 1
  135.  
  136. // Add an element to an associative array
  137. $associative['Four'] = 4;
  138.  
  139. // List literals implicitly assign integer keys
  140. $array = ['One', 'Two', 'Three'];
  141. echo $array[0]; // => "One"
  142.  
  143. // Add an element to the end of an array
  144. $array[] = 'Four';
  145. // or
  146. array_push($array, 'Five');
  147.  
  148. // Remove element from array
  149. unset($array[3]);
  150.  
  151. /********************************
  152.  * Output
  153.  */
  154.  
  155. echo('Hello World!');
  156. // Prints Hello World! to stdout.
  157. // Stdout is the web page if running in a browser.
  158.  
  159. print('Hello World!'); // The same as echo
  160.  
  161. // echo and print are language constructs too, so you can drop the parentheses
  162. echo 'Hello World!';
  163. print 'Hello World!';
  164.  
  165. $paragraph = 'paragraph';
  166.  
  167. echo 100;        // Echo scalar variables directly
  168. echo $paragraph; // or variables
  169.  
  170. // If short open tags are configured, or your PHP version is
  171. // 5.4.0 or greater, you can use the short echo syntax
  172. ?>
  173. <p><?= $paragraph ?></p>
  174. <?php
  175.  
  176. $x = 1;
  177. $y = 2;
  178. $x = $y; // $x now contains the same value as $y
  179. $z = &$y;
  180. // $z now contains a reference to $y. Changing the value of
  181. // $z will change the value of $y also, and vice-versa.
  182. // $x will remain unchanged as the original value of $y
  183.  
  184. echo $x; // => 2
  185. echo $z; // => 2
  186. $y = 0;
  187. echo $x; // => 2
  188. echo $z; // => 0
  189.  
  190. // Dumps type and value of variable to stdout
  191. var_dump($z); // prints int(0)
  192.  
  193. // Prints variable to stdout in human-readable format
  194. print_r($array); // prints: Array ( [0] => One [1] => Two [2] => Three )
  195.  
  196. /********************************
  197.  * Logic
  198.  */
  199. $a = 0;
  200. $b = '0';
  201. $c = '1';
  202. $d = '1';
  203.  
  204. // assert throws a warning if its argument is not true
  205.  
  206. // These comparisons will always be true, even if the types aren't the same.
  207. assert($a == $b); // equality
  208. assert($c != $a); // inequality
  209. assert($c <> $a); // alternative inequality
  210. assert($a < $c);
  211. assert($c > $b);
  212. assert($a <= $b);
  213. assert($c >= $d);
  214.  
  215. // The following will only be true if the values match and are the same type.
  216. assert($c === $d);
  217. assert($a !== $d);
  218. assert(1 === '1');
  219. assert(1 !== '1');
  220.  
  221. // 'Spaceship' operator (since PHP 7)
  222. // Returns 0 if values on either side are equal
  223. // Returns 1 if value on the left is greater
  224. // Returns -1 if the value on the right is greater
  225.  
  226. $a = 100;
  227. $b = 1000;
  228.  
  229. echo $a <=> $a; // 0 since they are equal
  230. echo $a <=> $b; // -1 since $a < $b
  231. echo $b <=> $a; // 1 since $b > $a
  232.  
  233. // Variables can be converted between types, depending on their usage.
  234.  
  235. $integer = 1;
  236. echo $integer + $integer; // => 2
  237.  
  238. $string = '1';
  239. echo $string + $string; // => 2 (strings are coerced to integers)
  240.  
  241. $string = 'one';
  242. echo $string + $string; // => 0
  243. // Outputs 0 because the + operator cannot cast the string 'one' to a number
  244.  
  245. // Type casting can be used to treat a variable as another type
  246.  
  247. $boolean = (boolean) 1; // => true
  248.  
  249. $zero = 0;
  250. $boolean = (boolean) $zero; // => false
  251.  
  252. // There are also dedicated functions for casting most types
  253. $integer = 5;
  254. $string = strval($integer);
  255.  
  256. $var = null; // Null value
  257.  
  258.  
  259. /********************************
  260.  * Control Structures
  261.  */
  262.  
  263. if (true) {
  264.     print 'I get printed';
  265. }
  266.  
  267. if (false) {
  268.     print 'I don\'t';
  269. } else {
  270.     print 'I get printed';
  271. }
  272.  
  273. if (false) {
  274.     print 'Does not get printed';
  275. } elseif (true) {
  276.     print 'Does';
  277. }
  278.  
  279. // ternary operator
  280. print (false ? 'Does not get printed' : 'Does');
  281.  
  282. // ternary shortcut operator since PHP 5.3
  283. // equivalent of "$x ? $x : 'Does'""
  284. $x = false;
  285. print($x ?: 'Does');
  286.  
  287. // null coalesce operator since php 7
  288. $a = null;
  289. $b = 'Does print';
  290. echo $a ?? 'a is not set'; // prints 'a is not set'
  291. echo $b ?? 'b is not set'; // prints 'Does print'
  292.  
  293.  
  294. $x = 0;
  295. if ($x === '0') {
  296.     print 'Does not print';
  297. } elseif ($x == '1') {
  298.     print 'Does not print';
  299. } else {
  300.     print 'Does print';
  301. }
  302.  
  303.  
  304.  
  305. // This alternative syntax is useful for templates:
  306. ?>
  307.  
  308. <?php if ($x): ?>
  309. This is displayed if the test is truthy.
  310. <?php else: ?>
  311. This is displayed otherwise.
  312. <?php endif; ?>
  313.  
  314. <?php
  315.  
  316. // Use switch to save some logic.
  317. switch ($x) {
  318.     case '0':
  319.         print 'Switch does type coercion';
  320.         break; // You must include a break, or you will fall through
  321.                // to cases 'two' and 'three'
  322.     case 'two':
  323.     case 'three':
  324.         // Do something if $variable is either 'two' or 'three'
  325.         break;
  326.     default:
  327.         // Do something by default
  328. }
  329.  
  330. // While, do...while and for loops are probably familiar
  331. $i = 0;
  332. while ($i < 5) {
  333.     echo $i++;
  334. }; // Prints "01234"
  335.  
  336. echo "\n";
  337.  
  338. $i = 0;
  339. do {
  340.     echo $i++;
  341. } while ($i < 5); // Prints "01234"
  342.  
  343. echo "\n";
  344.  
  345. for ($x = 0; $x < 10; $x++) {
  346.     echo $x;
  347. } // Prints "0123456789"
  348.  
  349. echo "\n";
  350.  
  351. $wheels = ['bicycle' => 2, 'car' => 4];
  352.  
  353. // Foreach loops can iterate over arrays
  354. foreach ($wheels as $wheel_count) {
  355.     echo $wheel_count;
  356. } // Prints "24"
  357.  
  358. echo "\n";
  359.  
  360. // You can iterate over the keys as well as the values
  361. foreach ($wheels as $vehicle => $wheel_count) {
  362.     echo "A $vehicle has $wheel_count wheels";
  363. }
  364.  
  365. echo "\n";
  366.  
  367. $i = 0;
  368. while ($i < 5) {
  369.     if ($i === 3) {
  370.         break; // Exit out of the while loop
  371.     }
  372.     echo $i++;
  373. } // Prints "012"
  374.  
  375. for ($i = 0; $i < 5; $i++) {
  376.     if ($i === 3) {
  377.         continue; // Skip this iteration of the loop
  378.     }
  379.     echo $i;
  380. } // Prints "0124"
  381.  
  382.  
  383. /********************************
  384.  * Functions
  385.  */
  386.  
  387. // Define a function with "function":
  388. function my_function () {
  389.     return 'Hello';
  390. }
  391.  
  392. echo my_function(); // => "Hello"
  393.  
  394. // A valid function name starts with a letter or underscore, followed by any
  395. // number of letters, numbers, or underscores.
  396.  
  397. function add ($x, $y = 1) { // $y is optional and defaults to 1
  398.     $result = $x + $y;
  399.     return $result;
  400. }
  401.  
  402. echo add(4); // => 5
  403. echo add(4, 2); // => 6
  404.  
  405. // $result is not accessible outside the function
  406. // print $result; // Gives a warning.
  407.  
  408. // Since PHP 5.3 you can declare anonymous functions;
  409. $inc = function ($x) {
  410.     return $x + 1;
  411. };
  412.  
  413. echo $inc(2); // => 3
  414.  
  415. function foo ($x, $y, $z) {
  416.     echo "$x - $y - $z";
  417. }
  418.  
  419. // Functions can return functions
  420. function bar ($x, $y) {
  421.     // Use 'use' to bring in outside variables
  422.     return function ($z) use ($x, $y) {
  423.         foo($x, $y, $z);
  424.     };
  425. }
  426.  
  427. $bar = bar('A', 'B');
  428. $bar('C'); // Prints "A - B - C"
  429.  
  430. // You can call named functions using strings
  431. $function_name = 'add';
  432. echo $function_name(1, 2); // => 3
  433. // Useful for programatically determining which function to run.
  434. // Or, use call_user_func(callable $callback [, $parameter [, ... ]]);
  435.  
  436.  
  437. // You can get the all the parameters passed to a function
  438. function parameters() {
  439.     $numargs = func_num_args();
  440.     if ($numargs > 0) {
  441.         echo func_get_arg(0) . ' | ';
  442.     }
  443.     $args_array = func_get_args();
  444.     foreach ($args_array as $key => $arg) {
  445.         echo $key . ' - ' . $arg . ' | ';
  446.     }
  447. }
  448.  
  449. parameters('Hello', 'World'); // Hello | 0 - Hello | 1 - World |
  450.  
  451. // Since PHP 5.6 you can get a variable number of arguments
  452. function variable($word, ...$list) {
  453.     echo $word . " || ";
  454.     foreach ($list as $item) {
  455.         echo $item . ' | ';
  456.     }
  457. }
  458.  
  459. variable("Separate", "Hello", "World"); // Separate || Hello | World |
  460.  
  461. /********************************
  462.  * Includes
  463.  */
  464.  
  465. <?php
  466. // PHP within included files must also begin with a PHP open tag.
  467.  
  468. include 'my-file.php';
  469. // The code in my-file.php is now available in the current scope.
  470. // If the file cannot be included (e.g. file not found), a warning is emitted.
  471.  
  472. include_once 'my-file.php';
  473. // If the code in my-file.php has been included elsewhere, it will
  474. // not be included again. This prevents multiple class declaration errors
  475.  
  476. require 'my-file.php';
  477. require_once 'my-file.php';
  478. // Same as include(), except require() will cause a fatal error if the
  479. // file cannot be included.
  480.  
  481. // Contents of my-include.php:
  482. <?php
  483.  
  484. return 'Anything you like.';
  485. // End file
  486.  
  487. // Includes and requires may also return a value.
  488. $value = include 'my-include.php';
  489.  
  490. // Files are included based on the file path given or, if none is given,
  491. // the include_path configuration directive. If the file isn't found in
  492. // the include_path, include will finally check in the calling script's
  493. // own directory and the current working directory before failing.
  494. /* */
  495.  
  496. /********************************
  497.  * Classes
  498.  */
  499.  
  500. // Classes are defined with the class keyword
  501.  
  502. class MyClass
  503. {
  504.     const MY_CONST      = 'value'; // A constant
  505.  
  506.     static $staticVar   = 'static';
  507.  
  508.     // Static variables and their visibility
  509.     public static $publicStaticVar = 'publicStatic';
  510.     // Accessible within the class only
  511.     private static $privateStaticVar = 'privateStatic';
  512.     // Accessible from the class and subclasses
  513.     protected static $protectedStaticVar = 'protectedStatic';
  514.  
  515.     // Properties must declare their visibility
  516.     public $property    = 'public';
  517.     public $instanceProp;
  518.     protected $prot = 'protected'; // Accessible from the class and subclasses
  519.     private $priv   = 'private';   // Accessible within the class only
  520.  
  521.     // Create a constructor with __construct
  522.     public function __construct($instanceProp)
  523.     {
  524.         // Access instance variables with $this
  525.         $this->instanceProp = $instanceProp;
  526.     }
  527.  
  528.     // Methods are declared as functions inside a class
  529.     public function myMethod()
  530.     {
  531.         print 'MyClass';
  532.     }
  533.  
  534.     // final keyword would make a function unoverridable
  535.     final function youCannotOverrideMe()
  536.     {
  537.     }
  538.  
  539.     // Magic Methods
  540.  
  541.     // what to do if Object is treated as a String
  542.     public function __toString()
  543.     {
  544.         return $property;
  545.     }
  546.  
  547.     // opposite to __construct()
  548.     // called when object is no longer referenced
  549.     public function __destruct()
  550.     {
  551.         print "Destroying";
  552.     }
  553.  
  554. /*
  555.  * Declaring class properties or methods as static makes them accessible without
  556.  * needing an instantiation of the class. A property declared as static can not
  557.  * be accessed with an instantiated class object (though a static method can).
  558.  */
  559.  
  560.     public static function myStaticMethod()
  561.     {
  562.         print 'I am static';
  563.     }
  564. }
  565.  
  566. // Class constants can always be accessed statically
  567. echo MyClass::MY_CONST;    // Outputs 'value';
  568.  
  569. echo MyClass::$staticVar;  // Outputs 'static';
  570. MyClass::myStaticMethod(); // Outputs 'I am static';
  571.  
  572. // Instantiate classes using new
  573. $my_class = new MyClass('An instance property');
  574. // The parentheses are optional if not passing in an argument.
  575.  
  576. // Access class members using ->
  577. echo $my_class->property;     // => "public"
  578. echo $my_class->instanceProp; // => "An instance property"
  579. $my_class->myMethod();        // => "MyClass"
  580.  
  581.  
  582. // Extend classes using "extends"
  583. class MyOtherClass extends MyClass
  584. {
  585.     function printProtectedProperty()
  586.     {
  587.         echo $this->prot;
  588.     }
  589.  
  590.     // Override a method
  591.     function myMethod()
  592.     {
  593.         parent::myMethod();
  594.         print ' > MyOtherClass';
  595.     }
  596. }
  597.  
  598. $my_other_class = new MyOtherClass('Instance prop');
  599. $my_other_class->printProtectedProperty(); // => Prints "protected"
  600. $my_other_class->myMethod();               // Prints "MyClass > MyOtherClass"
  601.  
  602. final class YouCannotExtendMe
  603. {
  604. }
  605.  
  606. // You can use "magic methods" to create getters and setters
  607. class MyMapClass
  608. {
  609.     private $property;
  610.  
  611.     public function __get($key)
  612.     {
  613.         return $this->$key;
  614.     }
  615.  
  616.     public function __set($key, $value)
  617.     {
  618.         $this->$key = $value;
  619.     }
  620. }
  621.  
  622. $x = new MyMapClass();
  623. echo $x->property; // Will use the __get() method
  624. $x->property = 'Something'; // Will use the __set() method
  625.  
  626. // Classes can be abstract (using the abstract keyword) or
  627. // implement interfaces (using the implements keyword).
  628. // An interface is declared with the interface keyword.
  629.  
  630. interface InterfaceOne
  631. {
  632.     public function doSomething();
  633. }
  634.  
  635. interface InterfaceTwo
  636. {
  637.     public function doSomethingElse();
  638. }
  639.  
  640. // interfaces can be extended
  641. interface InterfaceThree extends InterfaceTwo
  642. {
  643.     public function doAnotherContract();
  644. }
  645.  
  646. abstract class MyAbstractClass implements InterfaceOne
  647. {
  648.     public $x = 'doSomething';
  649. }
  650.  
  651. class MyConcreteClass extends MyAbstractClass implements InterfaceTwo
  652. {
  653.     public function doSomething()
  654.     {
  655.         echo $x;
  656.     }
  657.  
  658.     public function doSomethingElse()
  659.     {
  660.         echo 'doSomethingElse';
  661.     }
  662. }
  663.  
  664.  
  665. // Classes can implement more than one interface
  666. class SomeOtherClass implements InterfaceOne, InterfaceTwo
  667. {
  668.     public function doSomething()
  669.     {
  670.         echo 'doSomething';
  671.     }
  672.  
  673.     public function doSomethingElse()
  674.     {
  675.         echo 'doSomethingElse';
  676.     }
  677. }
  678.  
  679.  
  680. /********************************
  681.  * Traits
  682.  */
  683.  
  684. // Traits are available from PHP 5.4.0 and are declared using "trait"
  685.  
  686. trait MyTrait
  687. {
  688.     public function myTraitMethod()
  689.     {
  690.         print 'I have MyTrait';
  691.     }
  692. }
  693.  
  694. class MyTraitfulClass
  695. {
  696.     use MyTrait;
  697. }
  698.  
  699. $cls = new MyTraitfulClass();
  700. $cls->myTraitMethod(); // Prints "I have MyTrait"
  701.  
  702.  
  703. /********************************
  704.  * Namespaces
  705.  */
  706.  
  707. // This section is separate, because a namespace declaration
  708. // must be the first statement in a file. Let's pretend that is not the case
  709.  
  710. <?php
  711.  
  712. // By default, classes exist in the global namespace, and can
  713. // be explicitly called with a backslash.
  714.  
  715. $cls = new \MyClass();
  716.  
  717.  
  718.  
  719. // Set the namespace for a file
  720. namespace My\Namespace;
  721.  
  722. class MyClass
  723. {
  724. }
  725.  
  726. // (from another file)
  727. $cls = new My\Namespace\MyClass;
  728.  
  729. //Or from within another namespace.
  730. namespace My\Other\Namespace;
  731.  
  732. use My\Namespace\MyClass;
  733.  
  734. $cls = new MyClass();
  735.  
  736. // Or you can alias the namespace;
  737.  
  738. namespace My\Other\Namespace;
  739.  
  740. use My\Namespace as SomeOtherNamespace;
  741.  
  742. $cls = new SomeOtherNamespace\MyClass();
  743.  
  744.  
  745. /**********************
  746. * Late Static Binding
  747. *
  748. */
  749.  
  750. class ParentClass
  751. {
  752.     public static function who()
  753.     {
  754.         echo "I'm a " . __CLASS__ . "\n";
  755.     }
  756.  
  757.     public static function test()
  758.     {
  759.         // self references the class the method is defined within
  760.         self::who();
  761.         // static references the class the method was invoked on
  762.         static::who();
  763.     }
  764. }
  765.  
  766. ParentClass::test();
  767. /*
  768. I'm a ParentClass
  769. I'm a ParentClass
  770. */
  771.  
  772. class ChildClass extends ParentClass
  773. {
  774.     public static function who()
  775.     {
  776.         echo "But I'm " . __CLASS__ . "\n";
  777.     }
  778. }
  779.  
  780. ChildClass::test();
  781. /*
  782. I'm a ParentClass
  783. But I'm ChildClass
  784. */
  785.  
  786. /**********************
  787. *  Magic constants
  788. *  
  789. */
  790.  
  791. // Get current class name. Must be used inside a class declaration.
  792. echo "Current class name is " . __CLASS__;
  793.  
  794. // Get full path directory of a file
  795. echo "Current directory is " . __DIR__;
  796.  
  797.     // Typical usage
  798.     require __DIR__ . '/vendor/autoload.php';
  799.  
  800. // Get full path of a file
  801. echo "Current file path is " . __FILE__;
  802.  
  803. // Get current function name
  804. echo "Current function name is " . __FUNCTION__;
  805.  
  806. // Get current line number
  807. echo "Current line number is " . __LINE__;
  808.  
  809. // Get the name of the current method. Only returns a value when used inside a trait or object declaration.
  810. echo "Current method is " . __METHOD__;
  811.  
  812. // Get the name of the current namespace
  813. echo "Current namespace is " . __NAMESPACE__;
  814.  
  815. // Get the name of the current trait. Only returns a value when used inside a trait or object declaration.
  816. echo "Current trait is " . __TRAIT__;
  817.  
  818. /**********************
  819. *  Error Handling
  820. *  
  821. */
  822.  
  823. // Simple error handling can be done with try catch block
  824.  
  825. try {
  826.     // Do something
  827. } catch (Exception $e) {
  828.     // Handle exception
  829. }
  830.  
  831. // When using try catch blocks in a namespaced enviroment use the following
  832.  
  833. try {
  834.     // Do something
  835. } catch (\Exception $e) {
  836.     // Handle exception
  837. }
  838.  
  839. // Custom exceptions
  840.  
  841. class MyException extends Exception {}
  842.  
  843. try {
  844.  
  845.     $condition = true;
  846.  
  847.     if ($condition) {
  848.         throw new MyException('Something just happend');
  849.     }
  850.  
  851. } catch (MyException $e) {
  852.     // Handle my exception
  853. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement