PHP echo string containing variable [duplicate] - php

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 3 years ago.
I have variable containing both text and variables (as text) such as the following
$variable = 'something';
$string = 'this is my string that contains a $variable in text form, but i want the variable to actually contain $variable when i echo it';
and what im trying to achieve is by echoing that string, it turns the variables into the correct text which would make the above sentence something like
echo $string;
should result in
"this is my string that contains a something in text form, but i want
the variable to actually contain something when i echo it"
Thank you for any help
Edit.
I've tried double quotes like echo "$string"; however its still echos it as text.
im getting the variable via a simple query
$answer_id = $row0['id'];
<div class="text">
<?php echo "$answer_id"; ?>
</div>
This still outputs a string without variables

You should use double quotes
$string = "this is my string that contains a $variable in text form, but i want the variable to actually contain $variable when i echo it";
LIVE DEMO

Related

How to include text next to php variable in heredoc text [duplicate]

This question already has answers here:
Mixing a PHP variable with a string literal
(5 answers)
Closed 4 months ago.
My actual use case involves filling in javascript variable names with partial id's from php, but to illustrate the issue here's a simple example with html:
$var="ello worl";
echo <<<HTML
H$var d
HTML;
I want the output to be "Hello world" however of course there is a space after the $var variable name so the output is "Hello worl d". If I remove the space, then it changes the variable name.
How do I place text next to the right side of the variable?
I've tried quotes and escaping etc. but to no avail.
You can enclose the variable in curly braces ({ and }) to ensure the PHP interpeter knows which characters are part of the variable name and which are static text.
$var="ello worl";
echo <<<HTML
H{$var}d
HTML;
Demo: https://3v4l.org/TYgEF
Documentation reference: https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing

Can't replace variable names in string [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 2 years ago.
I want to send a system message that addresses the user with his first name. The message is stored in a .txt file as:
Hello $user->firstname
Login link: something.something/user/id
In the userController (where the message is sent from) I'm now trying to replace the $user->firstname with the actual $user->firstname:
$output = file_get_contents(Yii::$app->basePath."message.txt");
$user = $this->findModel($id); //this is tested and works
$output = str_replace("$user->firstname", $user->firstname, $output);
However, my output after this is still the exact same as in the text file. What am I doing wrong?
I think it might be as simple as using single quotes in your str_replace call:
$output = str_replace('$user->firstname', $user->firstname, $output);
When you use double quotes, PHP has already tried to replace the string before calling str_replace.
See https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing for more information.
$output = str_replace("$user->firstname", $user->firstname, $output);
Variables inside double quotes get replaced - so you are not trying to replace the text $user->firstname here, you are trying to replace the text George (assuming that was the users first name) - but there is no George in your input text, so … nothing to replace.
Use single quotes, or escape the $ sign with a \

Insert GET variable into string in PHP [duplicate]

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 7 years ago.
This works:
Welcome <?php echo $_GET["name"]; ?><br>
But this doesn't:
'html' => 'Hello $_GET["name"];',
How should I code this?
it should be echo "Hello $_GET['name']";, single quotes show exact values, you can use double quotes if you are using variables.
echo "Hello {$_GET["name"]}";
This should do.
Correction:
Its not a good practice to use double quotes inside double quotes as pointed out in comments. So the correct thing should be Hello {$_GET['name']}

PHP programming help - Am I doing something wrong? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
In PHP, do both ( " ) and ( ' ) have the same effect? I'm new to PHP and I've been using them interchangeably. Can I?
Yes you can use single quoted or double quoted strings but they have some differences. Take a look at php string type.
You you can use them interchangeably, however following are differences:
Inside double quotes, php is able to parse variables for example
"Hello $name" // result: Hello [whatever stored in $name eg John]
Inside single quotes php is not able to parse variables:
'Hello $name' // result: Hello $name
Since php does not parse variables from single quotes, using single quotes is slightly faster.
More Information:
http://php.net/manual/language.types.string.php
$name = 'Amar';
echo "Hello, $name"; // outputs Hello, Amar
echo 'Hello, $name'; // outputs Hello, $name
Single quotes ( ' ) are faster by a very small margin since they don't need to scan strings for variables.

Whats the {} tag do in php? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
curly braces in string
Just came across this piece of code and it got me curious...
$msg .= "–{$mime_boundary}–n";
The $mime_boundary var was specified earlier to be output as a string.
Did a quick test....
$var = "XmytextX";
$str ="some wrapper{$var}end of";
echo $str;
and it does indeed output into the string. I've just never seen this way of outputting a var before.
Couldn't find any documentation on it - can anyone enlighten me?
So, normally, you could use the double quotes to output any variable like so:
echo "Hello, $name";
But, what if you wanted to output an item inside an array?
echo "Hello, $row['name']";
That wouldn't work. So, you would enclose the variable in curly brackets to tell the compiler to interpolate the entire variable:
echo "Hello, {$row['name']}";
On that note, you could also use it with objects:
echo "Hello, {$row->name}";
Hope that helps!
It's called variable-interpolation. In fact you don't need the {} around the var at all. This would also work:
echo "The value of my var is $var";
However if you need a more complex variable to output it sometimes only works with the {}. For example:
echo "This is a {$very['long']['and']['complex']['variable']}";
Also note, that variable-interpolation only works in strings with double-quotes! So this wouldn't work:
echo 'this is my $var';
// outputs: this is my $var
The curly braces set a variable name off from the rest of the text, allowing you to avoid the use of spaces.
For example, if you removed the curly braces, the PHP engine would not recognize $mime_boundary as a variable in the following statement:
$msg .= "–$mime_boundary–n";
By encapsulating the variable in curly braces, you tell the PHP engine to process the variable and return its value.
It's there to eliminate ambiguity: in your case, if you wrote "some wrapper $vared of", it's clear that PHP will try to put there the value of $vared, and that's not what you want.
The curly braces let you specify which part of the string should be interpolated.
Consider $str ="some wrapper{$var}end of";
verses $str ="some wrapper$varend of";
They also allow you to insert array elements and class variables directly into strings
$str = "foobar $foo['bar']"; // parse error
$str = "foobar {$foo['bar']}";
$str = "this is my {$foo->bar[1]}";
It's related to variable interpolation.
This is explained in the manual (under "Complex (curly) syntax"). I'm curious as to why you haven't read it if you are working in PHP.
If you take a look at http://php.net/manual/en/language.types.string.php you'll see its just another way of embedding a variable within a string, it just allows you to define where the end of the variable is.
For example:
$var = "Animal"
echo "The {$var}s"; //Outputs "The Animals"

Categories