47
$i = 1
echo '
<p class="paragraph$i">
</p>
'
++i

Trying to insert a variable into an echoed string. The above code doesn't work. How do I iterate a php variable into an echo string?

10 Answers 10

111

Single quotes will not parse PHP variables inside of them. Either use double quotes or use a dot to extend the echo.

$variableName = 'Ralph';
echo 'Hello '.$variableName.'!';

OR

echo "Hello $variableName!";

And in your case:

$i = 1;
echo '<p class="paragraph'.$i.'"></p>';
++i;

OR

$i = 1;
echo "<p class='paragraph$i'></p>";
++i;
3
  • I suppose only the first would work for my instance as the variable is not separated from the word. Commented Nov 8, 2011 at 18:03
  • 1
    No, it shouldn't matter, as the second one isn't outputting any space between "paragraph" and the output of the variable.
    – Simon
    Commented Nov 8, 2011 at 18:05
  • The 3rd won is what works for me. But I do understand the difference between double and single quotes. Just that I'm editing an open source program to make some minor customizations and I don't want to change the formatting around too much. Commented Nov 8, 2011 at 18:19
31

Always use double quotes when using a variable inside a string and backslash any other double quotes except the starting and ending ones. You could also use the brackets like below so it's easier to find your variables inside the strings and make them look cleaner.

$var = 'my variable';
echo "I love ${var}";

or

$var = 'my variable';
echo "I love {$var}";

Above would return the following: I love my variable

1
  • 1
    Note that the first of these examples is deprecated, and will be removed in PHP 9.0, as its behaviour is a little bit peculiar. The second example will remain supported, and is the most flexible.
    – IMSoP
    Commented Nov 20, 2023 at 17:50
11

Variable interpolation does not happen in single quotes. You need to use double quotes as:

$i = 1
echo "<p class=\"paragraph$i\"></p>";
++i;
7
echo '<p class="paragraph'.$i.'"></p>'

should do the trick.

7
echo '<p class="paragrah"' . $i . '">'
0
5
echo '<p class="paragraph'.$i.'"></p>';
4

Here's the 3 best ways of doing this.

Method One:

$x = '+3';
echo "1+2$x";

Double Quotes (") allows you to just pass the variable directly inside it.


Method Two:

$x = '+3';
echo '1+2'.$x;

When you don't want to use double quotes for whatever reason go with this. The (.) simply means "Add" basically. So if you were to want to add something like, 1+2+3+4+5 and have your variable in the middle all you need to do is:

$x = '+3';
echo '1+2'.$x.'+4+5';

Method 3: (Adding a variable directly inside the called variable)

$x = '+3';
$y = '+4';
$z = '+5';
echo "1+2${"x".$y.$z}";
Output: 1+2+3+4+5

Here we are adding $y and $z to $x using the "."; The {} prioritize's the work inside it before rendering the undefined variable.

This personally is a very useful function for calling functions like:

//Add the Get request to a variable.
$x = $_GET['tool'];

//Edit: If you want this if to contain multiple $xresult's change the if's
//Conditon in the "()" to isset($get). Simple. Now just add $xresultprogram
//or whatever.
if($x == 'app') {
    $xresultapp = 'User requested tool: App';
}

//Somewhere down far in HTML maybe...

echo ${"xresult".$x}; // so this outputs: $xresultapp's value

//Note: doing ${"xresult".$_GET['tool']} directly wont work.
//I believe this is because since some direct non-echo html was loaded
//before we got to this php section it cant load cause it has already
//Started loading client side HTML and JS.

This would output $xresultapp's 'User requested tool: App' if the url query is: example.com?tool=app. You can modify with an else statement to define what happens when some value other than 'app' is requested. Remember, everything is case-sensitive so if they request 'App' in capitals it won't output $xresultapp.

2

Use double quotes:

$i = 1;
echo "
<p class=\"paragraph$i\">
</p>
";
++i;
0
2
$i = 1;

echo "<p class='paragraph{$i}'></p>"; 

$i++;
0
0

You can try this

$i = 1
echo '<p class="paragraph'.$i.'"></p>';
++i; 
0

Not the answer you're looking for? Browse other questions tagged or ask your own question.