PHP

Topic: String

How To Convert Strings to Numbers?

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:    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).    If the valid numeric data contains '.', 'e', or 'E', it will be converted to a double floating number. Otherwise, it will be converted to an integer.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

Browse random answers:

How To Count the Number of Characters in a String?
How Many ways to Remove Leading and Trailing White Spaces?
How To Remove Leading and Trailing Spaces from User Input Values?
How to Find a Substring from a Given String?
What Is the Best Way to Test the strpos() Return Value?
How To Take a Substring from a Given String?
How To Replace a Substring in a Given String?
How To Reformat a Paragraph of Text?
How To Convert Strings to Upper or Lower Cases?
How To Convert Leading Characters to Upper Case?
How To Compare Two Strings with strcmp()?
How To Convert Strings to Hex Numbers?
How To Generate a Character from an ASCII Value?
How To Convert a Character to an ASCII Value?
How To Split a String into Pieces?
How To Join Multiple Strings into a Single String?
How To Replace a Group of Characters by Another Group?
How Many Escape Sequences Are Recognized in Single-Quoted Strings?
Can You Specify the "new line" Character in Single-Quoted Strings?
What Are the Special Characters You Need to Escape in Double-Quoted Stings?
How To Include Variables in Double-Quoted Strings?
How Many Ways to Include Array Elements in Double-Quoted Strings?
How To Access a Specific Character in a String?
How To Assigning a New Character in a String?
How To Concatenate Two Strings Together?
How To Compare Two Strings with Comparison Operators?
How To Convert Strings to Numbers?
How To Count the Number of Characters in a String?
How To Remove the New Line Character from the End of a Text Line?
How To Remove Leading and Trailing Spaces from User Input Values?
How to Find a Substring from a Given String?
How To Remove the New Line Character from the End of a Text Line?
How to Find a Substring from a Given String?
How To Take a Substring from a Given String?
How To Replace a Substring in a Given String?
How To Reformat a Paragraph of Text?