Tools, FAQ, Tutorials:
Converting Strings to Numbers in PHP
How To Convert Strings to Numbers?
✍: FYIcenter.com
In a numeric context, PHP will automatically convert any string to a numeric value. Strings will be converted into two types of numeric values, double floating number and integer, based on the following rules:
Here is a PHP script example of converting some examples:
<?php $foo = 1 + "10.5"; echo "\$foo=$foo; type is " . gettype ($foo) . "\n"; $foo = 1 + "-1.3e3"; echo "\$foo=$foo; type is " . gettype ($foo) . "\n"; $foo = 1 + "bob-1.3e3"; echo "\$foo=$foo; type is " . gettype ($foo) . "\n"; $foo = 1 + "bob3"; echo "\$foo=$foo; type is " . gettype ($foo) . "\n"; $foo = 1 + "10 Small Pigs"; echo "\$foo=$foo; type is " . gettype ($foo) . "\n"; $foo = 4 + "10.2 Little Piggies"; echo "\$foo=$foo; type is " . gettype ($foo) . "\n"; $foo = "10.0 pigs " + 1; echo "\$foo=$foo; type is " . gettype ($foo) . "\n"; $foo = "10.0 pigs " + 1.0; echo "\$foo=$foo; type is " . gettype ($foo) . "\n"; ?>
This script will print:
$foo=11.5; type is double $foo=-1299; type is double $foo=1; type is integer $foo=1; type is integer $foo=11; type is integer $foo=14.2; type is double $foo=11; type is double $foo=11; type is double
⇒ PHP Built-in Functions for Strings
⇐ Converting Numbers to Strings in PHP
2016-10-13, 1768🔥, 0💬
Popular Posts:
How to decode the id_token value received from Google OpenID Connect authentication response? Accord...
How to convert a JSON text string to an XML document with PHP language? Currently, there is no built...
Where to see some Examples of Invalid JSON Values? Here are some Examples of Invalid JSON Values: 1....
How To Move Uploaded Files To Permanent Directory in PHP? PHP stores uploaded files in a temporary d...
Can Two Forms Be Nested? Can two forms be nested? The answer is no and yes: No. You can not nest two...