dangercrow Posted January 21, 2010 Share Posted January 21, 2010 Basically, I want to know the proper use of the echo command...How do I echo: Text only HTML code A mix of the above Longer stuff And what are the differences between these codes: echo "Blah" echo ("Blah") echo (Blah) And is there any difference between using double (") and single (') quotes?And do I need a space after the command "echo"?Thanks Link to comment Share on other sites More sharing options...
Ingolme Posted January 21, 2010 Share Posted January 21, 2010 In a program, you can place parenthesis anywhere, but they're unnecessary most of the time. Take this, for instance: $c = ($a) + ($b * (5)); The echo command doesn't need parenthesis, it's best if you don't use them.A string with single quotes will be represented literally, a string with double-quotes will interpret the variables in it: $str = 'something';echo "Do $str"; // Prints "Do something"echo 'Do $str'; // Prints "Do $str" echo (Something); This is wrong, because all strings must be surrounded by quotes. PHP parsers will throw an error if you do that. "something" isn't a command or constant. Link to comment Share on other sites More sharing options...
boen_robot Posted January 21, 2010 Share Posted January 21, 2010 ... if you do have a constant named "Something", you can echo its value though. So, for example: define('Something', 'Hello world!');echo Something; will output Hello world! Link to comment Share on other sites More sharing options...
dangercrow Posted January 21, 2010 Author Share Posted January 21, 2010 Ok thanks for that! Link to comment Share on other sites More sharing options...
Redroest Posted January 22, 2010 Share Posted January 22, 2010 I recommand you to use single quotes for strings and double qoutes for values when combining it with html for example. <?php//normalecho 'Normal string';//using html inside stringecho '<div id="testdiv">Normal string with html</div>';//set string in variable$language = 'php';$markup = 'xhtml';//output php variables in a stringecho 'I am using '.$language.' combined with '.$markup.'<br />';//javascript in phpecho '<script type="javascript">';echo ' alert("javascript in php");';echo '</script>';//advanced javascript in php$text = 'php string text in javascript-error';echo '<script type="javascript">';echo ' alert("'.$text.'"); ';echo '</script>';?> Just make single quote as your php to string converter. Double quote can be used to combine php with either javascript or html. This will make your code look nice and easier to read! You can also use \ before the quote but I recommand using it only when you have to combine more then 2 languages in one singe script, but then again, use single quote for php, doublequote for javascript and \ to escape one of the quotes just to output the quote as a string in stead of using it for php Link to comment Share on other sites More sharing options...
Synook Posted January 23, 2010 Share Posted January 23, 2010 Note that the only real difference between single and double quotes is that variables are interpreted inside double quotes, and not single ones. Link to comment Share on other sites More sharing options...
Redroest Posted January 23, 2010 Share Posted January 23, 2010 Is there also a slight differance in speed?I gues it is a lot easier for a server to read echo 'One '.$var.'<br />'; then echo "One $var"; Link to comment Share on other sites More sharing options...
Synook Posted January 23, 2010 Share Posted January 23, 2010 Yes, single quotes are faster. Link to comment Share on other sites More sharing options...
boen_robot Posted January 23, 2010 Share Posted January 23, 2010 Is there also a slight differance in speed?I gues it is a lot easier for a server to read echo 'One '.$var.'<br />'; then echo "One $var";In the particular example you give, using double quotes would be faster.If you have a string in which you don't need to interpolate variables, it will be faster if you just use single quotes, i.e.echo 'String'; is better than echo "String"; but $var = "meh";echo "String $var"; and/or $var = "meh";echo "String {$var}"; are better than $var = "meh";echo 'String' . $var; or $var = "meh";echo "String" . $var; or even (I think) $var = "meh";echo 'String', $var; Link to comment Share on other sites More sharing options...
MrFish Posted January 23, 2010 Share Posted January 23, 2010 Also, I don't know if anyone made note of this but you can use single quotes to print something with double quotes (also could use an escape though).This won't work. echo "And Bill said "I hate snow""; This will echo 'And Bill said "I hate snow"'; prints And Bill said "I hate snow"But you can always escape the quotes.echo "And Bill said \"I hate snow\""; This will work too. Link to comment Share on other sites More sharing options...
Redroest Posted January 23, 2010 Share Posted January 23, 2010 Cool, learned something new today. Thanks :)Although I will only use the one with he singlequotes, because it makes it a lot easier to read for everyone that wants to change something in it including myself. I also like to use a lot of newlines. Does this affect speed? //many people use thisif($var == $var2){ echo 'yes';}//Or even thisif($var == $var2){ echo 'yes'; }//I use thisif($var == $var2){ echo 'yes';} @MrFishThis is not a good way to show quotes because it forces you to use singlequotes as string identifier and another time double quotes. I really recommand you to use backslash to show that the quote is part of a string. Link to comment Share on other sites More sharing options...
Synook Posted January 24, 2010 Share Posted January 24, 2010 In the particular example you give, using double quotes would be faster.Interestingly enough, I think I once did a benchmark that showed that using single quotes and concatenation was faster than interpolating. Can't find it now, though. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.