<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: thiCha</title>
    <description>The latest articles on DEV Community by thiCha (@thicha0).</description>
    <link>https://dev.to/thicha0</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F430937%2Fb62a39a0-d22b-4c45-baa8-5b0299fdb9b5.jpg</url>
      <title>DEV Community: thiCha</title>
      <link>https://dev.to/thicha0</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/thicha0"/>
    <language>en</language>
    <item>
      <title>Pipe operator is coming to PHP !</title>
      <dc:creator>thiCha</dc:creator>
      <pubDate>Tue, 07 Oct 2025 18:32:12 +0000</pubDate>
      <link>https://dev.to/thicha0/pipe-operator-is-coming-to-php--2838</link>
      <guid>https://dev.to/thicha0/pipe-operator-is-coming-to-php--2838</guid>
      <description>&lt;p&gt;A new operator is coming to PHP: &lt;strong&gt;the pipe ! (|&amp;gt;)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Just like in linux, you will now be able to chain multiple commands together.&lt;/p&gt;

&lt;h3&gt;
  
  
  What it is
&lt;/h3&gt;

&lt;p&gt;This operator will help us make the code more &lt;strong&gt;readable and expressive.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;ucfirst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;strtolower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;str_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"HELLO_WORLD"&lt;/span&gt;&lt;span class="p"&gt;)));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the pipe operator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"HELLO_WORLD"&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;str_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;strtolower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;ucfirst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The other way to make the first example readable is to use a temporary variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"HELLO_WORLD"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;str_replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'_'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;' '&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;strtolower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;ucfirst&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pipe operator gives you clean style to chain calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to use it
&lt;/h3&gt;

&lt;p&gt;In PHP, we use callables so we have multiple ways to use it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;userland functions&lt;/li&gt;
&lt;li&gt;built-in functions&lt;/li&gt;
&lt;li&gt;static class methods&lt;/li&gt;
&lt;li&gt;lambda functions&lt;/li&gt;
&lt;li&gt;arrow-functions&lt;/li&gt;
&lt;li&gt;invokable classes&lt;/li&gt;
&lt;li&gt;first-class callables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s a demo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"42"&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;intval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$x&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$x&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Value: &lt;/span&gt;&lt;span class="nv"&gt;$x&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Transformer&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;MathHelper&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'addExclamation'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="o"&gt;|&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;format_output&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Value: 84!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The drawback
&lt;/h3&gt;

&lt;p&gt;“Perfect,” you might think — and almost!&lt;/p&gt;

&lt;p&gt;But there’s a small limitation.&lt;/p&gt;

&lt;p&gt;The pipe operator only works when the &lt;strong&gt;result of the previous&lt;/strong&gt;  can be passed as the &lt;strong&gt;first argument&lt;/strong&gt; of the next callable.&lt;br&gt;
So while the pipe operator makes code cleaner, it does have some &lt;strong&gt;functional-style constraints&lt;/strong&gt; you’ll need to keep in mind.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The pipe operator brings a fresh, functional touch to PHP — making our code flow as smoothly as our ideas.&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Tell Don't Ask principle explained</title>
      <dc:creator>thiCha</dc:creator>
      <pubDate>Thu, 26 Dec 2024 19:43:05 +0000</pubDate>
      <link>https://dev.to/thicha0/tell-dont-ask-principle-explained-1gbf</link>
      <guid>https://dev.to/thicha0/tell-dont-ask-principle-explained-1gbf</guid>
      <description>&lt;p&gt;The basic principle of the Tell Don't Ask principle is to prefer telling an object what to do rather than asking an object for its data and acting on this data.&lt;/p&gt;

&lt;p&gt;The idea is to put into the object the behavior related to that object.&lt;/p&gt;

&lt;p&gt;The object that owns a data is responsible of its own logic.&lt;/p&gt;

&lt;p&gt;The code is therefore more coherent within the codebase and within the object itself.&lt;/p&gt;

&lt;p&gt;Last but not least, the behavior is encapsulated in the object meaning that you can work with a minimal public interface outside of the object which makes it easier to maintain and modify.&lt;/p&gt;

</description>
      <category>learning</category>
      <category>php</category>
      <category>principles</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Yoda condition, in PHP, What is ?</title>
      <dc:creator>thiCha</dc:creator>
      <pubDate>Wed, 28 Aug 2024 17:22:13 +0000</pubDate>
      <link>https://dev.to/thicha0/yoda-condition-in-php-what-is--fgo</link>
      <guid>https://dev.to/thicha0/yoda-condition-in-php-what-is--fgo</guid>
      <description>&lt;p&gt;While scrolling through code you might come across something like this if (1 === $var).&lt;/p&gt;

&lt;p&gt;What could possibly be the difference between if ($var === 1) and if (1 === $var) ?&lt;/p&gt;

