Home Programming PHP PHP Logic operators
PHP Logic operators PDF Print E-mail
Written by Javawocky   
Tuesday, 09 February 2010 21:08

Often new PHP programmers can get confused between how PHP treats data types.  PHP is loosely typed which means that you don't have to define a variable implicitly like you do with other languages like Java.

For example concider this code

$x = 1;
$x = "1";
$x = true;

if you id a logic test on this code PHP will automatically assume the data type you are using.  All the following if statements will return true.

if($x==1) ...
if($x=="1")..
if($x==true)... 

Now that's all well and good, but what if we are not sure where $x is coming from?  For example if you are writing an API function which can be called from anywhere.  The programmer calling could pass in all kinds of things, so what if we want our code to be tight and we want to check for an exact condition?  We would do something like this...

if($x===true)...

Now this statement will be true if and only if $x=true.

For more info on PHP operators, check out the API docs here

 
Copyright © 2012 sprocketandlube.com. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.