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'
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.
– KingRider
@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.
– Bacco
Yes exact Bacco, each different language, always make it mandatory to convert from operator. But another type php PDO ai has more things different.
– KingRider