&lt;h3&gt;
  
  
  if ($var === 1)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This is how we learn the code because it you can write the same way you speak: if the variable $var is equal to 1 then do something.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  if (1 === $var)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;This is known as the “Yoda condition” from the famous Jedi Master speaking backwards.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It has to be known that there is no difference in functionality between the two approaches. So, why the hell would you use a Yoda condition when writing conditions ?&lt;/p&gt;

&lt;h3&gt;
  
  
  The only reason why the Yoda condition is used
&lt;/h3&gt;

&lt;p&gt;When writing an if statement, it may have happened that you missed some equal signs. The problem is when you end up with something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$var&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Instead of $var === 1&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens here ?&lt;/p&gt;

&lt;p&gt;No error is detected because this is code is valid. The problem is that it does not work like intended: the variable is &lt;em&gt;assigned&lt;/em&gt; to the value 1, no comparison is performed and it is always true.&lt;/p&gt;

&lt;p&gt;This is where the Yoda condition is useful; with the same case, we get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$var&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// Instead of 1 === $var&lt;/span&gt;
    &lt;span class="c1"&gt;// Do something&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here we also missed some equals but the code is not valid and, this time, we get an error since this is not valid.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We have seen that the sole purpose of the Yoda condition is to avoid accidental assignment which comes at the cost of less readeability.&lt;/p&gt;

&lt;p&gt;Should you use it ? Why not ! But keep in mind that this will not improve your code by any margin.&lt;/p&gt;

&lt;p&gt;Also it can help to flex at a party with devs (which does not occur that often by the way).&lt;/p&gt;

&lt;p&gt;For you reading, thank you ! 💚&lt;/p&gt;

</description>
      <category>yoda</category>
      <category>php</category>
      <category>learning</category>
    </item>
    <item>
      <title>Magic Strings &amp; Magic Numbers 🪄</title>
      <dc:creator>thiCha</dc:creator>
      <pubDate>Mon, 29 Jul 2024 21:39:40 +0000</pubDate>
      <link>https://dev.to/thicha0/magic-strings-magic-numbers-4fa4</link>
      <guid>https://dev.to/thicha0/magic-strings-magic-numbers-4fa4</guid>
      <description>&lt;p&gt;Magic numbers are literal numeric values that are used in code without any explanation of their meaning. Magic strings are strings literals without any explanation either.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why is it a problem ?
&lt;/h2&gt;

&lt;p&gt;First of all, it is not easy to read when there is only a number that is written in code. Let's look at an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$myModel = new Model();
$myModel-&amp;gt;status_id = 3; // A magic number
$myModel-&amp;gt;save();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here there is no explanation of the meaning behind the value "3". I can probably find the answer by looking at the code surrounding this snippet or look for a corresponding value elsewhere (like in a database table).&lt;/p&gt;




&lt;p&gt;Also the number "3" is not important in itself for my comprehension of the code. The status should correspond to a more realistic status of my business like "Created", "Sent", "Archived".&lt;/p&gt;




