Character misplacing making fun bugs in PHP
Tags: php
Published: 2022-07-15 10:46:01

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**

```php
$age= 17;

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

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

```php
$a = 17;

if( $a > 18 ) {

}

{
	echo ‘you can get a license’;
}

```
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**


```php

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

        return $wear_a_hat;
}
```
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:

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

        return $wear_hat;
}
```
In order to solve this problem use brackets to change the priorities.

```php
$wear_a_hat = ($is_raining and $is_snow_comming);
```
or simply use return that has a less priority than (and) 

```php
return $is_raining and $is_snow_comming;
```


**3. I have a string that’s not visible**

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

echo $text;
```

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 **

```php
$number_of_wheels = 4;
$number_of_engine = 1;

$is_car_ready = ($number_of_wheels & $number_of_engine);
```

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 )

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

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

```

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

```php
$a = 2.0;
$b = 2.0000000000000001;

echo $a == $b; 
```
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.**

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


echo in_array('tomato', $fruits);

```
php will go through your array and loose check ‘tomato’ for each value
```php
‘tomato’ == 1 // yes
‘tomato’ == 0 // no
‘tomato’ == 1 // yes
```

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


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


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

```


**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.