This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 6 years ago.
Afternoon guys!
For some reason Complex (Curly) Syntax in my PHP code doesn't interpreter as variable
function view($name, $data = [])
{
require __DIR__ . '/../app/views/{$name}.view.php';
}
Here is the code I am calling view('index'); the problem is I get this error:
Warning: require(./core/../app/views/{$name}.view.php): failed to open stream
By the way my PHP version before anyone asks is 7.0.13
Thanks for your help!
Your string has to be placed in double quotes (") instead of single quotes ('). Otherwise variables won't be replaced.
Look here:
https://secure.php.net/manual/en/language.types.string.php#language.types.string.syntax.single
Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.
Related
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.
What is better to use in PHP?
' or "? for something like this:
<?=$json["response"]["players"][0]["personaname"];?>
And this:
$steamid = "";
"Better" really depends on your use cases. In both your example, the single quote is better for the perfomance because you have no $variable interpolation needed.
Otherwise "better" may be a question of style (and a little of performance).
Read the documentation for more : http://php.net/manual/en/language.types.string.php
Better? Well, let's just say that an empty string with single quotes is the most basic string literal you can use. In your example shown, I would use single quoted strings for array elements and the empty string. PHP will know that the string literal is only a string literal, and that nothing else needs to be "interpolated." Better? Who knows.
One place to use "" is with escape sequences such as "\n" .
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
I came across some code in a file I was working that basically has
echo <<<output
"some html here"
output;
What does this do? Normally I do something like
echo "some html here";
but I've run into some cases where I have to use both ' and " and then it breaks the statement to echo, I think the first method would be a way around this but I would like to know what it's actually doing.
Thanks in advance.
You are looking at heredoc notation.
A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 8 years ago.
Here's my code:
$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=$name');
It doesn't appear to be using $name correctly. How would I add that variable into my string like I'm trying to do?
Change the single quotes to double quotes. PHP variables are not interpolated when in single quotes.
$jsonData = file_get_contents("http://example.com/bin/serp.php?engine=google&phrase=$name");
You can also use concatenation here:
$jsonData = file_get_contents('http://example.com/bin/serp.php?engine=google&phrase=' . $name);
Either change the single quotes around your string to double quotes:
"http://example.com/bin/serp.php?engine=google&phrase=$name"
Or use string concatenation with the . operator:
'http://example.com/bin/serp.php?engine=google&phrase=' . $name
Both of these techniques are mentioned on PHP's Strings documentation.
This question already has answers here:
PHP using Gettext inside <<<EOF string
(2 answers)
Closed 8 years ago.
Is it possible to put php code ( NOT ONLY {$SomeVariable} ) inside eof ?
bad syntax but just to get the idea...
$html = <<<EOF
<!docutype html>
<html>
<head>
<title>{ SOME PHP CODE }</title>
</head>
EOF;
No, heredoc strings are the same as double-quoted strings with the exception that the double-quote character does not need to be escaped. Variables are expanded in the same way (including complex variables as well as simple variables as in your example). You cannot run code other than this within the string.
I'd recommend reviewing the strings documentation:
http://www.php.net/manual/en/language.types.string.php
Also your example is invalid, the closing operator (EOF; in this example) cannot be indented.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Should I use php quote escapes for single quotes or use double quotes in arrays?
Is it okay to use array[key] in PHP?
what is the difference between these three $_POST values? :
$_POST[data];
$_POST['data'];
$_POST["data"];
The first one, the index is the constant data. Since that is likely undefined, PHP will often just convert it to the string 'data' and log a warning message.
The second two are both identical. The index is the string 'data'.
[Short addendum, since this is a dupe.]
This is considered technically wrong, unless a constant foo had been defined.
print $_POST[data];
Only in double quoted context it is valid (actually required sans curly quotes) to leave out the array keys:
print " use $_POST[data] in double quote context";
Btw, also check the manual (it can also be freely downloaded!) on these topics:
http://php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.types.array.php
You are taking it slightly wrong.
These quotes has nothing to with "POST value".
You can use almost any PHP expression as an array key - a string, a variable, a constnt, a function call.
I your case these keys being regular PHP strings.
And as a string it ought to be quoted - that's all
As for the quotes - there is no difference in this case.
Double quotes accept some special characters to interpret, you can see the list in the manual.
But as there are no special characters in your strings - there is no difference, which quotes to use.