Jump to content
xisto Community
Arbitrary

Php String To Int Typecasting

Recommended Posts

I've been working on writing this program that needs strings to be converted to integers. Sometimes PHP works by doing the converting for me, but sometimes it just breaks down and quits working. I've got no idea why. $sub1 = 0+substr($text, 0, 3);$sub2 = 0+substr($text, 3, 6);$sub3 = 0+substr($text, 6, 9);$sum = 0+$sub1+$sub2+$sub3;echo $sum;Basically, $text is a string of numbers that looks something like: 296294255268313 and so on.Then I use substr to get the first three digits/letters of $text (296) and then the second three digits/letters of $text(294) and the third (255). I add 0 to each of these strings so that PHP can automatically cast it into an integer. This works fine.However, when I then go to add these integers together and put it inside variable $sum, everything goes awry. It outputs a weird (and very large) number. So then I decided to test if it was possible to add integers to strings with the following code:$sub1 = 294+255+substr($text, 0, 3);echo $sub1;And this, to my surprise, outputted the correct sum (845). Does anyone know how this happened or how it can be fixed? Thanks. =]

Share this post


Link to post
Share on other sites

It actually has nothing to do with the casting. You see, you are using substr as the following:


string substr(string $str, int $start, int $end);

when the correct syntax is as follows:


string substr(string $str, int $start, int $length);

So you should use:


$sub2 = 0+substr($text, 3, 3);$sub3 = 0+substr($text, 6, 3);

in the appropriate place. I've come across this problem many times because of differences between programming languages. To check a syntax, just go to http://de1.php.net/substr/

to get it instantly.


Share this post


Link to post
Share on other sites

Oh gods! So that's why. Thank you! =] No wonder the numbers were turning out to be so big.

You got that big number because you are not adding your substrings, instead of this what you are doing is to concatenate these substrings. The correct way was posted by bluefish.

 

Best regards,

Share this post


Link to post
Share on other sites

So easy, typecast with (int) on the string. example: it will typecast string "25" as int<? if (is_int((int)"25")){echo "hi";}?>-salahuddin66

Share this post


Link to post
Share on other sites

More better wayadd 0 with a string which has int valu. (typecasting may create problem in some situation)$val = "25";$val = $val +0;if(is_int($val)){echo "hi";}-salahuddin66

Share this post


Link to post
Share on other sites

Here

Php String To Int Typecasting

 

I am fed of finding logic for this, I even have my own logic, but not working.

 

The problem: there is a string - $given (it is holding a a string input by user, say "I am 25 or 26 year old boy and not 25.5 and 26.5")

 

I want to find the integer and float values from the string and display to the user, a sorted list of values by data type.

 

Like this way:

 

Integer 25

Integer 26

Float 25.5

Float 26.5

String "I"

String "want"

String "to"

:

:

String "and"

 

 

Pleeeeeeeeeeeeeeeeeeeeeeeease any Ideas...

I am fed up of logic.

 

Mine was :

$array = explode(' ', $given)

And for each $array[$I] element, look for every letter and see its ascii code is in 48 to 57, if yes then settype($array[$I],"integer")

But...

 

Help HELP

Share this post


Link to post
Share on other sites

the syntax is $stringname(startposition, length)

then if you want or need to typecast it, do so with int($substring) or just add 0 like you did. Either will work

but if you use + to add it will automatically be cast to an integer anyway.

if you were concatenating, you would use . Not +

The only reason to typecast is to use it in an expression that requires a number, and won't accept a string, even if it contains a number - like mktime

- frann

Share this post


Link to post
Share on other sites

No, he's not. He's adding a number with a length of 3 to a number with a length of 6 and another number with a length of 9

You don't use + for concatenation in php, you use .

You must be thinking of javascript or ruby or some other language.

Share this post


Link to post
Share on other sites

I've been working on writing this program that needs strings to be converted to integers. Sometimes PHP works by doing the converting for me, but sometimes it just breaks down and quits working. I've got no idea why.
$sub1 = 0+substr($text, 0, 3);
$sub2 = 0+substr($text, 3, 6);
$sub3 = 0+substr($text, 6, 9);
$sum = 0+$sub1+$sub2+$sub3;
echo $sum;

Basically, $text is a string of numbers that looks something like: 296294255268313 and so on.

Then I use substr to get the first three digits/letters of $text (296) and then the second three digits/letters of $text(294) and the third (255). I add 0 to each of these strings so that PHP can automatically cast it into an integer. This works fine.

However, when I then go to add these integers together and put it inside variable $sum, everything goes awry. It outputs a weird (and very large) number. So then I decided to test if it was possible to add integers to strings with the following code:

$sub1 = 294+255+substr($text, 0, 3);
echo $sub1;

And this, to my surprise, outputted the correct sum (845). Does anyone know how this happened or how it can be fixed? Thanks. =]


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.