Nov 25, 2009

Php String To Int Typecasting

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Scripting > PHP

Php String To Int Typecasting

Arbitrary
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. =]

 

 

 


Comment/Reply (w/o sign-up)

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

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


when the correct syntax is as follows:

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


So you should use:
CODE
$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 www.php.net/substr to get it instantly.

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

TavoxPeru
QUOTE(Arbitrary @ Mar 25 2007, 03:35 PM) *
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,

Comment/Reply (w/o sign-up)

FeedBacker
So easy, typecast with (int) on the string.

example: it will typecast string "25" as int

<?

if (is_int((int)"25"))
{
echo "hi";

}
?>

-salahuddin66

Comment/Reply (w/o sign-up)

FeedBacker
More better way

add 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

Comment/Reply (w/o sign-up)

iGuest-Om
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


Comment/Reply (w/o sign-up)

(G)

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.


Comment/Reply (w/o sign-up)

(G)frann

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


Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Php Int Typecasting


    Looking for php, string, int, typecasting

See Also,

*SIMILAR VIDEOS*
Searching Video's for php, string, int, typecasting
advertisement



Php String To Int Typecasting

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com