|  | Posted by Erwin Moller on 11/06/06 16:06 
brett wrote:
 >> if ((is_page()) || ($category->cat_ID === $cat) ) {
 >
 > What is the notation for equivalency?  Is it "==" or "==="?
 
 == is more loose than ===
 
 === also checks if the data compared is of the same type.
 eg
 
 $a = 1;
 $b = "1";
 if ($a == $b){
 // evaluates to true
 }
 
 if ($a === $b){
 // evaluates to false
 }
 
 This can be a very important extra = in some situations. :-)
 
 A common situation where === makes sense is a functioncall that can return 0
 as valid answer, eg:
 
 // This function returns the number of elements in an array
 // or it returns false if not an array is passed
 function countNumber($someArray){
 if is_array($someArray){
 return count($someArray);
 } else {
 return false;
 }
 }
 
 Now if you use this function and feed the following 3 variables to it:
 $test = array("bla",3,24);
 $result = countNumber($test);
 if ($result){
 echo "number of elements $result";
 } else {
 echo "Not an array!";
 }
 
 That works ok, it returns 3.
 
 suppose $test was:
 $test = 23;
 
 Which is also ok, it would respond that $test is not an array.
 
 Now the problem:
 $test = array();
 
 If you feed this, PHP will correctly count 0 elements, and thus return 0.
 
 However, when used as an expression, 0 evaluates to false, thus PHP will
 incorrectly say it was not an array.
 
 If you use the following construct, this will NOT happen.
 
 if ($result === true){
 echo "number of elements $result";
 } else {
 echo "Not an array!";
 }
 
 
 Regards,
 Erwin Moller
 
 
 >
 > Thanks,
 > Brett
  Navigation: [Reply to this message] |