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" .
Related
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.
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.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
this is more of a better-practice type of question.
I'd like to know if using brackets in double-quoted strings for variables is good practice.
For example:
<?php
$Variable = 'a variable';
$SingleQuotedString = 'Single quoted string with ' . $Variable;
// Single quoted string with a variable
$DoubleQuotedString = "Double quoted string with $Variable";
// Double quoted string with a variable
$DoubleQuotedStringWithBrackets = "Double quoted string with {$Variable} in brackets.";
// Double quoted string with a variable in brackets.
?>
It doesn't change the output or the code from simple tests, and obviously works. I'm just confused because not many people do this, and I don't see recommendations or people disagreeing with it, and I've been using them just fine.
Thanks for any feedback!
The curly braces are to allow the use of arrays and objects, i.e:
$string = "my array value: {$foo['bar']}";
or
$string = "my object value: {$foo->bar}";
Fastest and cleanest version is the first one. Variables in a double quoted string... just don't "feel" right to me.
$SingleQuotedString = 'Single quoted string with ' . $Variable;
The only situation where it comes in handy I can think of is when you have a
$string = "with a {$load} of {$variables} in {one} {sentence}!";
and the readability would suffer to much otherwise.
You need to use brackets in case you have no space after your variable
$a = 1;
$aa = 2;
echo "$aaa"; // prints nothing but a notice cause $aaa is not defined
echo "{$a}aa"; // prints 1aa
echo "{$aa}a"; // prints 2a
or if you want to call an object method
echo "{$myObject->myMethod()}"; // fatal error cause $myObject is null ;) otherwise it works just fine
Otherwise you can use brackets or not.
Single quotes are faster if you have no variables in your string.
Accessing array value or object propertie does'nt require brackets. But maybe it's a bit easyer to read.
The real reason that bracket quoted variables in strings exists is for accessing values in arrays or objects. E.g.
echo "The result is {$res['foo']}"
or
echo "The result is {$res->foo}"
which won't work if you didn't use the brackets. If you find it easier to see bracket quoted strings then use them. If not then use them only when required (to dereference an array or object).
These are all stylistic choices because so long as you follow the rules you'll end up with the same result. You're trading off readability, editability, error-resistance and a truly tiny amount of speed with each one. There are no accepted 'best practices', but simply developer preferences which people will defend zealously.
I find the single quote form noisy, more verbose and less aesthetically pleasing. I tend to use it only for short strings.
I prefer double quotes for longer strings, because they give me the flexibility to move interpolations around the string in an less error prone way. I use brackets when I must be
explicit or need the value inside an array or object, but always err on the side of succinctness.
In general it's probably best to just be consistent to maximize team productivity.
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.
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.