Monday, 25 September 2017

PHP using double quotes or single quotes in a function difference



Hey I have some simple php/html:




  if( $_POST["name"] || $_POST["age"] )
{
echo "Welcome ". $_POST['name']. "
";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>






Name:
Age:







I don't understand why on the second line we use double quotes i.e. $_POST["name"] but in the echo statement we use $_POST['name'], could someone tell me why these are different? Completely new to PHP...


Answer



It doesn't really matter what quotes you choose. There are several discussions about performance. Indeed double quotes are a tiny bit slower. But the difference is minimal even at 100,000+ iterations.



The only differences are the concenation and escaping:



Concenation



Double quotes:




$b = 1;
echo "Test ".$b; // outputs Test 1
echo "Test $b"; // also outputs Test 1


Single quotes:



$b = 2;
echo 'Test '.$b; // outputs Test 2

echo 'Test $b'; // outputs Test $b


Escaping



Double quotes:



echo "\t";
// outputs actual tab letter



Single quotes



echo '\t';
// outputs \t as plain text.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...