soycharliente Posted July 27, 2007 Share Posted July 27, 2007 What does {} do when you enclose variables in it? I see it all the time and I suck at using Google. Examples I've seen: {$var} {$foo['bar']} Quote Link to comment Share on other sites More sharing options...
grimmier Posted July 27, 2007 Share Posted July 27, 2007 you use the curly brackets when you want to plug variable data into a Double quoted string. example <?php $var = 'something'; echo "this is {$var} about Mary"; //prints "this is something about Mary ?> more commonly you will see this when you are constructing SQL queries, ie "SELECT * from DB where `this` = '{$var}' "; Quote Link to comment Share on other sites More sharing options...
Crow Posted July 27, 2007 Share Posted July 27, 2007 Basically, it means 'Evaluate whatever is in the Curly Brace'. Example: Say you wanted to create a dynamic variable..or something. <?php $user = 'Crow'; ${u . $user} = 'Somevalue'; ?> That would create the variable $uCrow with the value Somevalue. (To the above poster, you don't need to use {} to insert variable data into a double-quoted string, that's done automatically.) Quote Link to comment Share on other sites More sharing options...
almightyegg Posted July 27, 2007 Share Posted July 27, 2007 When using a query like: $meh = mysql_query("SELECT * FROM table WHERE col = '$variable['variable']'") or die(mysql_error()); the {}s are needed around the $variable['variable'] so it is executed without errors. It should be: $meh = mysql_query("SELECT * FROM table WHERE col = '{$variable['variable']}'") or die(mysql_error()); There are other ways of doing it but I like this way. NB. It's only necessary when it is a variable like $variable['variable'], not when it is just $variable. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted July 27, 2007 Author Share Posted July 27, 2007 tHANKS GUYS. Quote Link to comment Share on other sites More sharing options...
ignace Posted July 27, 2007 Share Posted July 27, 2007 you should be using sprintf() especially when using queries, also *_real_escape_string() is recommended or something along those lines like addslashes() Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.