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 \
Related
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
This question already has answers here:
Escaping quotation marks in PHP
(7 answers)
How can I prevent SQL injection in PHP?
(27 answers)
Closed 5 years ago.
I have a php variable referring to a string that contains apostrophes, but when I quote this variable, it thinks I am trying to end the string. My variable is reading from an array of table data, so I can not go in and put a "\" before every apostrophe in the table. If $foo contains the string "don't", how do I correctly say '$foo' without it trying to end the string. Thanks.
You are correct in thinking that you need to add escape characters ("\") before the apostrophes.
To do this on the fly with the database data you can use the php function addslashes.
so:
$escapedString = addslashes($string);
You could also do this with the string replace function for higher precision:
$escapedString = str_replace("'", "\'", $string);
You can use PHP's addslashes PHP Manual - Add slashes
$foo = addslashes($foo);
This question already has answers here:
Using PHP variables inside HTML tags?
(7 answers)
Closed 8 years ago.
in the code below,
{$row['title']}
Why are there two '\'s with the href tag? When I remove them and add "./" in front of the 'index.php', it doesn't work. Do they have somthing with the array( {$row['id]} ) that is being used in the id parameter? How do you think can I use this href tag without '\'s? When do we use '\' in a href tag?
I guess that the code snippet you copied from looks something like this:
<?php
echo "{$row['title']}";
?>
the \" is used to indicate that the quote is a character and not the end of the string.
This is called "escaping"
See characters like " and ' are used in HTML/JavaScript as well as php. So when u have to echo a string in PHP you echo like this: echo "Hello"
Now, what if that string itself had a ", like your case above.
So, we use \ character to escape the ", which is part of the string as in your example.
echo "foo";
The \ character in PHP escapes characters and tell's PHP to ignore whatever character is immediately ahead of it. They can be useful when you are trying to nest a string with the same type of quotes, such as the following:
$sql = 'SELECT * FROM members WHERE memberid = \'0001\'';
This is it's simplest usage, albeit has many other uses, as described here -> PHP: Escape Sequences
This question already has answers here:
php parameter with apostrophes
(2 answers)
Closed 2 years ago.
Part of my PHP code includes the construction of a URL string as follows:
$msg = $_REQUEST["msg"];
$content = 'action=sendmsg'.
'&user='.rawurlencode($username).
'&password='.rawurlencode($password).
'&to='.rawurlencode($destination).
'&text='.rawurlencode($msg);
When $msg happens to contain an apostrophe, it get sent as "\'".
What do I need to do to make sure the backslash is not inserted by PHP?
You probably want to check out stripslashes: http://php.net/manual/en/function.stripslashes.php
Assume you want to send the $content variable instead of just stripping the backslashes, Consider to use urlencode() instead of rawurlencode(). Also, you can use the function once for your $content variable.
$content = urlencode($content);
UPDATE: both urlencode and rawurlencode may not fit your case. Why don't you just send out the $content without URL encode? How do you send our the query string? If you are using cURL, you do not need to encode the parameters.
You can try
Stipslashes
or put the string in "" and add ' where you want.
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.