Page 1 of 1

Variable… thingy… with {}s

Posted: Sun May 15, 2011 11:50 pm
by JelvinJS7
So I've noticed stuff like this happening in the jacek's videos:
$avariable = "hello";
$notavariable = "world";
echo "{$avariable} {$notavariable}!";
To output "hello world!"

So what I want to know is, what's the deal with the brackets? Cuz can't you output it the same way without them? (as in
$avariable = "hello";
$notavariable = "world";
echo "{$avariable} {$notavariable}!";
)
is it some security measure or something?

AHHHH IT'S SO CONFUSING!!!!
sorry had to spazz out there^


Btw, I already checked the php.net page on variables, and didn't see anything.

Re: Variable… thingy… with {}s

Posted: Mon May 16, 2011 12:01 am
by jacek
Without the {} php has to guess which part of the string following the $ indicates the variable. When using {} it tells php exactly which part of the string is the variable.

say you had
echo "I am a test string $variablethat has a variable in it.";
you see how $variablethat could be the variable or $variable
echo "I am a test string {$variable}that has a variable in it.";
Tells php that it is $variable and not $variablethat

In reality though it would not work like this as php stops looking for a variable when it finds a working one, meaning the shortest valid variable is used, but its a nice example ;)

Re: Variable… thingy… with {}s

Posted: Mon May 16, 2011 12:48 am
by JelvinJS7
OHHHH! that makes sense!
Duh! (facepalm)