Things to Watch Out For with if in PHP

Recently I happened to discover that PHP’s if has an automatically handled parameter for comparisons

For example

  1. $a=’adasds’; if($a==0){ echo “GGGG”; }else{ echo “KKKK”; } The result is GGGG. This is because when if evaluates $a it checks whether there is a number at the very start of the string; if there is no number it returns 0, so the result is GGGG
  2. $a=’55adasds’; if($a==55){ echo “GGGG”; }else{ echo “KKKK”; } The result is GGGG. The if comparison checks whether there is a number at the beginning of $a; if there is a number it returns that number, so the result is GGGG
  3. $a=’ad55asds’; or $a=’adasds55’; if($a==55){ echo “GGGG”; }else{ echo “KKKK”; } The result is KKKK, which shows that when if evaluates $a it does not check whether there is a number in the middle or at the end, and the result simply returns 0; In summary, with if comparisons, if the variable you define is an integer this problem won’t come up, but if it is a string you have to take the situations in examples 1 and 2 into account.