<?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>Forem: Nafis Nahiyan</title>
    <description>The latest articles on Forem by Nafis Nahiyan (@nahi3an).</description>
    <link>https://forem.com/nahi3an</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%2F621971%2Fcfb31f71-278f-41f7-b0a2-4593b4a8e5c0.jpeg</url>
      <title>Forem: Nafis Nahiyan</title>
      <link>https://forem.com/nahi3an</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nahi3an"/>
    <language>en</language>
    <item>
      <title>🛑 Back to the Basics: WHY FALSE NEVER GETS PRINTED IN PHP ❓❓</title>
      <dc:creator>Nafis Nahiyan</dc:creator>
      <pubDate>Tue, 15 Aug 2023 07:34:50 +0000</pubDate>
      <link>https://forem.com/nahi3an/back-to-the-basics-why-false-never-gets-printed-in-php-4lnd</link>
      <guid>https://forem.com/nahi3an/back-to-the-basics-why-false-never-gets-printed-in-php-4lnd</guid>
      <description>&lt;p&gt;❗Boolean in all languages means a data type that can either be &lt;strong&gt;true&lt;/strong&gt; or &lt;strong&gt;false&lt;/strong&gt;. But in PHP, boolean has a mysterious behavior. Before getting into that, let's talk about &lt;strong&gt;true&lt;/strong&gt; and &lt;strong&gt;false&lt;/strong&gt; in PHP.&lt;/p&gt;

&lt;p&gt;❗In PHP, there are a few values that are by default false. They are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;false: The defined false value itself&lt;/li&gt;
&lt;li&gt;null: The defined null value in PHP&lt;/li&gt;
&lt;li&gt;0 and -0: Positive and negative integer values of 0&lt;/li&gt;
&lt;li&gt;0.00 and -0.00: Positive and negative float values of 0&lt;/li&gt;
&lt;li&gt;"": empty string&lt;/li&gt;
&lt;li&gt;[ ]: Empty array&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;❗Any other values except these are considered &lt;strong&gt;true&lt;/strong&gt; in PHP. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# This is an empty string value
$value = "";

# If we check it with is_bool()  
echo is_bool($value);
# we will get 1 or true 

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

&lt;/div&gt;



&lt;p&gt;❗On the other hand&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# This is a string 
$value = "false";

# If we check it with is_bool()
echo is_bool($value);
# we will get no output or false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;❗Now this brings us to the main  question: &lt;strong&gt;WHY FALSE NEVER GETS PRINTED IN PHP?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Often, when we try to print a false value with &lt;strong&gt;echo&lt;/strong&gt; or &lt;strong&gt;print&lt;/strong&gt;, it prints nothing, no output on screen. But why?&lt;/p&gt;

&lt;p&gt;❗For this, we have to understand &lt;strong&gt;the printing mechanism in PHP&lt;/strong&gt;. While printing, both &lt;strong&gt;echo&lt;/strong&gt; and &lt;strong&gt;print&lt;/strong&gt; converts or typecasts everything into a string. That is why when we try to print an array with any of them, it gives us a conversion error because it can’t convert an array to a string. But for boolean values, they can do this.&lt;/p&gt;

&lt;p&gt;❗Both &lt;strong&gt;echo&lt;/strong&gt; and &lt;strong&gt;print&lt;/strong&gt; type casts a true boolean value into the string value "1" and a false boolean value into the string value "" which is an empty string and then prints the value. Let's test this 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;$test = true;
# Type casting the value into a string
$typeCasted = (string)$test;

# var_dump() displays the detailed information # about a variable 

var_dump($typeCasted);
# we get string(1) "" : which means 
# this is a string with 1 character
# and the value is "1" 

echo $test 
# this does the type casting 
# to convert into string
# and prints the value "1"

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

&lt;/div&gt;



&lt;p&gt;On the other hand for false&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$test = false;

# Type casting the value into string
$typeCasted = (string)$test;


var_dump($typeCasted);
# we get string(0) "" : which means 
# this is a string with 0 character
# and the value is empty string or ""

echo $test 
# echo does the type casting 
# to convert into string 
# and  prints the value ""

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

&lt;/div&gt;



&lt;p&gt;❗So this is why printing boolean true gives us a "1" string value, and printing false gives us a "" or empty string value. Thank you for reading.&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>softwaredevelopment</category>
    </item>
    <item>
      <title>Back to the Basics: print vs echo 🤜 🤛</title>
      <dc:creator>Nafis Nahiyan</dc:creator>
      <pubDate>Sat, 12 Aug 2023 07:03:26 +0000</pubDate>
      <link>https://forem.com/nahi3an/back-to-the-basics-print-vs-echo-528k</link>
      <guid>https://forem.com/nahi3an/back-to-the-basics-print-vs-echo-528k</guid>
      <description>&lt;p&gt;print &amp;amp; echo or print() &amp;amp; echo() however you write them, are both used in php for the same purpose, printing out a value (int, bool, float, string &amp;amp; null). Though they both serve the same purpose there are some key differences between them. Let's explore them with an example:&lt;/p&gt;

&lt;p&gt;📌 Print returns an integer value of 1 but echo returns nothing or void. &lt;br&gt;
For example, if we do this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$name = "Oppenheimer";

$res = print $name . "\n"; # ✅ adding a new line for more readability

var_dump($res); # ✅ this gives us int(1) , which means print return an integer data type &amp;amp; value is 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But when we try  this there is a problem :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$res = echo $name . "\n"; # 🚨 this gives full-on error: Parse error: syntax error, unexpected token "echo" because the return type of echo is void

Now if we want we can use the return value of print if we want but this will not come in handy for a real-life example. For the sake of example let's do this:

$quantity = 5;
$price = 10;

$total = $quantity * $price + print "The price is: {$price} \n"; # ✅ first print executes its printing command then it returns 1 which is added to the total value

echo "The total is: {$total}"; # so this gives us The total is 51
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📌 Echo takes multiple parameters but print does not do this. For example, let's see this code :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$firstName = "John";
$lastName = "Doe";
$age = 30;

echo "Name:", $firstName, " ", $lastName, ", Age:", $age; # ✅ passing multiple parameters to echo function separated by comma ( , )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But when we try  this there is a problem :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print "Name:", $firstName, " ", $lastName, ", Age:", $age; #🚨 PHP Parse error: syntax error, unexpected token "," because the print function cannot take multiple parameters
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;📌 Lastly echo is slightly faster than print because it does not return anything&lt;/p&gt;

&lt;p&gt;So these are some main differences between print and echo in the world of PHP. Thank you for reading.&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>programming</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
