Nadia S.
—How do I convert a string to a number in PHP?
We’ll take a look at four ways to convert numeric strings into numbers in PHP:
intval()
and floatval()
functions.(int)
and (float)
.settype()
function.PHP is a dynamically typed language that recognizes numerical strings. You can perform mathematical operations directly with numeric strings. When we add 5
to '3.14'
, we get a float of 8.14
as a result:
$string = '3.14'; $mySum = $string + 5; var_dump($mySum);
Output:
// we get a float of 8.14 float(8.14)
intval()
and floatval()
FunctionsYou can pass a numeric string literal into the intval()
function to get the integer version or the floatval()
function to get the float version. For example:
// convert '3.9' to int and float $myFloat = floatval('3.9'); $myInt = intval('3.9'); // echo out type and value echo '$myFloat is ',var_dump($myFloat); echo '$myInt is ',var_dump($myInt);
We get an output of 3.9
as our float value. Note how casting a number to an integer won’t round it off, as'3.9'
becomes 3
:
$myFloat is float(3.9) $myInt is int(3)
(int)
and (float)
Make the (int)
or (float)
declaration in front of a numeric string literal to type cast it accordingly. For example:
$castFloat = (float) '3.9'; $castInt = (int) '3.9'; echo '$castFloat is ',var_dump($castFloat); echo '$castInt is ',var_dump($castInt);
Output:
$castFloat is float(3.9) $castInt is int(3)
settype()
FunctionYou can change the data type of the original numeric string into a float or integer using the settype()
function. This is useful in cases where you don’t want to create a new variable to hold a value of a different data type.
In this example, we pass in the variable $pi
and the datatype 'float'
as arguments into the settype()
function:
$pi = '3.14'; settype($pi, "float"); var_dump($pi);
The $pi
value is now a float.
float(3.14)
Here we pass in 'integer'
to change our $num
variable to an integer:
$num = '4.5'; settype($num, 'integer'); var_dump($num);
Output:
int(4)
Tasty treats for web developers brought to you by Sentry. Get tips and tricks from Wes Bos and Scott Tolinski.
SEE EPISODESConsidered “not bad” by 4 million developers and more than 100,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.
Here’s a quick look at how Sentry handles your personal information (PII).
×We collect PII about people browsing our website, users of the Sentry service, prospective customers, and people who otherwise interact with us.
What if my PII is included in data sent to Sentry by a Sentry customer (e.g., someone using Sentry to monitor their app)? In this case you have to contact the Sentry customer (e.g., the maker of the app). We do not control the data that is sent to us through the Sentry service for the purposes of application monitoring.
Am I included?We may disclose your PII to the following type of recipients:
You may have the following rights related to your PII:
If you have any questions or concerns about your privacy at Sentry, please email us at compliance@sentry.io.
If you are a California resident, see our Supplemental notice.