Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. 1)Constant Array
  2. ===============
  3. Now the constant data types in php can be defined as numerically indexed array and associative array.
  4.  
  5. So below line of code can be coded like as new constant array.
  6.  
  7. old
  8. ------
  9. <?php
  10. define('COLOR_RED', '#f44141');
  11. define('COLOR_BLUE', '#4286f4');
  12. define('COLOR_GREEN', '#1ae01e');
  13. define('COLOR_PURPLE', '#f309f7');
  14. define('COLOR_ORANGE', '#ef7700');
  15.  
  16. New Way
  17. ------
  18. <?php
  19. // As an associative array
  20. define('COLORS', [
  21. 'red' => '#f44141',
  22. 'blue' => '#4286f4',
  23. 'green' => '#1ae01e',
  24. 'purple' => '#f309f7',
  25. 'orange' => '#ef7700',
  26. ]);
  27.  
  28. echo(COLORS['red']); // #f44141
  29.  
  30.  
  31. // As indexed array
  32. define('COLORS', [
  33. 'red',
  34. 'blue',
  35. 'green',
  36. 'purple',
  37. 'orange',
  38. ]);
  39.  
  40. echo(COLORS[0]); // 'red'
  41.  
  42.  
  43. 2) Group use declaration
  44. =========================
  45. Previously we need to use single statement for importing class, function or variable like below code,
  46.  
  47. <?php
  48. use VendorName/LibraryName/ClasName1;
  49. use VendorName/LibraryName/ClasName2;
  50. use VendorName/LibraryName/ClasName3;
  51.  
  52. Now we can use latest grouping method as seen below
  53. ------------------------------------
  54. <?php
  55. use VendorName/LibraryName/{ClasName1, ClassName2. ClassName3};
  56.  
  57.  
  58. 3)NULL Check operator
  59. ====================
  60. PHP will throw an error about an undefined variable, index, if the variable is not defined or empty.
  61. so we need to check below code for safe code running.
  62.  
  63. Old method
  64. ----------
  65. <?php
  66. if(!isset($_GET['key'])) {
  67. $key = 'default-value';
  68. } else {
  69. $key = $_GET['key'];
  70. }
  71.  
  72. We can use ternary operator to check this null case. but even we need to add isset function in ternarry
  73.  
  74. PHP now introduced the new " Null Coalescing Operator ( ?? ) " to check the null case.
  75.  
  76. New method
  77. ----------
  78. <?php
  79. $key = $_GET['key'] ?? 'default_value';
  80.  
  81. You can also use below code for chained checking
  82.  
  83.  
  84. <?php
  85. if (isset($_GET['key']) {
  86. $key = $_GET['key'];
  87. } else if(isset($_POST['key'])) {
  88. $key = $_POST['key'];
  89. } else {
  90. $key = 'default value';
  91. }
  92.  
  93. // Versus
  94.  
  95. $key = $_GET['key'] ?? $_POST['key'] ?? 'default value';
  96.  
  97.  
  98. 4)Return Type Declarations and Coercive for PHP function:
  99. ================================================
  100.  
  101. This will force the function to accept the argument type and return type of the function.
  102. eg:
  103.  
  104. <?php
  105. function reverseString(String $str) : String {
  106. return strrev($str);
  107. }
  108. print(reverseString(1234));
  109.  
  110.  
  111. The above function will accept only string value and returns the string value.
  112. Now Here the number 1234 is passed it will coerced into string '1234' and returns string.
  113.  
  114. As analogus to strict used in the javascript, in PHP we can declare the strict method for function.
  115. Eg below.
  116.  
  117. <?php
  118. declare(strict_types=1);
  119. function reverseString(String $str) : String {
  120. return strrev($str);
  121. }
  122. print(reverseString(1234));
  123.  
  124. This will check the argument is string and if not report a fatal error and stop execution.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement