PHP curly braces in variable
Today I’ve got a discussion at stackoverflow about curly braces around PHP variable. The sample we’ve discussed about looked like this:
$query = "SELECT * FROM t WHERE smth LIKE '{$id}'"; |
In my opinion, it’s no sense for curly braces in this case. Imagine that you have an array or object and you need to access some array’s value or object’s variable. In this case, you will need curly brackets:
1 2 3 |
$query = "SELECT * FROM t WHERE smth LIKE '%{$array['key']}_jpg'"; // or $query = "SELECT * FROM t WHERE smth LIKE '%{$object->prop}_jpg'"; |
So, curly braces are needed to specify the begin and end of a variable.
Also, curly braces may be used for dynamic variables in PHP:
1 2 3 4 |
$a = 'smi'; $b = 'le'; $smile = ':-)'; echo ${$a.$b}; // will show :-) |