Page 1 of 1

transfering variable value... something funny here..

Posted: Thu Oct 13, 2011 2:33 pm
by grabber_grabbs
OK, if i do :

echo " <B>$desc<B>";

I have this as an answer ;
Baxter 3 poches de lait 2%

when i try to transfer the value of $desc into the variable NAME (for my cart)
Here is the code : echo " <input type=hidden name=\"NAME\" value= $desc > ";

I got this in the name variable : Baxter (only the first word)

if i get into the sql database and modify the description by removing (two) 2 blanks ...
Baxter3poches de lait 2%

I got this in the shopping cart :
Baxter3poches

why this command : echo " <input type=hidden name=\"NAME\" value= $desc > ";
is transfering only the first word of the description ?
thank guys.

Re: transfering variable value... something funny here..

Posted: Thu Oct 13, 2011 2:35 pm
by Celauran
You need quotes around the value.

Code: Select all

echo "<input type=\"hidden\" name=\"name\" value=\"{$desc}\" />";

Re: transfering variable value... something funny here..

Posted: Thu Oct 13, 2011 3:26 pm
by grabber_grabbs
Finally this did the trick
echo " <input type=hidden name=NAME value=\"{$desc}\" />";

now, why $desc gave only the first word ?

where can i have info on php syntax.... "{variable}"

does all the variable in PhP has to be transfered like this ?

how come if i write

newvar = $desc

at the end, newvar will have the same value/content of $desc...

thanks again

Re: transfering variable value... something funny here..

Posted: Thu Oct 13, 2011 3:35 pm
by Celauran
HTML attribute values need to be enclosed in quotes. While <foo val=1> will work, it's still incorrect. <foo val=some string with spaces> will never work; it will always interpret as val=some.

You don't need braces around a variable when you're echoing the variable inside a double-quoted string, but you'll run into problems with things like:

Code: Select all

echo "Some string $array['key']";
Enclosing the variable in braces eliminates that problem, so I just do it as a matter of course.