Vignesh
Vignesh

Reputation: 952

Php String to Integer conversion gives 0 as result

When I try to convert a number as a string to an integer, The value changes to 0.

I did a preg_match and picked out a number value which as an output of preg_match is in a string datatype.

That is, When i tried to extract the number 7123 in the following xml data, <Count> 7123 </Count> using preg_match, I got the value 7123 as a string. [Used gettype function]. When I am checking if its 0 or anyother value, I need to convert it to integer datatype just to compare the values farely. So i do intval("7123") or intval($count) to get the integer value. But when I echo the value, its 0. Read few blogs online and one said this type of a thing happens when the string value is non-printable. Can someone help me understand this and help me solve it?

Upvotes: 0

Views: 3252

Answers (1)

l&#39;L&#39;l
l&#39;L&#39;l

Reputation: 47159

It's very simple once you know how to convert a string to integer:

$str = "7123";
$val = (int) $str;

echo $val + 1;

result: 7124

This is not the only way to convert strings to integers, but it's been proven to be the fastest.

Upvotes: 2

Related Questions