tags:

views:

56

answers:

4

I have constructed the following -

`$format_$game_$name_$season`

4 variables with underscores between. Should this work, or will it act as one variable?

THankyou

+2  A: 

Useful Information

With backtick (``) delimiters, your variable will be blank.

With single quotes ('), it'll be interpreted literally.

With double quotes ("), you should get the variable interpolation (in most cases expect the case described in your question). You can use braces too for readability and some situations where it won't work without them, i.e {$var['key']}.

alex
Though the syntax note is important, this answer doesn't seem to address the initial question of whether or not the interpolation will act as the OP wants it to... Are you just saying that it will?
Matchu
@Matchu I'm saying as long as he surrounds that string with double quotes, it should substitute the variables. I think it should help him.
alex
@alex When I tried, that's not what happened =/
Matchu
@Matchu Hmm.. check my edit.
alex
Appreciate whoever cleared their downvote :D
alex
+1  A: 

Should this work, or will it act as one variable?

It will not, no.

1) You should use PDO for database access (I'm required to say this!)

With that out of the way:

2) Backticks are execution operators

3) Variables can be encased in braces: "{$format}_{$game}_{$name}_{$season}"

4) Some databases use the underscore as a special character so it may need to be escaped

webbiedave
+3  A: 

A quick test indicates that, for reasons I can not fathom, your interpolation will not work as you expect it to.

<?php
    $xa = 'x';
    $xb = 'y';
    $xc = 'z';
    echo "$xa_$xb_$xc";
?>

The output of the above script in PHP 5.3 is z, since it reads $xa_ (which is empty), $xb_ (which is empty), and $xc (which is z).

If you use braces for interpolation, however, you should get the desired output.

<?php
    $xa = 'x';
    $xb = 'y';
    $xc = 'z';
    echo "${xa}_${xb}_${xc}";
?>

The output of the above script in PHP 5.3 is x_y_z, as expected.

Matchu
+1 for going to the trouble to test it.
alex
+2  A: 

That wont work, but not because it'll act as one variable.

First, you need to use double quotes. Backticks wont interpret at all and variables wont be expanded in a single quoted string. Further reading: String Parsing.

Furthermore, when double quoting your string it'll be interpreted as $format_ . $game_ . $name_ . $season. That is, PHP will be thinking the three first variables end with an underscore. You'll have to do either $format . '_' . $game . '_' . $name . '_' . $season or "{$format}_{$game}_{$name}_{$season}".

Zackman