Why in PHP does the expression "2 + '6 apples'" equal 8?

Asked

Viewed 1,510 times

55

I found the example funny, but the question is valid in the sense of understanding why PHP behaves this way.

When we do the following example sum (an integer added to a string):

$numero_macas = 2 + '6 maçãs';

The result is:

8

But in that case, it returns only 2:

2 + 'seis(6) maçãs'

Why the PHP interpreter behaves this way?

  • So if this adding up with string and not numeral, is leaving the result is bytes, each letter is 1 byte, and count Qt has string ai is 8, and its name Wallace has 7 bytes.

  • 3

    @Kingrider is actually something else. PHP makes an implicit cast, similar to val( ) in many languages. It reads the string digit by digit, interpreting the numeric value, and stops when finding a character not valid for number. Later the value obtained is used in the sum.

  • Yes exact Bacco, each different language, always make it mandatory to convert from operator. But another type php PDO ai has more things different.

4 answers

64


The PHP interpretation engine works as follows, if the first part of the string is a valid number it is converted(int or float) and the rest is discarded, except for a few exceptions as the sign of more (+), least(-), dot(.) and scientific notations (e) and hexadecimal (x), seems to follow the same pattern of filter_sanitize for numbers.

In the first example, after the 6 everything is not considered a valid number.

$numero_macas = 2 + '6 maçãs';
                      ^
                      |
a partir daqui acabou o número

Curious examples:

<?php
//10 em notação cientifica, resultado 12
echo 2 + '10eNaN abacaxis'. PHP_EOL;

//26 em notação hexadecimal, resultado 28
echo  2 + '0x1A abacaxis'. PHP_EOL;

//sinal de menos, resultado -1
echo 2 + '-3 abacaxis'. PHP_EOL;

//sinal de mais, resultado -2
echo -5 + '+3 abacaxis'. PHP_EOL;

//sinal de menos com ponto, resultado 4.9
echo 5 + '-.1 abacaxis'. PHP_EOL;

//sinal de mais seguido de ponto, resultado -4.9
echo -5 + '+.1 abacaxis'. PHP_EOL;

//sinal de ponto, resultado 3.1
echo 3 + '.1 abacaxis'. PHP_EOL;

Example - ideone

In some cases a number followed by a letter may be a valid number as the scientific notation, that’s a question very interesting on the subject.

If the string starts with a value that is not numerical it will not be converted.

2 + 'seis(6) maçãs'

23

Because PHP tries to do a type coercion on operations of different types.

When he tries to convert the first one, he finds a digit in the text and can convert it to number, after he finds characters that are not digits (or valid numeric symbol in position) he discards all the rest.

In the second case it already finds invalid characters face and discards everything, bearing the number found in the text is 0.

The ideal is not to trust data string for automatic conversion. It is better to validate before doing operation.

6

The interpreter automatically converts the string started by 6 in the number 6.66 and '66' and '66 abacates' shall all be interpreted, in numerical instruction, as 66.

4

This is because PHP creates a cast automatic string for int or float

Let’s take this example:

echo (float) '120.5 por cento de aumento da conta de luz';

Upshot

120.5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.