&lt;p&gt;The same applies to Magic Strings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;switch ($request-&amp;gt;code_response) {
  case "code_1": // Magic String
    doAnAction();
    break;
  case "code_2": // Also a Magic String
    doAnotherAction();
    break;
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I can probably guess the utility of these code values by looking at the code of the doAnAction() and doAnotherAction() functions but it would greatly help to have a code more readable at this stage.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to solve this problem ?
&lt;/h2&gt;

&lt;p&gt;Different solutions can be used. The simplest one is to use a named variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$statusCreated = 3;

$myModel = new Model();
$myModel-&amp;gt;status_id = $statusCreated;
$myModel-&amp;gt;save();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A better solution is to use constant values in a separate class or even an enum.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum MyModelStatuses {
...
  case CREATED = 3;
  case ARCHIVED = 4;
...
}

$myModel = new Model();
$myModel-&amp;gt;status_id = MyModelStatuses::CREATED-&amp;gt;value; // don't forget to import the new class
$myModel-&amp;gt;save();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A separate class allow you to have a global logic behind these values that can be reused elsewhere.&lt;/p&gt;




&lt;p&gt;Another solution when the Magic Number is only used in one class is to use a const variable in the class:&lt;br&gt;
From:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyClass {
  public function myFunction() {
    for ($i = 0; $i &amp;lt; 10; $i++) { // Here 10 is a Magic Number
      // Do something
    }
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MyClass {
  private const NUMBER_OF_ITERATIONS = 10;

  public function myFunction() {
    for ($i = 0; $i &amp;lt; self::NUMBER_OF_ITERATIONS; $i++) {
      // Do something
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Such constants are used as parameters inside your class and can be easily replaced by new values.&lt;/p&gt;




&lt;p&gt;Let's see a last use case of avoiding Magic Numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$myModel = new MyModel();
$myModel-&amp;gt;duration = 60; // Magic Number
$myModel-&amp;gt;save();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here the value 60 can be confusing. What is its unit ? It could me seconds, it could be minutes or even hours !&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$secondsInAMinute = 60;

$myModel = new MyModel();
$myModel-&amp;gt;duration = $secondsInAMinute;
$myModel-&amp;gt;save();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The name of the new variable itself helps to make the code readable. Also the same number could refer to different meanings (in this case we could have 60 as $minutesInAnHour somewhere else in the code).&lt;/p&gt;




&lt;p&gt;That's it ! I hope that this article helped you understand what are Magic Numbers (and Magic Strings) and how to remove them from your code !&lt;/p&gt;

</description>
      <category>php</category>
      <category>cleancode</category>
      <category>magic</category>
    </item>
    <item>
      <title>Spaceship operator 🚀 in PHP</title>
      <dc:creator>thiCha</dc:creator>
      <pubDate>Tue, 16 Jul 2024 11:00:23 +0000</pubDate>
      <link>https://dev.to/thicha0/spaceship-operator-in-php-5g0n</link>
      <guid>https://dev.to/thicha0/spaceship-operator-in-php-5g0n</guid>
      <description>&lt;p&gt;Introduced in PHP 7, the Spaceship operator &amp;lt;=&amp;gt; is used to compare two expressions.&lt;/p&gt;

&lt;p&gt;It returns:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;0 if both values are equal&lt;/li&gt;
&lt;li&gt;1 if left operand is greater&lt;/li&gt;
&lt;li&gt;-1 if right operand is greater&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s see it with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo 2 &amp;lt;=&amp;gt; 2; // Outputs 0
echo 3 &amp;lt;=&amp;gt; 1; // Outputs 1
echo 1 &amp;lt;=&amp;gt; 3; // Outputs -1

echo "b" &amp;lt;=&amp;gt; "b"; // Outputs  0
echo "a" &amp;lt;=&amp;gt; "c"; // Outputs -1
echo "c" &amp;lt;=&amp;gt; "a"; // Outputs 1

// Note: for string comparison, the ASCII value of the characters are used, that is why "c" is greater than "a"
echo "ping" &amp;lt;=&amp;gt; "pong"; // Outputs -1 since "o" is greater than "i"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This operator can be used for sorting arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$numbers = [1, 4, 5, 9, 2, 3];

usort($numbers, function ($a, $b) {
    return $a &amp;lt;=&amp;gt; $b; // Sort in ascending order
});

echo print_r($numbers); // Outputs [1,2,3,4,5,9]

usort($numbers, function ($a, $b) {
    return $b &amp;lt;=&amp;gt; $a; // Sort in descending order
});

echo print_r($numbers); // [9,5,4,3,2,1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>php</category>
      <category>operator</category>
    </item>
    <item>
      <title>Elvis operator ?: vs Null coalescing operator</title>
      <dc:creator>thiCha</dc:creator>
      <pubDate>Thu, 30 May 2024 14:59:17 +0000</pubDate>
      <link>https://dev.to/thicha0/elvis-operator-vs-null-coalescing-operator-2l31</link>
      <guid>https://dev.to/thicha0/elvis-operator-vs-null-coalescing-operator-2l31</guid>
      <description>&lt;p&gt;The Elvis operator and the Null coalescing operator are both binary operators that allow you to evaluate an expression/variable and define a default value when the expression/variable is not available.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Elvis operator
&lt;/h2&gt;

&lt;p&gt;The Elvis operator is used to return a default value when the given operand is &lt;em&gt;false&lt;/em&gt;.&lt;br&gt;
Its name comes from the resemblance of the notation ?: with the hairstyle of the famous singer.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc7ib0wj59iajkt29oir.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frc7ib0wj59iajkt29oir.png" alt=" " width="225" height="225"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Usage in PHP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$variable = [];
$result = $variable ?: 'default value';
echo $result; // Outputs: default value (since an empty array is considered falsy)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Equivalent with a Ternary condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$variable = [];
$result = $result ? $result : 'default value';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The Null coalescing operator
&lt;/h2&gt;

&lt;p&gt;Quite similar to the Elvis operator, the null coalescing operator returns a default value when the given operand is &lt;em&gt;null&lt;/em&gt; or &lt;em&gt;undefined&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Usage in PHP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$variable = [];
$result = $variable ?? 'default value';
echo $result; // Outputs: [] (since an empty array is not null)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Equivalent with a Ternary condition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$variable = [];
$result = isset($result) ? $result : 'default value';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>php</category>
      <category>operator</category>
    </item>
  </channel>
</rss>
