Jump to content

[SOLVED] using {}


soycharliente

Recommended Posts

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}' ";

 

Link to comment
https://forums.phpfreaks.com/topic/62034-solved-using/#findComment-308843
Share on other sites

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.)

 

 

Link to comment
https://forums.phpfreaks.com/topic/62034-solved-using/#findComment-308845
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/62034-solved-using/#findComment-308848
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.