Print String With a PHP Variable in It

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

print variable in string using php



$string='hi i am [name] suthar and my email is [mail]. today i very happy because [message]';
$name="xyz";
$mail="xyz@xyz.com";
$message="my message";
$string=str_replace('[name]', $name, $string );
$string=str_replace('[mail]', $mail, $string );
$string=str_replace('[message]', $message, $string );
echo $string;

Why do you need single quote around the variable?

PHP: variables in strings without concatenation

In php you can escape variables like so

echo "Hello, ${var}'s head is big";

or like

echo "Hello, {$var}'s head is big";

Reference here under escape character section

Print string with a php variable in it

If you mix PHP and HTML you can do:

//PHP in HTML
<div class="vote_pct" style="width: <?php echo $width; ?>px;">

HTML in PHP
print '<div class="vote_pct" style="width: ' . $width . 'px;">';

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;

How to get a variable name as a string in PHP?

You could use get_defined_vars() to find the name of a variable that has the same value as the one you're trying to find the name of. Obviously this will not always work, since different variables often have the same values, but it's the only way I can think of to do this.

Edit: get_defined_vars() doesn't seem to be working correctly, it returns 'var' because $var is used in the function itself. $GLOBALS seems to work so I've changed it to that.

function print_var_name($var) {
foreach($GLOBALS as $var_name => $value) {
if ($value === $var) {
return $var_name;
}
}

return false;
}

Edit: to be clear, there is no good way to do this in PHP, which is probably because you shouldn't have to do it. There are probably better ways of doing what you're trying to do.

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.

Using echo to print variable string not showing anything

use echo htmlspecialchars($s);

Convert the predefined characters

"<" (less than) and ">" (greater than)

to HTML entities via htmlspecialchars function.

For more check w3chtmlspecialchars



Related Topics



Leave a reply



Submit