📜 ⬆️ ⬇️

3 facts about PHP that you might not know

There is no secret for any web developer that PHP is a simple, flexible and non-demanding language. But when working with this language, you may encounter unexpected things. In this article, I will present the “weird facts” and explain why PHP gives such results.

Floating point inaccuracies


Most of you probably know that floating point numbers cannot really represent all real numbers. In addition, some operations between two seemingly well-given numbers can lead to unexpected situations. This is because the accuracy with which a computer stores numbers has its own characteristics. This phenomenon affects not only PHP, but all programming languages. The inaccuracy in floating-point operations gave the programmers considerable headaches from the day the discipline was founded as such.

This fact has already been written more than once. One of the most important articles is “What every geek needs to know about floating point operations.” If you have never read it, I strongly recommend that you correct this situation.

Let's look at this little piece of code:
')
<?php<br/>
echo (int) ((0.1 + 0.7) * 10);


What do you think the result will be? If you assume that the result of the operation will be 8, then you will be mistaken. Actually 7. For those who have a Zend certificate, this example is already known. By the way, you can find this example in the Zend Certification Preparation Guide.

Now let's see why this happens:

0.1 + 0.7 = 0.79999999999

The result of this operation is stored in reality as 0.79999999999, not 0.8, as one might think. This is where the problems begin.

The second operation we perform:

0.79999999 * 10 = 7.999999999

This operation works as expected, but the problems remain.

Finally, the third and last operation:

(int) 7.9999999 = 7

This expression uses an explicit type conversion. When the value is cast to an int, PHP cuts off the fractional part and eventually returns 7.



How PHP "increments" strings


During operation, we always use increment / decrement operations similar to the data:

<?php

$a = 5;

$b = 6;

$a++;

++$b;


Each of us easily understands what is happening here. But try to figure out what this code will display:

<?php

$a = 1;

$b = 3;

echo $a++ + $b;

echo $a + ++$b;

echo ++$a + $b++;


Let's get a look:

4

6

7


Not so difficult, right? Now let's increase the difficulty a bit. Have you ever tried to increment lines before? Try to assume that this code will output:

<?php

$a = 'fact_2';

echo ++$a;

$a = '2nd_fact';

echo ++$a;

$a = 'a_fact';

echo ++$a;

$a = 'a_fact?';

echo ++$a;

This task is more difficult. Let's see what we got:
fact_3

2nd_facu

a_facu

a_fact?


Surprised? Making the increment of the line that ends by a digit, we will actually increment the character (next character alphabetically, ie, after t follows u). Regardless of whether the string starts with a digit or not, the last character will be changed. However, this operation does not make any sense in the case where the string ends with a non-alphanumeric character.

This point is well described in the official documentation for increment / decrement operations, but many did not read this material because they did not expect to find anything special there. I want to admit that until recently I was thinking the same way.

Mystery of meanings


You are an array wizard in PHP. Feel free to do this. You already know everything about creating, editing and deleting arrays. However, the following example may surprise you.

Very often when working with arrays you have to look for something in them. PHP has a special function for this in_array (). Let's see it in action:

<?php

$array = array(

'isReady' => false,

'isPHP' => true,

'isStrange' => true

);

var_dump(in_array('phptime.ru', $array));


What should be displayed?

true

Isn't that a bit strange? We have an associative array that contains only Bulenov values, and when we perform a string search, we get true. Is this really magic? Let's see another example:

<?php

$array = array(

'count' => 1,

'references' => 0,

'ghosts' => 1

);

var_dump(in_array('aurelio', $array));


And what do we get?

true

And again in_array () returned true. How is this possible?

You have just used one of the favorite and at the same time hated PHP functions. I must say that PHP is not a strictly typed language. Many problems occur precisely because of this. In fact, all of the following values, if PHP compares them, are identical when using a single comparison operator:

0

false

""

"0"

null

array()




By default, in_array () uses a "flexible" comparison, therefore a non-empty ("") and non-zero string ("0") is equivalent to true, the same applies to all non-zero elements (eg 1). Therefore, in our first example, we got true, because 'phpmaster.com' == true, while in the second example, 'aurelio' == 1.

To solve this problem, you must use the third additional parameter in the in_array () function, which allows strict comparison of elements. If we now write:

<?php

$array = array(

'count' => 1,

'references' => 0,

'ghosts' => 1

);

var_dump(in_array('aurelio', $array, true));

we get false.

Conclusion


In this article you have seen the strange and unexpected behavior of the PHP interpreter. Here's what you could learn from the reading:



If you are an advanced programmer, then, most likely, you already knew about the existence of these oddities, but repetition is never useless.

Source: https://habr.com/ru/post/226861/


All Articles