Why Does PHP Convert a String with the Letter E into a Number

Why does PHP convert a string with the letter E into a number?

"608E-4234" is the float number format, so they will cast into number when they compares.

608E-4234 and 272E-3063 will both be float(0) because they are too small.

For == in php,

If you compare a number with a string or the comparison involves
numerical strings, then each string is converted to a number and the
comparison performed numerically.

http://php.net/manual/en/language.operators.comparison.php

Attention:

What about the behavior in javascript which also has both == and ===?

The answer is the behavior is different from PHP. In javascript, if you compare two value with same type, == is just same as ===, so type cast won't happen for compare with two same type values.

In javascript:

608E-4234 == 272E-3063 // true
608E-4234 == "272E-3063" // true
"608E-4234" == 272E-3063 // true
"608E-4234" == "272E-3063" // false (Note: this is different form PHP)

So in javascript, when you know the type of the result, you could use == instead of === to save one character.

For example, typeof operator always returns a string, so you could just use

typeof foo == 'string' instead of typeof foo === 'string' with no harm.

php: converting number to alphabet and vice versa

I don't understand at all the logic you're trying to use in that function. What you're trying to do seems very strange (why does 'a' map to zero and yet 'aa' maps to 26?), but this appears to work. (You will want to use some more test cases, I only checked that it gives the correct output for the case 'aba'.)

function toNum($data) {
$alphabet = array( 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y',
'z'
);
$alpha_flip = array_flip($alphabet);
$return_value = -1;
$length = strlen($data);
for ($i = 0; $i < $length; $i++) {
$return_value +=
($alpha_flip[$data[$i]] + 1) * pow(26, ($length - $i - 1));
}
return $return_value;
}

Why is the answer 15 here?

From php.net:

When a string is evaluated in a numeric context, the resulting value
and type are determined as follows.

If the string does not contain any of the characters '.', 'e', or 'E'
and the numeric value fits into integer type limits (as defined by
PHP_INT_MAX), the string will be evaluated as an integer. In all other
cases it will be evaluated as a float.

The value is given by the initial portion of the string. If the string
starts with valid numeric data, this will be the value used.
Otherwise, the value will be 0 (zero). Valid numeric data is an
optional sign, followed by one or more digits (optionally containing a
decimal point), followed by an optional exponent. The exponent is an
'e' or 'E' followed by one or more digits.

Literally:

  1. $a + $b means numeric + numeric.
  2. "5 USD" starts with a valid numeric data, so PHP converts it into 5.
  3. "USD 5" or "U5SD" starts with not valid numeric data, so PHP converts it into 0.

UPDv1:

<?php
header('Content-Type: text/plain');

function plus($a, $b){
echo $a, ' + ', $b, ' = ', $a + $b, PHP_EOL;
}

plus('5 frogs', 3); // 5 + 3 = 8
plus('frogs: 5', 3); // 0 + 3 = 3
plus('f5rogs', 3); // 0 (not HEX) + 3 = 3
plus('0xF5', 3); // 245 (HEX) + 3 = 248
plus('0011b', 3); // 11 (not BIN) + 3 = 14
plus('1E5', '1.2xx'); // 100000 (FLOAT) + 1.2 (FLOAT) = 100001.2
plus('true', 2); // 0 (not BOOL) + 2 = 2
?>

Also, check out this: php string number concatenation messed up.


UPDv2:

There is "no way" for PHP to typecast string value to octal, regardless of zero-fill effect. Still, as mentioned before, PHP able to typecast string to hexadecimal.

<?php
header('Content-Type: text/plain');

function plus($a, $b){
echo $a, ' + ', $b, ' = ', $a + $b, PHP_EOL;
}

plus(008, 12); // Invalid octal, PHP assumes it is 0. Result: 12.
plus('008', 12); // Invalid octal? No, it is decimal. Result: 20.
plus(0x0F, 1); // Valid hexadecimal. Result: 16.
plus('0x0F', 1); // Valid hexadecimal. Result: 16.
plus('0x0X', 1); // Invalid hexadecimal, PHP assumes it is 0. Result: 1.
?>

It is not mentioned in "string to number conversion" docs.

PHP how to convert number exponential number to string?

printf and friends will do this:

<?php
$awb = 2.01421700079E+14;
$str = sprintf("%d", $awb);
var_dump($str);

Output:

string(15) "201421700079000"

There obviously isn't enough information in your original number to get any more precision than that.

PHP Convert a string primary key to a number representation

Not really sure what you are trying to achieve, but using the examples you provided, you can simply use :

str_replace(range('A', 'Z'), range(1, 26), Syour_id)

B0AL02087 will return: 2011202087

BBAL02087 will return: 2211202087

Translate non-digital characters to specific digits

Create an array, and insert a space to the first element. Then use range() to generate an array with a to z. Use strtolower() to force the input to lowercase (as the characters from range() we generate is lowercase too.

Then do a replacement with str_replace(), which accepts arrays as values. The keys is the value that the value will be replaced with.

function convert_to_int($string) {;
$characters = array_merge([' '], range('a', 'z'));
return str_replace(array_values($characters), array_keys($characters), $string);
}
  • Live demo at https://3v4l.org/cHZap

PHP: add hyphen anytime a string contains a number after one letter or more

function convert_input($string) {
$arr = explode(' ', $string);
if (count($arr) == 1) {
$first = '';
$second = strtoupper($string);
} else {
$first = $arr[0].' ';
$second = strtoupper($arr[1]);
}
$second = str_replace('-', '', $second);
$p = strcspn($second, '0123456789');
$letters = substr($second, 0, $p);
$numbers = substr($second, $p);
$glue = $letters && $numbers ? '-' : '';
return $first.$letters.$glue.$numbers;
}

echo convert_input('mm234');
echo convert_input('Carl Mm345');
echo convert_input('adsf mmmm');
echo convert_input('adsf');
echo convert_input('adsfdsf 123');
echo convert_input('Banana B-234');

Comparing int to string causes weird results in php?

This is discussed in the PHP Manual.

String conversion to numbers

When a string is evaluated in a numeric context, the resulting value
and type are determined as follows.

The string will be evaluated as a float if it contains any of the
characters '.', 'e', or 'E'. Otherwise, it will be evaluated as an
integer.

The value is given by the initial portion of the string. If the string
starts with valid numeric data, this will be the value used.
Otherwise, the value will be 0 (zero). Valid numeric data is an
optional sign, followed by one or more digits (optionally containing a
decimal point), followed by an optional exponent. The exponent is an
'e' or 'E' followed by one or more digits.

Note the part that states

If the string starts with valid numeric data, this will be the value
used.

Since your string starts with 32 PHP will compare if(32 == 32) which will be true.

Use type safe checks, that takes the datatype into consideration, when dealing with types that could be different if this behaviour is not desired. Like

1 === 1:   true
1 == 1: true
1 === "1": false
1 == "1": true
"foo" === "foo": true


Related Topics



Leave a reply



Submit