string variable problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

string variable problem

Post by SirChick »

I have this small script:

Code: Select all

<?php
$Multiply = 0.00;
$Input = 6;
settype($Multiply, "string");
settype($Input, "string");
Echo $Answer = $Multiply.$Input;
?>
But its not giving the correct result i want. I wanted it to show 0.006
How ever it is showing 06 instead.

Any one know where i went wrong ?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Just put the values in quotes to denote a string.

Code: Select all

C:\Users\scott>php -r "$multiply = '0.00'; $input = '6'; echo $multiply.$input;"
0.006
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
SirChick
Forum Contributor
Posts: 125
Joined: Tue Jul 31, 2007 11:55 am

Post by SirChick »

scottayy wrote:Just put the values in quotes to denote a string.

Code: Select all

C:\Users\scott>php -r "$multiply = '0.00'; $input = '6'; echo $multiply.$input;"
0.006

Ok that works which is what i got here:

Code: Select all

$multiply = '0.00'; 
$input = '6'; 
echo $multiply.$input;
How ever,
$input = '6';

Is coming from a $_POST['inputbox']

and is naturally going in as a number so how can i convert to string for it to use it cos when i did settype($Input, "string");
to test it out it didnt work.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Coming from an input box, I imagine it will already be a string. However, you can just use type-casting.

Code: Select all

$input = (string) $_POST['input'];
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

All elemental data coming from $_GET and $_POST is going to be a string already.
Post Reply