Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1)Constant Array
- ===============
- Now the constant data types in php can be defined as numerically indexed array and associative array.
- So below line of code can be coded like as new constant array.
- old
- ------
- <?php
- define('COLOR_RED', '#f44141');
- define('COLOR_BLUE', '#4286f4');
- define('COLOR_GREEN', '#1ae01e');
- define('COLOR_PURPLE', '#f309f7');
- define('COLOR_ORANGE', '#ef7700');
- New Way
- ------
- <?php
- // As an associative array
- define('COLORS', [
- 'red' => '#f44141',
- 'blue' => '#4286f4',
- 'green' => '#1ae01e',
- 'purple' => '#f309f7',
- 'orange' => '#ef7700',
- ]);
- echo(COLORS['red']); // #f44141
- // As indexed array
- define('COLORS', [
- 'red',
- 'blue',
- 'green',
- 'purple',
- 'orange',
- ]);
- echo(COLORS[0]); // 'red'
- 2) Group use declaration
- =========================
- Previously we need to use single statement for importing class, function or variable like below code,
- <?php
- use VendorName/LibraryName/ClasName1;
- use VendorName/LibraryName/ClasName2;
- use VendorName/LibraryName/ClasName3;
- Now we can use latest grouping method as seen below
- ------------------------------------
- <?php
- use VendorName/LibraryName/{ClasName1, ClassName2. ClassName3};
- 3)NULL Check operator
- ====================
- PHP will throw an error about an undefined variable, index, if the variable is not defined or empty.
- so we need to check below code for safe code running.
- Old method
- ----------
- <?php
- if(!isset($_GET['key'])) {
- $key = 'default-value';
- } else {
- $key = $_GET['key'];
- }
- We can use ternary operator to check this null case. but even we need to add isset function in ternarry
- PHP now introduced the new " Null Coalescing Operator ( ?? ) " to check the null case.
- New method
- ----------
- <?php
- $key = $_GET['key'] ?? 'default_value';
- You can also use below code for chained checking
- <?php
- if (isset($_GET['key']) {
- $key = $_GET['key'];
- } else if(isset($_POST['key'])) {
- $key = $_POST['key'];
- } else {
- $key = 'default value';
- }
- // Versus
- $key = $_GET['key'] ?? $_POST['key'] ?? 'default value';
- 4)Return Type Declarations and Coercive for PHP function:
- ================================================
- This will force the function to accept the argument type and return type of the function.
- eg:
- <?php
- function reverseString(String $str) : String {
- return strrev($str);
- }
- print(reverseString(1234));
- The above function will accept only string value and returns the string value.
- Now Here the number 1234 is passed it will coerced into string '1234' and returns string.
- As analogus to strict used in the javascript, in PHP we can declare the strict method for function.
- Eg below.
- <?php
- declare(strict_types=1);
- function reverseString(String $str) : String {
- return strrev($str);
- }
- print(reverseString(1234));
- 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