Reputation: 33
I'm writing a laravel controller if that is relevant here.
"\"$alias\""
Here I am passing the variable in alias, and I want to surround it with double quotes.
"\"remote\""
Here I am just trying to pass the word remote enclosed in double quotes.
Not sure if I am doing this correctly, or if there is a better way to do it.
For the ones without variables, is '"remote"'
a better way to achieve what I want?
Thanks again!
Upvotes: 0
Views: 148
Reputation: 1557
The above are incomplete and also incorrect, unnecessarily using brackets.
Not many people know how to properly use and output strings with PHP. Do it properly using inline strings, heredoc and nowdoc. The below may be incomplete so look at the PHP documentation at https://www.php.net/manual/en/language.types.string.php for more info.
Single quotes (literal) will output exactly what you enter between them and cannot include escape characters or variables:
$a=1;
echo' "Double quoted" $a © ';
//Output: "Double quoted" $a ©
Double quotes (inline) can include variables, escape characters, etc.
$a=1;
echo" 'Single quoted' $a © \" escaped";
//Output: 'Single quoted' 1 © " escaped
Inline indexed variable:
echo "Index 2 of myvar: $myvar[2]";
echo "Index a of myvar: {$myvar['a']}";
Heredoc: (basically multi-line double quoted as above)
echo<<<myeot //Start heredoc
'Single quoted text'
"Double quoted text"
The value of a is $a
The value of b[index] is {$b['index']}
myeot; //end heredoc
Can also define variables with heredoc and nowdoc:
$str=<<<myeot
xxx
Number $a will output
yyy
myeot;
Nowdoc is very similar to heredoc but is supported by PHP 5.3.0 and above only.
Upvotes: -1
Reputation: 2408
echo "The value is \"$value\"";
and
echo 'The value is "'.$value.'"';
are both perfectly valid to show a variable in double quotes.
echo "The word is \"bird\"";
and
echo 'The word is "bird"';
are both perfectly valid to show a constant phrase in double quotes.
Remember that variables aren't parsed inside single-quoted strings, so echo 'The value is $value';
won't work.
Upvotes: 2