what is difference between this two echo ?? ////////////////string 1///// $name='Mahdi'; echo " hello Mr".$name ////////////////string 2///// $name='Mahdi'; echo " hello Mr $name "; PHP: and what is difference also $_POST['Username']; $_POST[Username]; // where Username is text feild PHP:
Double quoted strings will expand variable names. In your first, you are concatenating the varaible onto the end of the string. In the second, the variable is expanded. See here for more details on strings / single and double quotes. http://php.net/manual/en/language.types.string.php In what context are you using the latter? I assume it should be double quoted?
string 1 should be echo " hello Mr".$name; PHP: missing the ; and would be the same as string 2 with a space after the Mr second one depends where it is if you are setting $blah = $_POST[username]; will give an error, however will be ok with echo "$_POST[username]";
Any variable that sits between double quotes will be interpolated with the value of said variable. If you have the variable name outside of the double quotes - you can append it onto the end of that string by using the concatenation operator. Like you have done in your example. My preferred method of coding is to always use single quotes in combination with the concatenation operator. My reasons for this is: It executes faster, the interpreter doesn't have to check the string to see if any variables exist It looks a lot cleaner and you can easily distinguish where the variables are in large strings It's better for outputting HTML. Since it's written using double quotes when following W3C specifications
Thanks all I asked this question only to know if one best than other because I see poeple who use single quoted and other poeple who use double quoted in echo "My name is $name" or echo "My name is ".$name" & $_POST["Name"] or $_POST['Name'] or $_POST[Name] , so I see there is no difference even in eficiency and speed
Which one you use is down to preference really; if you are concerned about speed and efficiency (which, in most scenarios, will be negligable) then you will want to use a third way of doing it (as 'arguments', comma seperated) or use print with expandable variables. Otherwise, use whichever you feel more comfortable doing. I would still refer you here for further reading however and make your own mind up; http://php.net/manual/en/language.types.string.php