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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.