2013-02-10

PHP has got triple equals!

It's a very interesting question, that in PHP, there are three different use of the '=' sign, in PHP conditional statements. The first and most intuitive, you can simply write, by using the easiest conditional statement - 'if', I think. This is the syntax for statement 'if':

                                                 <?php

                                                 if() {
                                                       // do something
                                                 } else {
                                                      //do something else
                                                 }

                                                 ?>


So we have here the syntax. Now, let's try to write something in PHP. For example:
                                                 <?php
                                                 $day='Monday';

                                                 if($day=='Monday') {
                                                          echo 'Day equal Monday!';
                                                 } elseif($day==='123') {
                                                          echo 'Type and Values are the same!';
                                                 } else {
                                                          echo 'Nothing here match!';
                                                 }
                                                 ?>

As you can see, we used here sign 5 times! The first use of a single character '=', means that we are signing the value to the variable ($day='Monday';). We are setting day equal Monday. At the second case, we are using a double equals ($day=='Monday';), because we are running a test: Does day equal Monday? So we are not signing anything to the variable but we are testing whether the left side equal the right side. Few programmers know, that there is also triple equals ($day==='Monday';). If we are using it, we are whether two variables are equal and also the same type. In other words, it is a comparison to see if they are both integers or both are strings. So cute<3


Brak komentarzy:

Prześlij komentarz

Dziękuję Ci za poświęcony czas i pozostawienie komentarza :)