Coding

Published on May 14th, 2018 | by Guest

0

PHP 7.3: A New Era of Web Development

In today’s technology-driven world, PHP is the world’s most famous and accessible server-side programming language and mainly used to build the dynamic website. PHP is freely available (open source) language. PHP scripts are executed on the server. Incredibly PHP is powerful enough to be at the core of the most prominent blogging platform on the web (WordPress) and is enough to run the most expansive social network (Facebook).

Here, the question arises why people use PHP? (Note: Hire PHP developer if you aren’t aware any technical language like PHP.) The fact about the use of PHP are mentioned below:

  • Runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
  • Apache, IIS, Nginx, Lighttpd and many more servers are compatible with PHP
  • Free to use
  • Support a wide range of databases
  • Very simple to learn and runs efficiently on the server side

Whenever PHP comes up with a new version, it brings new and useful features. Since the release of PHP 7.0, there is a leading jump in performance and highlights from the previous version 5.6. PHP 7.0 was released in 2015, 7.1 in 2016, 7.2 in 2017 and the newest PHP 7.3 will be released by the development team in late 2018.

In PHP 7.3 comes with many new features and improvements. Here, we demonstrate the expected features with the code examples, the rationale behind them and the relevant RFCs.

  1. Trailing comma is now allowed in Method & Function calls
  2. Nowdoc & Heredoc syntax
  3. Specific Option to Create json_encode()  & json_decode() (On errors)
  4. list()  is included in References
  5. is_countable()  function: New feature to Acknowledge

Trailing comma is now allowed in Method & Function calls

Arguments in function and method calls are allowed to have trailing commas. This does not affect declarations.

The following syntax would be allowed:

// regular functions.
foo('bar', 'baz',); // Notice the trailing comma after 'baz'.

In pre PHP-7.3, the above code throws an error PHP Parse error: syntax error, unexpected ‘)’ in .. on line ..

Traditionally, we cannot use more than one comma at the end or use them in succession to skip arguments. The advantage of this change is mainly for those functions with variadic parameters. So, this change makes the array syntax (which already allows trailing commas) consistent.

Note: This is the wrong way to use in function/method declarations

function foo($bar, $baz, ) { // nah, you can't do this.
}

None of your existing code will continue to work. Try it if you have other any function calls that accept variadic parameters, and I believe you could make diffs cleaner with this. I’d suggest you go ahead and add trailing commas to the calls.

Nowdoc & Heredoc syntax

Nowdoc and Heredoc syntax that uses multi-line strings had rigid requirements that the ending identifier should be the first string appearing in the new line.

$foo = <<<IDENTIFIER
the crazy dog jumps over the lazy fox
"foo" bar;
IDENTIFIER

In this, the last IDENTIFIER  must be first string in a new line for this to work. There must not be another character after the last IDENTIFIER  (other than a semicolon, which is optional).

The RFC suggests some making changes to Heredoc/Nowdoc syntax:

  1. The token which is ending that no longer needs to be first string of the line.
  2. Indented with spaces or tabs at the ending token
  3. Add more expressions after the ending token without any errors
  4. The white-space character must not intermix. If you do then you will get a Parse error: Invalid indentation – tabs and spaces cannot be mixed in .. on line …
  5. Some specific number of space/tabs used in the ending token will be stripped off from the contents within the Heredoc/Nowdoc expression.
  6. If the number of the white-space characters used in the ending token is higher than any of space/tab character within the expression, you will get the Parse error: Parse error: Invalid body indentation level (expecting an indentation level of at least ..) in .. on line ..

See below example that takes advantage of this new feature without the violating the newly enforced rules:

$foo = ['foo', 'bar', <<<EOT
    baz
        - hello world! --
    ahoy
    EOT, 'qux', 'quux'
];

var_dump($foo);

Output:

array(5) {
    [0]=>
    string(3) "foo"
    [1]=>
    string(3) "bar"
    [2]=>
    string(29) "baz
        - hello world! --
    ahoy"
    [3]=>
    string(3) "qux"
    [4]=>
    string(4) "quux"
}

Take notice how the white-spaces used in the Heredoc declaration did not make into the var_dump() ‘d to output, and we continued to add more elements to the $foo  array after the EOT token.

Specific Option to Create json_encode & json_decode (On errors)

json_encode()  and json_decode()  are known for their silent behaviour to errors in the provided PHP variables or the JSON string. But this made it prone to buggy code because everyone did not know of this edge case. In earlier years it took significant time to debug, but right now we have an option to make PHP throw an error on JSON operator failures:

try {
    json_decode("{", false, 512, JSON_THROW_ON_ERROR);
}
catch (\JsonException $exception) {
    echo $exception->getMessage(); // echoes "Syntax error"
}

The new \JsonException  is a subclass of \Exception , and both JSON_THROW_ON_ERROR  constant and JsonException  exception are declared in the global namespace.

list() is included in References

Using list(), it is effortless to assign elements of an array to variables. Until PHP 7.3, it was not possible to assign the variable by reference. At that time, it threw a fatal error:

$arr = ['apple', 'mango'];
list($a, &$b) = $arr;
$b = 'grapes';
echo $arr[1];

Fatal error: [] and list() assignments cannot be by reference in .. on line ..

You will able to do that with PHP 7.3, and the output from echo $arr[1];  will be “grapes”!

You still cannot reference non-referenceable variables:
list($a, &$b) = [12, 14];  will throw Fatal error: Cannot assign reference to non referenceable value in .. on line ..

is_countable() function: New feature to Acknowledge

PHP 7.2 deprecated quite a lot of functions and buggy use cases. In PHP 7.2, if you call the count()  with a variable that is not “countable” PHP display a warning about it.

A “countable” variable is either an array or an object whose class implements a \Countable  interface. Because there can be a lot of boilerplate code, PHP 7.3 now has a new is_countable()  function that returns if the passed variable is countable or not.

Conclusion

However, PHP 7.3 hasn’t released yet, but rumors are getting pretty warm in the market. PHP 7.3 is filled with a variety of flexible features that will help you with your web development projects. If you need help while making changes to your PHP based website, you can hire a PHP developer to assist with your project.


Author Bio:

Harshal Shah is the CEO and founder of Elsner Technologies Pvt. Ltd., a PHP development company offering various project development and testing services. He is a tech evangelist with a reputation for providing optimal solutions for automating business and solving real-life problems with the power of IT.

Tags: , , ,


About the Author

Contribution of guest authors towards Techno FAQ blog



Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top ↑