dcolumbus
dcolumbus

Reputation: 9722

Variable within double quotes

Which is the correct syntax?

$current_renewal_date = $wpdb->get_results(
                    "
                    SELECT expiry_date
                    FROM bid_tag
                    WHERE id = $renewal_tag_id
                    LIMIT 1
                    "
                );

OR

$current_renewal_date = $wpdb->get_results(
                    "
                    SELECT expiry_date
                    FROM bid_tag
                    WHERE id = {$renewal_tag_id}
                    LIMIT 1
                    "
                );

Upvotes: 1

Views: 3721

Answers (4)

Mickle Foretic
Mickle Foretic

Reputation: 1409

I don't know if you could really say that there is a single correct form to put a variable within a string, but the most flexible and sure to work would be:

$current_renewal_date = $wpdb->get_results(
                    "
                    SELECT expiry_date
                    FROM bid_tag
                    WHERE id = ".$renewal_tag_id."
                    LIMIT 1
                    "
                );

There are at least 2 benefits of using this syntax instead of just putting the variable within the string.

Variable misinterpretation

1) When you put the variable within the string, you will probably experience problems if you need to mix the variable with other text.

Example:

$string = "My ID is AB$variableCD";

what is the variable? is it $variable? is it $variableC? or $variableCD? There are many interpretations for this on the syntax that you are using. This doesn't happen when you do

$string = "My ID is AB".$variable."CD";

Array usage

2) You can't easily use variables that have array indexes when you mix them within a string

Example:

$string = "The largest country is $options[countries in the world][$index]";

will get you *PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting ']'*

while using the following will work perfectly:

$string = "The largest country is ".$options['countries in the world'][$index];

And a little extra that isn't really a matter of syntax...

3) And there is actually a performance benefit, and that is the possibility of using ' instead of " for your strings. Using ' makes the string literal, that is, it isn't parsed and this causes a performance improvement when handling large strings.

$string = 'hello';

instead of

$string = "hello";

Upvotes: 0

eis
eis

Reputation: 53462

They both work. The preferred way would be using prepared statements and bound parameters.

Upvotes: 0

Gumbo
Gumbo

Reputation: 655129

Both are correct. The latter is just used to avoid ambiguity like if you want to reference $foo and not $foobar:

"{$foo}bar"

Upvotes: 1

Ry-
Ry-

Reputation: 224859

They're both correct* and equivalent. When you’re just interpolating a variable and not accessing an element in an array, the braces are optional.

* Use parametrized queries please

Upvotes: 0

Related Questions