DEV Community

Cover image for Character misplacing making fun bugs in PHP
Debuqer
Debuqer

Posted on

Character misplacing making fun bugs in PHP

During this years of my career as a PHP developer I have confront some bugs that of course most of them are not php bugs.
You may spent too much time on a snippet that looks well but you don't get your expected result, at last you find you missed a little thing somewhere.
Here is a list of some facts about php that made me more careful when I work.

1. The statement of if always running, no matter condition result is true or not

$age= 17;

if( $age > 18 ); {
    echo you can get a license;
}

Enter fullscreen mode Exit fullscreen mode

You may think this guy is not allowed to get a license but you are wrong.
PHP will understand this snippet like this:

$a = 17;

if( $a > 18 ) {

}

{
    echo you can get a license;
}

Enter fullscreen mode Exit fullscreen mode

That’s because semi-colon will close your if block while it has empty statement.

2. Although I have definitely false condition; true will be return


function should_wear_a_hat($is_raining, $is_snow_comming) 
{
        $wear_a_hat = $is_raining and $is_snow_comming;

        return $wear_a_hat;
}
Enter fullscreen mode Exit fullscreen mode

Why my code tells me not wear a hat despite the fact that there is snow out there.
In php, assignment(=) has a higher priority than logical operator(and); so php thinks you have a code like this:

function should_wear_a_hat($is_raining, $is_snow_comming) 
{
        ($wear_hat = $is_raining) and $is_snow_comming;

        return $wear_hat;
}
Enter fullscreen mode Exit fullscreen mode

In order to solve this problem use brackets to change the priorities.

$wear_a_hat = ($is_raining and $is_snow_comming);
Enter fullscreen mode Exit fullscreen mode

or simply use return that has a less priority than (and)

return $is_raining and $is_snow_comming;
Enter fullscreen mode Exit fullscreen mode

3. I have a string that’s not visible

$text = `i am 18 y/o`;

echo $text;
Enter fullscreen mode Exit fullscreen mode

That’s odd but it happens. If you used Backtick instead of single quote(') php will be ready for a shell command and not a string.

** 4. Idiot php thinks result of (1 and 4) is false **

$number_of_wheels = 4;
$number_of_engine = 1;

$is_car_ready = ($number_of_wheels & $number_of_engine);
Enter fullscreen mode Exit fullscreen mode

You are ready to check if your car is ready or not, but unfortunately it’s not
If you miss a (&) php will evaluate your condition in binary system, despite of logical comparison in binary (4 & 1 = 0 )

$number_of_wheels  = 4; // 100
$number_of_engine = 1; // 001

echo ($number_of_wheels & $number_of_engine) // 100 & 001 = 000 

Enter fullscreen mode Exit fullscreen mode

5. There is two different number, but php says they are equal

$a = 2.0;
$b = 2.0000000000000001;

echo $a == $b; 
Enter fullscreen mode Exit fullscreen mode

You can’t be more accurate than php limit, so be careful when using this kind of checking.

6. Php will say my fruits array has the tomato but I know it does n’t.

$fruits = [
     'apple' => 1,
     'banana' => 0,
     'cherry' => 1,
];


echo in_array('tomato', $fruits);

Enter fullscreen mode Exit fullscreen mode

php will go through your array and loose check ‘tomato’ for each value

tomato == 1 // yes
tomato == 0 // no
tomato == 1 // yes
Enter fullscreen mode Exit fullscreen mode

So actually you have tomato in your array, to resolve this problem just pass the third parameter that makes php to check in strict

$fruits = [
    'apple' => 1,
    'banana' => 0,
    'cherry' => 1,
];


echo in_array('tomato', $fruits, true);

Enter fullscreen mode Exit fullscreen mode

Conclusion
Some of these were nerve racking mistakes that happening when simple characters are missing.
Handling them is possible with getting more deeper in php mechanism and writing tests for your codes.

Top comments (0)