php: inserta una variable en una cadena de eco

$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?

preguntado el 08 de noviembre de 11 a las 17:11

10 Respuestas

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!";

Y en tu caso:

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

OR

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

respondido 08 nov., 11:22

I suppose only the first would work for my instance as the variable is not separated from the word. - Anthony Miller

No, it shouldn't matter, as the second one isn't outputting any space between "paragraph" and the output of the variable. - Simon

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. - Anthony Miller

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

Respondido el 05 de diciembre de 14 a las 08:12

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

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

respondido 08 nov., 11:22

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

debería hacer el truco.

respondido 08 nov., 11:22

echo '<p class="paragrah"' . $i . '">'

respondido 08 nov., 11:22

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

respondido 08 nov., 11:22

Here's the 3 best ways of doing this.

Método uno:

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

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


Método Dos:

$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 y $z a $x usando el "."; los {} 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.

Esto daría como resultado $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.

Respondido 06 Abr '20, 06:04

Utilice comillas dobles:

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

respondido 08 nov., 11:22

$i = 1;

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

$i++;

respondido 24 nov., 20:01

Usted puede ensayar lo siguiente

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

Respondido 07 ago 13, 10:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.