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

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.

Related

What's better to use: ' or " [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.
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" .

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']}

How to enter variable into string? [duplicate]

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.

Difference between uses of inverted commas in PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Difference between single quote and double quote string in php
Can you use " and ' interchangeably, 100%? Or is there a reason or use for each? What is the difference exactly?
Observe for yourself:
$name = 'John';
echo "Hello $name" . '<br>';
echo 'Hello $name';
Result:
Hello John // result from double quotes
Hello $name // result from single quotes
As can be seen variables inside double quotes are parsed while in single quotes they aren't.
So when you put variables inside double quotes, they can be parsed and their correct value is output whereas with single quotes, variables are not parsed and you get the same output of variable name itself as in Hello $name.
Since variables inside single quotes aren't parsed, using them is just a little good when it comes to performance.
If there is no question of variables inside quotes, you can use them inter-changeably though keeping above performance tip in mind.
For more information, you can look at the official documentation.
Just to add to the great answer of Sarfraz, there are certain situations where you would want to use one or the other.
Single quotes ('') are always parsed slightly (minutely) faster than double quotes so if you are an optimization freak, a good rule of thumb is to use single quotes instead of double quotes if you will not be parsing any variables.
However, if you have tons of variables and don't want to do something like:
echo 'My name is ' . $name . '!';
then you're better off with double quotes.
However when dealing with html output, you may consider the hassle of escaping your double quotes too tedious to deal with:
echo "<p id=\"myParagraph\">$name</p>";
So in this case the vote goes to single quotes.
Another thing is that when you build SQL queries with PHP, you may notice that you might prefer using double quotes to be able to parse variables and avoid escaping the single quotes:
"SELECT * FROM CoolGuys WHERE Name = '$name'";
In the end it's all a matter of preferrence. :)
Good luck!

using no quotes vs single quotes vs double quotes in $_POST value [duplicate]

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.

Categories