{"id":1355,"date":"2019-12-11T12:09:42","date_gmt":"2019-12-11T12:09:42","guid":{"rendered":"https:\/\/www.hostinger.com\/blog\/?p=1355"},"modified":"2022-02-25T11:38:39","modified_gmt":"2022-02-25T11:38:39","slug":"introducing-php-7-4","status":"publish","type":"post","link":"https:\/\/www.hostinger.com\/blog\/introducing-php-7-4","title":{"rendered":"Introducing PHP 7.4: Performance, Features, Deprecations"},"content":{"rendered":"<p>PHP 7.4 has finally arrived! This new version, released on November 28, 2019, is now available on all <a href=\"https:\/\/www.hostinger.com\/\">Hostinger<\/a> servers. Developers can expect improvements in code readability, maintenance, and ease of use. Let&rsquo;s look at some of the new features, performance tweaks, and other reasons why you should migrate to PHP 7.4.<\/p><p><strong>Table of Contents:&nbsp;<\/strong><\/p><ul>\n<li><a href=\"#what-does-php-7-4-mean-for-you\">What Does PHP 7.4 Mean for You?<\/a><\/li>\n<li><a href=\"#changing-your-php-version\">Changing Your PHP Version<\/a><\/li>\n<li><a href=\"#whats-new-in-php-7-4\">What&rsquo;s New in PHP 7.4?<\/a><\/li>\n<li><a href=\"#deprecations\">Deprecations<\/a><\/li>\n<\/ul><h2 id=\"what-does-php-7-4-mean-for-you\">What Does PHP 7.4 Mean for You?<\/h2><p>PHP continues to evolve with the newest PHP 7.4 update full of new features. Like we have seen in previous PHP 7 releases, performance and speed keep improving. One of the most exciting new features is <strong>preloading<\/strong>. It helps speed up script execution and introduces the ability to have faster and cleaner code, thanks to the simplification of common lines of code.<\/p><p>The good people responsible for PHP have heard their audience&rsquo;s comments and requests and answered them completely. They have since been continuously changing code to be more intuitive and easier to switch between programming languages.<\/p><p>PHP is used in over 78.9% of all websites. According to <a href=\"https:\/\/w3techs.com\/technologies\/details\/pl-php\" target=\"_blank\" rel=\"noopener\">W3techs<\/a>, the most popular sites using PHP are Wikipedia, Pinterest, and Facebook, to name a few.<\/p><p>If we specifically look at WordPress sites running PHP, comparing PHP 5 and 7, we can see a double speed increase. WordPress-powered websites definitely gain the most by using the latest PHP version. Hostinger users can supercharge their WordPress sites with just a click of a button.<\/p><p><img decoding=\"async\" class=\"aligncenter size-full wp-image-1356\" src=\"https:\/\/www.hostinger.com\/blog\/wp-content\/uploads\/sites\/4\/2019\/12\/php-usage-statistics.png\" alt=\"PHP Usage Statistics\" width=\"1468\" height=\"1142\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/4\/2019\/12\/php-usage-statistics.png\/w=1468,fit=scale-down 1468w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/4\/2019\/12\/php-usage-statistics.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/4\/2019\/12\/php-usage-statistics.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/4\/2019\/12\/php-usage-statistics.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1468px) 100vw, 1468px\" \/><\/p><p>See all these cool figures? This graph contains information about websites actively using PHP. Are <a href=\"https:\/\/trends.builtwith.com\/websitelist\/PHP\" target=\"_blank\" rel=\"noopener\">39,191,714<\/a> live websites enough to grab your attention? That&rsquo;s how many are using PHP right now. Plus PHP 7.4 is already testing better than PHP 7.3 with improved performance and other quality of life improvements.<\/p><p>The graph below shows the overall <a href=\"https:\/\/www.phoronix.com\/scan.php?page=article&amp;item=php-74-benchmarks&amp;num=1\" target=\"_blank\" rel=\"noopener\">benchmark test<\/a> on new and old versions of PHP. Some of the criteria tested were ease of use, speed, and performance.<\/p><p><img decoding=\"async\" class=\"aligncenter wp-image-1369 size-full\" src=\"https:\/\/www.hostinger.com\/blog\/wp-content\/uploads\/sites\/4\/2019\/12\/php-geometric-mean-of-all-results-1.png\" alt=\"PHP Geometric Mean of All Results\" width=\"1463\" height=\"1035\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/4\/2019\/12\/php-geometric-mean-of-all-results-1.png\/w=1463,fit=scale-down 1463w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/4\/2019\/12\/php-geometric-mean-of-all-results-1.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/4\/2019\/12\/php-geometric-mean-of-all-results-1.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/4\/2019\/12\/php-geometric-mean-of-all-results-1.png\/w=768,fit=scale-down 768w\" sizes=\"(max-width: 1463px) 100vw, 1463px\" \/><\/p><h2 id=\"changing-your-php-version\">Changing Your PHP Version<\/h2><p>Ready to update? Thought so. Hostinger makes it as easy as ever with these four simple steps. You&rsquo;ll be fiddling around with your new-and-improved PHP version in no time.<\/p><ol>\n<li>Log into your Hostinger account and hit the <strong>Home<\/strong> button.<\/li>\n<li>On your Home page, scroll down to the <strong>Hosting<\/strong> section and click on the <strong>Manage<\/strong> icon.<\/li>\n<li>In the search box, type in <strong>PHP configuration<\/strong> and click on it.<\/li>\n<li>Select PHP 7.4 and click <strong>Save<\/strong>.<\/li>\n<\/ol><p><img decoding=\"async\" class=\"aligncenter size-full wp-image-1360\" src=\"https:\/\/www.hostinger.com\/blog\/wp-content\/uploads\/sites\/4\/2019\/12\/enabling-php-7.4-hostinger-hpanel.gif\" alt=\"Enabling PHP 7.4 in Hostinger hPanel\" width=\"856\" height=\"372\"><\/p><p>Congrats! You now have the best and most up-to-date PHP version out there.<\/p><p>To check your current PHP version, all you need to do is go to the <strong>Hosting<\/strong> tab and check the left side panel for <strong>PHP version<\/strong>. If it&rsquo;s anything less than 7.4, go ahead and update.<\/p><h2 id=\"whats-new-in-php-7-4\">What&rsquo;s New in PHP 7.4?<\/h2><p>Since 2016, PHP7 has been releasing annual updates without fail. Each year they deliver new features, additions, and the possibility to write cleaner code that makes the language more reliable and user-friendly for those who run it on their websites.<\/p><p>Let&rsquo;s dig in and take a closer look at some of the changes that were made in PHP 7.4. For a full list check out the changelog <a href=\"https:\/\/www.php.net\/manual\/en\/migration74.php\" target=\"_blank\" rel=\"noopener\">here<\/a>.<\/p><h3>Preloading<\/h3><p>Let&rsquo;s talk about code. When using a framework or library, its files have to be loaded and linked on every request. Preloading is when you can load frameworks and libraries into OPCache. It allows for the server to load PHP files and store them in memory during startup and have them available for any future requests. Talk about getting things going quick!<\/p><p>Preloading is run by a specific <strong>php.ini<\/strong> directive: <strong>opache.preload<\/strong>.This has the PHP script compiler and executes when the server starts up. It can also be used to preload more files and choose to either include or compile them.<\/p><p>This is awesome &ndash; however, if the source of the preloaded files is ever changed, the server must be restarted. The preloaded files also remain cached in the OPCache memory forever.<\/p><p>That said, these preloaded files will continue to be available for any future requests in case you ever need to use them again.<\/p><h3>Spread Operator in Array Expressions<\/h3><p>Back when PHP 5.6 was released, PHP began supporting argument unpacking (spread operator) but now, with 7.4, we are able to use this feature with an array expression. Argument unpacking is a syntax for unpacking arrays and traversables into argument lists. And, in order to do so, it only needs to be prepended by &hellip; (3 dots.) That&rsquo;s it.<\/p><p>Let&rsquo;s look at this example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$animals = ['dog', 'cat'];\r\n$animalkingdom = ['lion', 'elephant', ...$animals, 'giraffe'];\r\n\/\/ [&lsquo;lion&rsquo;, &lsquo;elephant&rsquo;, &lsquo;dog&rsquo;, &lsquo;cat&rsquo;, &lsquo;giraffe&rsquo;];<\/pre><p>We can now expand an array from anywhere we want in another array, by simply using the Spread Operator syntax.<\/p><p>Here is a longer example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$num1 = [1, 2, 3];\r\n$num2 = [...$num1]; \/\/ [1, 2, 3]\r\n$num3 = [0, ...$num1]; \/\/ [0, 1, 2, 3]\r\n$num4 = array(...$num1, ...$num2, 111); \/\/ [1, 2, 3, 1, 2, 3, 111]\r\n$num5 = [...$num1, ...$num1]; \/\/ [1, 2, 3, 1, 2, 3]<\/pre><p>Not only that, but you can also use it in a function. Check out this example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">function getNum() {\r\n  return ['a', 'b'];\r\n}\r\n$num6 = [...getNum(), 'c']; \/\/ ['a', 'b', 'c']\r\n\r\n$num7 = [...new NumIterator(['a', 'b', 'c'])]; \/\/ ['a', 'b', 'c']\r\n\r\nfunction arrGen() {\r\n    for($i = 11; $i &lt; 15; $i++) {\r\n        yield $i;\r\n    }\r\n}\r\n$num8 = [...arrGen()]; \/\/ [11, 12, 13, 14]<\/pre><p>In addition, you are now able to unpack arrays and generators that are returned by a function directly into a new array.<\/p><p>A code example would look like this:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">function getAnimals(){\r\n\treturn ['dog', 'cat', 'elephant'];\r\n}\r\n$num1 = [...getAnimals(), 'lion', 'tiger', 'giraffe'];<\/pre><p>And with PHP 7.4, it would print:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">array(6) {\r\n\t[0]=&gt;\r\n\tstring(3) \"dog\"\r\n\t[1]=&gt;\r\n\tstring(3) \"cat\"\r\n\t[2]=&gt;\r\n\tstring(8) \"elephant\"\r\n\t[3]=&gt;\r\n\tstring(4) \"lion\"\r\n\t[4]=&gt;\r\n\tstring(5) \"tiger\"\r\n\t[5]=&gt;\r\n\tstring(7) \"giraffe\"\r\n}<\/pre><p>With this new array expression, spread operators should have way better performance over the 7.3 <strong>array_merge<\/strong>. This is because the spread operator is a language structure while <strong>array_merge<\/strong> is a function. Also because spread operator supports <strong>objects<\/strong> implementing traversable and <strong>array_merge<\/strong> only supports arrays.<\/p><p>It&rsquo;s important to note that you can only use indexed arrays since string keys are not supported. If used, a recoverable error will be shown on the screen once a string key is found.<\/p><p>Another great benefit of 7.4 is the removal of the array_merge. Say goodbye to the dreaded index shift!<\/p><p>For example, let&rsquo;s look at this long-winded array merge below:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$array = [&lsquo;banana, &lsquo;orange&rsquo;];\r\n$array[2] = &lsquo;orange&rsquo;;\r\n$array[1] = &lsquo;apple&rsquo;; \/\/shifting\r\nvar_dump($array); \r\n\/\/ prints \r\narray(3) {\r\n\t[0]=&gt;\r\n\tstring(6) \"banana\"\r\n\t[1]=&gt;\r\n\tstring(5) \"apple\"\r\n\t[2]=&gt;\r\n\tstring(6) \"orange\"<\/pre><p>Another benefit of 7.4 is the generator function. A <a href=\"https:\/\/www.php.net\/manual\/en\/language.generators.syntax.php\" target=\"_blank\" rel=\"noopener\">generator function<\/a> works just like a normal function, except instead of returning a value, it yields as many values as it needs to.<\/p><p>Check out the code example below:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">function generator() {\r\n\tfor ($i = 3; $i &lt;= 5; $i++) {\r\n\t\tyield $i;\r\n\t}\r\n}\r\n$num1 = [0, 1, 2, ...generator()];<\/pre><h3>Weak References<\/h3><p>Now PHP 7.4 has a WeakReference class, which is not to be confused with the class <strong>WeakRed<\/strong> or the <strong>Weakref<\/strong> extension.<\/p><p><strong>WeakReferences<\/strong> let the programmer recall a reference to an object. This is useful because it doesn&rsquo;t prevent the object from being destroyed. They are helpful for implementing cache-like structures.<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">WeakReference {\r\n\/* Methods *\/\r\npublic __construct ( void )\r\npublic static create ( object $referent ) : WeakReference\r\npublic get ( void ) : ?object\r\n}<\/pre><h3>Contravariant Parameters and Covariant Returns<\/h3><p>Currently, PHP uses mostly invariant parameter types and return types. This means that if a method has a parameter or return type of X, then the subtype parameter or return type must also be type <strong>X<\/strong>.<\/p><p>Now, PHP 7.4 allows <strong>covariant<\/strong> (ordered from specific to generic) and <strong>contravariant<\/strong> (reversing the order) on parameter and return types.<\/p><p>Here are examples of both:<\/p><p>Covariant return type example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">interface Factory {\r\n\tfunction make(): object;\r\n}\r\nclass UserFactory implements Factory {\r\n\tfunction make(): User;\r\n}<\/pre><p>Contravariant parameter type example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">interface Concatable {\r\n\tfunction concat(Iterator $input); \r\n}\r\nclass Collection implements Concatable {\r\n\t\/\/ accepts all iterables, not just Iterator\r\n\tfunction concat(iterable $input) {\/* . . . *\/}\r\n}<\/pre><h3>Typed Properties 2.0<\/h3><p>Since PHP 5, type hints have been an available feature allowing you to specify the type of variable that is expected to be passed to a function or class. In the PHP 7.2 migrations, the addition of the <strong>object<\/strong> data type gave hope that more would be available in the future. The future is now.<\/p><p>In the new 7.4, PHP is able to support the following type list:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">bool, int, float, string, array, object, iterable, self, parent\r\nany class or interface name\r\n?type \/\/ where \"type\" may be any of the above<\/pre><p>Note that the <strong>parent<\/strong> type can be used in classes and does not need to have a parent consistent with the parameter and return type.<\/p><p>Also, note that <strong>void<\/strong> and <strong>callable<\/strong> are not supported. <strong>Void<\/strong> was removed because it was not useful and had unclear semantics; <strong>Callable &ndash;<\/strong>&nbsp;because its behavior was context-dependent.<\/p><p>Let&rsquo;s check out some more examples.<\/p><p>Here is a class written for PHP 7.3:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">class User {\r\n    \/** @var int $id *\/\r\n    private $id;\r\n    \/** @var string $name *\/\r\n    private $name;\r\n \r\n    public function __construct(int $id, string $name) {\r\n        $this-&gt;id = $id;\r\n        $this-&gt;name = $name;\r\n    }\r\n \r\n    public function getId(): int {\r\n        return $this-&gt;id;\r\n    }\r\n    public function setId(int $id): void {\r\n        $this-&gt;id = $id;\r\n    }\r\n \r\n    public function getName(): string {\r\n        return $this-&gt;name;\r\n    }\r\n    public function setName(string $name): void {\r\n        $this-&gt;name = $name;\r\n    }\r\n}<\/pre><p>In PHP 7.4, without sacrificing any type-safety, a class can now be written as simple as:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">class User {\r\n    public int $id;\r\n    public string $name;\r\n \r\n    public function __construct(int $id, string $name) {\r\n        $this-&gt;id = $id;\r\n        $this-&gt;name = $name;\r\n    }\r\n}<\/pre><p>Here are some examples of all the types 7.4 now supports:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">class Example {\r\n  \r\n    public int $scalarType;\r\n    protected ClassName $classType;\r\n    private ?ClassName $nullableClassType;\r\n\r\n    \/\/ Types are also legal on static properties\r\n    public static iterable $staticProp;\r\n\r\n    \/\/ Types can also be used with the \"var\" notation\r\n    var bool $flag;\r\n\r\n    \/\/ Typed properties may have default values (more below)\r\n    public string $str = \"foo\";\r\n    public ?string $nullableStr = null;\r\n\r\n    \/\/ The type applies to all properties in one declaration\r\n    public float $x, $y;\r\n    \/\/ equivalent to:\r\n    public float $x;\r\n    public float $y;\r\n}<\/pre><h3>Arrow Functions 2.0<\/h3><p>Anonymous functions in PHP tend to be wordy and lengthy, even when they are only performing simple operations. This is partially due to a large amount of syntactic boilerplate and the need to manually import used variables.<\/p><p>This makes code that uses simple closures confusing to read and even harder to understand.<\/p><p>Let&rsquo;s look at some code that you would use with PHP 7.3:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">function array_values_from_keys($arr, $keys) {\r\n    return array_map(function ($x) use ($arr) { return $arr[$x]; }, $keys);\r\n}<\/pre><p>Here is the more concise syntax of PHP 7.4:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">function array_values_from_keys($arr, $keys) {\r\n    return array_map(fn($x) =&gt; $arr[$x], $keys);\r\n}<\/pre><p><span style=\"font-weight: 400\">Therefore, arrow functions now have this simple form:<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">fn(parameter_list) =&gt; expr<\/pre><p><span style=\"font-weight: 400\">Below you can see an example of two functions <\/span><strong>$fn1 (7.3)<\/strong><span style=\"font-weight: 400\"> and <\/span><strong>$fn2 (7.4)<\/strong><span style=\"font-weight: 400\"> side by side. They have the same outcome but look different:<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$y = 1;\r\n$fn1 = fn($x) =&gt; $x + $y;\r\n\r\n\r\n$fn2 = function ($x) use ($y) \r\n{\r\n    return $x + $y;\r\n};<\/pre><p><span style=\"font-weight: 400\">This will also work if the arrow functions are nested:<\/span><\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">$z = 1;\r\n$fn = fn($x) =&gt; fn($y) =&gt; $x * $y + $z;<\/pre><p><span style=\"font-weight: 400\">Here the outer function captures <\/span><strong>$z<\/strong><span style=\"font-weight: 400\">. Then, the inner function also captures <\/span><strong>$z<\/strong><span style=\"font-weight: 400\"> from the outer function. With 7.4, the outer scope can become available in the inner function. This is something 7.3 wasn&rsquo;t able to do.<\/span><\/p><p>The arrow function syntax allows for a variety of functions, such as variadics, default values, parameter and return types, as well as by-reference passing and returning, all while keeping a clean, readable look. Below are all the valid arrow functions now available:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">fn(array $x) =&gt; $x;\r\nfn(): int =&gt; $x;\r\nfn($x = 42) =&gt; $x;\r\nfn(&amp;$x) =&gt; $x;\r\nfn&amp;($x) =&gt; $x;\r\nfn($x, ...$rest) =&gt; $rest;<\/pre><p>One thing to note is that arrow functions have the lowest precedence. See the example:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">fn($x) =&gt; $x + $y\r\n\/\/ is\r\nfn($x) =&gt; ($x + $y)\r\n\/\/ not\r\n(fn($x) =&gt; $x) + $y<\/pre><h2 id=\"deprecations\">Deprecations<\/h2><p>There are many deprecations happening with the move to 7.4. The following list is a short overview of the functions targeted for deprecation. You can find a more detailed explanation <a href=\"https:\/\/wiki.php.net\/rfc\/deprecations_php_7_4\" target=\"_blank\" rel=\"noopener\">here<\/a>:<\/p><ul>\n<li>The <strong>real<\/strong> type<\/li>\n<li>Magic quotes legacy<\/li>\n<li><strong>array_key_exists()<\/strong> with objects<\/li>\n<li><strong>FILTER_SANITIZE_MAGIC_QUOTES<\/strong> filter<\/li>\n<li>Reflection <strong>export()<\/strong> methods<\/li>\n<li><strong>mb_strrpos()<\/strong> with encoding as 3rd argument<\/li>\n<li><strong>implode()<\/strong> parameter order mix<\/li>\n<li>Unbinding <strong>$this<\/strong> from non-static closures<\/li>\n<li><strong>hebrevc()<\/strong> function<\/li>\n<li><strong>convert_cyr_string()<\/strong> function<\/li>\n<li><strong>money_format()<\/strong> function<\/li>\n<li><strong>ezmlm_hash()<\/strong> function<\/li>\n<li><strong>restore_include_path()<\/strong> function<\/li>\n<li><strong>allow_url_include<\/strong> ini directiv<\/li>\n<\/ul><p>Some important ones to note are the following two-step deprecations.<\/p><h3>Changing the Precedence of a Concatenation Operator<\/h3><p>Currently the precedence of &lsquo;.&rsquo;, &lsquo;+,&rsquo; and &lsquo;-&lsquo; operators are all equal. Any combination of these operators will simply be solved left-to-right.<\/p><p>Let&rsquo;s look at this PHP 7.3 code:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">echo \"sum: \" . $a + $b; \r\n\/\/ would be evaluated left-to-right\r\necho (\"sum: \" . $a) + $b;\r\n\/\/ could also look like this<\/pre><p>With PHP 7.4, &lsquo;+&rsquo; and &lsquo;-&rsquo; take precedence over &lsquo;.&rsquo; so the additions and subtractions would always be performed before the string. This looks like the following:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">echo \"sum: \" . $a + $b; \r\n\/\/ would be executed as if the code were as follows.\r\necho \"sum :\" . ($a + $b);<\/pre><p>This two-step proposal aims to be less error-prone and more intuitive. PHP 7.4 currently is in the first stage with a deprecation notice of un-parenthesized expressions of &lsquo;+&rsquo;, &lsquo;-,&rsquo; and &lsquo;.&rsquo; while waiting for the final vote\/change happening in PHP 8.<\/p><h3>Left-Associative Ternary Operator<\/h3><p>Unlike most other languages, the ternary operator in PHP is left-associative rather than right-associative. This is not only uncommon but also confusing for programmers who switch between different languages. PHP 7.4 proposes to remove the left-associativity and requires the use of parentheses instead.<\/p><p>Let&rsquo;s take a look at the code below:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">return $a == 1 ? 'one'\r\n     : $a == 2 ? 'two'\r\n     : $a == 3 ? 'three'\r\n     : $a == 4 ? 'four'\r\n               : 'other';<\/pre><p>In most other languages it would be interpreted as:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">return $a == 1 ? 'one'\r\n     : ($a == 2 ? 'two'\r\n     : ($a == 3 ? 'three'\r\n     : ($a == 4 ? 'four'\r\n               : 'other')))<\/pre><p>While in PHP, it is instead interpreted as:<\/p><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\">return ((($a == 1 ? 'one'\r\n     : $a == 2) ? 'two'\r\n     : $a == 3) ? 'three'\r\n     : $a == 4) ? 'four'\r\n               : 'other';<\/pre><p>This can lead to errors because it&rsquo;s generally not what was intended.<\/p><p>Through a separate two-step proposal, PHP 7.4 has implemented the explicit use of parentheses as a deprecation warning and will hopefully carry out a compile runtime error in future versions.<\/p><h2 id=\"h-conclusion\">Conclusion<\/h2><p>Just in time for the holidays, PHP 7.4 brings new features and quality of life improvements for all PHP developers.<\/p><p>WordPress websites will definitely benefit from these improvements and their users can expect faster execution times and less memory usage when using PHP 7.4 compared to the previous versions.<\/p><p>With the addition of first-class property type declarations and type hinting, arrow merging functions, and ridiculously better speed, the 7.4 will surely improve both the pace and quality of your workflow.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP 7.4 has finally arrived! This new version, released on November 28, 2019, is now available on all Hostinger servers. Developers can expect improvements in code readability, mai\u2026<\/p>\n","protected":false},"author":27,"featured_media":1367,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"hashtags":[],"class_list":["post-1355","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-engineering"],"hreflangs":[],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/posts\/1355","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/users\/27"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/comments?post=1355"}],"version-history":[{"count":18,"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/posts\/1355\/revisions"}],"predecessor-version":[{"id":2567,"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/posts\/1355\/revisions\/2567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/media\/1367"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/media?parent=1355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/categories?post=1355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/tags?post=1355"},{"taxonomy":"hashtags","embeddable":true,"href":"https:\/\/www.hostinger.com\/blog\/wp-json\/wp\/v2\/hashtags?post=1355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}