Re: why use {} around vraiable?
| From: | Robin Vickery | Date: | Thu, 20 Mar 2008 16:41:30 +0000 |
| Subject: | Re: why use {} around vraiable? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-271909@lists.php.net to get a copy of this message | ||
On 20/03/2008, Lamp Lists <lamp.lists@yahoo.com> wrote:
> hi,
> I saw several times that some people use this
>
> $parameters = array(
> 'param1' => "{$_POST["param1"]}",
> 'param2' => "{$_POST["param2"]}"
> );
>
> or
>
> $query = mysql_query("SELECT * FROM table1 WHERE id='{$session_id}'");
>
> I would use:
>
> $parameters = array(
> 'param1' => $_POST["param1"],
> 'param2' => $_POST["param2"]
> );
>
> and
>
> $query = mysql_query("SELECT * FROM table1 WHERE id=' ".$session_id."
> ' ");
>
>
> does it really matter? is there really difference or these are just two "styles"?
yes, it matters when you're trying to include a complex variable
"this is a $variable"; # ok
"this is an $array[12]"; # ok
"this is an $array[word]"; # warning under E_STRICT
"this is an $array[two words]"; # not ok, can't have whitespace
"this is an {$array[two words]}"; # not ok, indexes should be quoted
"this is an {$array['two words']}"; # ok
"this is an $object->property"; # ok if you're after the property
"this is an {$object}->property"; # but you need brackets if you want
the object as a string
etc...