transfering variable value... something funny here..

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
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

transfering variable value... something funny here..

Post 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.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post by Celauran »

You need quotes around the value.

Code: Select all

echo "<input type=\"hidden\" name=\"name\" value=\"{$desc}\" />";
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

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

Post 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
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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.
Post Reply