{"id":152,"date":"2023-10-17T23:15:35","date_gmt":"2023-10-17T21:15:35","guid":{"rendered":"https:\/\/extendsclass.com\/blog\/?p=152"},"modified":"2023-05-30T11:44:26","modified_gmt":"2023-05-30T09:44:26","slug":"php-tips-and-tricks","status":"publish","type":"post","link":"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks","title":{"rendered":"Handy PHP Tips and Tricks: Operators and functions to know"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_47_1 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"ez-toc-toggle-icon-1\"><label for=\"item-69daafb15baaf\" aria-label=\"Table of Content\"><span style=\"display: flex;align-items: center;width: 35px;height: 30px;justify-content: center;direction:ltr;\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/label><input  type=\"checkbox\" id=\"item-69daafb15baaf\"><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks\/#Yield_keyword\" title=\"Yield keyword\">Yield keyword<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks\/#Array_functions\" title=\"Array functions\">Array functions<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks\/#array_map\" title=\"array_map()\">array_map()<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks\/#array_filter\" title=\"array_filter()\">array_filter()<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks\/#Concise_code\" title=\"Concise code\">Concise code<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks\/#Nullsafe_operator\" title=\"Nullsafe operator&nbsp;\">Nullsafe operator&nbsp;<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks\/#Null_Coalescing_Operator\" title=\"Null Coalescing Operator\">Null Coalescing Operator<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks\/#Arrow_functions\" title=\"Arrow functions\">Arrow functions<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/extendsclass.com\/blog\/php-tips-and-tricks\/#Constructor_property_promotion\" title=\"Constructor property promotion\">Constructor property promotion<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Yield_keyword\"><\/span>Yield keyword<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>The &#8220;yield&#8221; operator in PHP is a powerful tool used to produce a sequence of values. Instead of returning all the values at once, &#8220;yield&#8221; allows the  function to yield each value as it is requested, resulting in more efficient memory usage. Here&#8217;s a concise example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function generateNumbers() {\n    yield 1;\n    yield 2;\n    yield 3;\n}\n\nforeach (generateNumbers() as $number) {\n    echo $number . \" \";\n}\n\/\/ Output: 1 2 3<\/code><\/pre>\n\n\n\n<p>Consider a scenario where you need to load one million rows of data from a database. The traditional approach of loading all the data at once can be memory-intensive and may impact the performance of your application. However, with the help of the &#8220;yield&#8221; operator, you can overcome these challenges and maintain a clean and efficient codebase. By using the &#8220;yield&#8221; operator in a generator function, you can fetch and process the data gradually, one row at a time. This allows you to conserve memory resources and ensures that your code remains concise and easy to understand. The &#8220;yield&#8221; operator proves to be a valuable tool in handling large datasets without compromising code simplicity and clarity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Array_functions\"><\/span>Array functions<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are numerous PHP functions available for manipulating arrays. Let&#8217;s take a brief look at some of my favorite functions that I find particularly useful for array manipulation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"array_map\"><\/span><code>array_map<\/code>()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>array_map<\/code> function transforms array elements by applying a callback function. It&#8217;s a handy tool for efficient array manipulation.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ multiplying each element by 2\n\n$myTab = array_map( function($x) { return $x * 2; }, &#91;1, 2, 3] );\n\necho json_encode  ( $myTab ); \/\/ Output &#91;2, 4, 6] <\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"array_filter\"><\/span><code>array_filter<\/code>()<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The <code>array_filter<\/code> function  selectively filters array elements based on a callback function. It&#8217;s great for removing unwanted elements from an array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ keeping only the even numbers\n\n$myTab = array_filter(&#91;1, 2, 3, 4, 5], function($x) { return $x % 2 === 0; });\n\necho json_encode  ( $myTab ); \/\/ Output &#91;2, 4] <\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Concise_code\"><\/span>Concise code<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Personally, I like concise code.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Nullsafe_operator\"><\/span><strong><strong>Nullsafe operator<\/strong>&nbsp;<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The Nullsafe operator provides a safe way to access properties or methods on potentially null objects. It simplifies code and reduces null-related errors. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ With Nullsafe operator\n$address = $user?-&gt;getProfile()?-&gt;getAddress();\n\n\/\/ Without Nullsafe operator\nif ($user !== null &amp;&amp; $user-&gt;getProfile() !== null &amp;&amp; $user-&gt;getProfile()-&gt;getAddress() !== null) {\n    $address = $user-&gt;getProfile()-&gt;getAddress();\n} else {\n    $address = null;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Null_Coalescing_Operator\"><\/span>Null Coalescing Operator<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>The null coalescing operator offers a concise way to handle null values. It allows you to provide a default value if a variable is null. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ With Coalescing operator\n$name = $username ?? 'Guest';\n\n\/\/ Without Coalescing operator\n$name = isset($username) ? $username : 'Guest';\n\/\/ Or worse\nif (isset($username)) {\n    $name = $username;\n} else {\n    $name = 'Guest';\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Arrow_functions\"><\/span>Arrow functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Arrow functions introduce a more compact syntax for writing anonymous functions. They provide a shorthand way to define functions without the need for the &#8220;function&#8221; keyword. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$addition = fn($a, $b) =&gt; $a + $b;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Constructor_property_promotion\"><\/span><strong>Constructor property promotion<\/strong><span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>Less boilerplate code to define and initialize properties.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class User {\n  public function __construct(\n    public int $id = 0,\n    public string $firstname = null,\n    public string $lastname = null,  \n  ) {}\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Useful PHP Tips and Tricks: Must-Know Operators and Functions. Yield, Concise code, and Array functions.<\/p>\n","protected":false},"author":1,"featured_media":124,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_sitemap_exclude":false,"_sitemap_priority":"","_sitemap_frequency":""},"categories":[2],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/152"}],"collection":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/comments?post=152"}],"version-history":[{"count":15,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/152\/revisions"}],"predecessor-version":[{"id":154,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/posts\/152\/revisions\/154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media\/124"}],"wp:attachment":[{"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/media?parent=152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/categories?post=152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/extendsclass.com\/blog\/wp-json\/wp\/v2\/tags?post=152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}