php - insert a variable in an echo string
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;
Insert variable in Echo String
You have a few mismatched quotes.
if ($row['comment'] != null)
echo '<div class="rowComment">' . '<div class="postComment" id="postcomment">' . $row['comment'] . '</div>' . '<div class="row3_a">' . "Posted: $post_date Ago" . '</div></div>';
PHP - concatenate or directly insert variables in string
Between those two syntaxes, you should really choose the one you prefer :-)
Personally, I would go with your second solution in such a case (Variable interpolation), which I find easier to both write and read.
The result will be the same; and even if there are performance implications, those won't matter 1.
As a sidenote, so my answer is a bit more complete: the day you'll want to do something like this:
echo "Welcome $names!";
PHP will interpret your code as if you were trying to use the $names
variable -- which doesn't exist.
- note that it will only work if you use "" not '' for your string.
That day, you'll need to use {}
:
echo "Welcome {$name}s!"
No need to fallback to concatenations.
Also note that your first syntax:
echo "Welcome ".$name."!";
Could probably be optimized, avoiding concatenations, using:
echo "Welcome ", $name, "!";
(But, as I said earlier, this doesn't matter much...)
1 - Unless you are doing hundreds of thousands of concatenations vs interpolations -- and it's probably not quite the case.
How to insert a variable in the middle of an img src string using echo in PHP
This is really about the correct use of quotes and whether to echo it out from within PHP or to stop the PHP interpreter and output the HTML raw.
One way
<?php
$amount = 50
$ordernumber = 123
echo "<img src='sale.php?profile=1&saleamt=$amount&ordernum=$ordernumber' style='height:0px; width:0px; border:0px;' />";
Other way
<?php
$amount = 50
$ordernumber = 123
?>
<img src="sale.php?profile=1&saleamt=<?php echo $amount;?>&ordernum=<?php echo $ordernumber;?>" style="height:0px; width:0px; border:0px;" />
How do I insert variable inside an echo string and function call?
JavaScript probably is requiring $name to be quoted.
$hint = "<p onClick='DoIt(); showUser(3, change, \"" . $name . "\");'>" . $name . "</p>";
Escaping the quotes like this will cause them to appear in the JS function call.
Quotes are often a thorny problem when mixing PHP, JavaScript and HTML on one line.
Mixing a PHP variable with a string literal
echo "{$test}y";
You can use braces to remove ambiguity when interpolating variables directly in strings.
Also, this doesn't work with single quotes. So:
echo '{$test}y';
will output
{$test}y
How to insert php variable inside a double quoted php string
You missed out the closing '
. Also, you need to concat them using the PHP concatenation operator .
to join the string with the variable:
$a['id'] = 1;
$html .= '<a href="' . $a['id'] . '">Link</a>';
PHP Variable inside PHP -> Echo -> Javascript
You'll want to use string concatenation (using the .
character) to insert a variable into your string. Like this:
echo '
[...]
<script type="text/javascript">
var dID = ' . $dID . ';
function wait(ms){
[...]
';
A .
will concatenate two strings together. For example:
echo 'hello ' . ' world'
You can also insert a variable directly into a string, if you use double quotes. Single quotes do not allow you to do this:
$text = "world";
echo "hello $text";
In general, you should wrap your variables in curly brackets ({
and }
)
$text = "world";
echo "hello {$text}";
Related Topics
PHP - How to Create a Newline Character
Insert Array into MySQL Database With PHP
Post Request in Laravel - Error - 419 Sorry, Your Session/ 419 Your Page Has Expired
Sending Email Via PHP Mail Function Goes to Spam
Storing Files in Database VS File System
Preserving Session Variables Across Different Domains
How to Properly Url Encode a String in PHP
PHP Pdoexception: "Sqlstate[Hy093]: Invalid Parameter Number"
How Does Recursiveiteratoriterator Work in PHP
How to Get Protected Property of Object in PHP
How to Resolve Ambiguous Column Names When Retrieving Results
What's the Maximum Value For an Int in PHP
How to Delete a Line from the File With PHP
How to Loop Through Two Arrays At Once