Jump to content

"echo" Command


dangercrow
 Share

Recommended Posts

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

Edited by Dangercrow
Link to comment
Share on other sites

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

... 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

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

Edited by Redroest
Link to comment
Share on other sites

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

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

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.

Edited by MrFish
Link to comment
Share on other sites

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.

Edited by Redroest
Link to comment
Share on other sites

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...