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, 1559🔥, 0💬
Popular Posts:
How to use the "send-request" Policy statement to call an extra web service for an Azure API service...
How to run CMD Commands in Dockerfile to change Windows Docker images? When building a new Windows i...
How to attach console to a Running Container using the "docker container exec" command? I want to ge...
How to create the Hello-3.0.epub package? I have all required files to create Hello-3.0.epub. To cre...
How To Open Standard Output as a File Handle in PHP? If you want to open the standard output as a fi...