In this tutorial, I am going to explain about echo. In php there are two ways of getting the output.
- Echo
You might observing that in the previous tutorials i am using echo command for outputting the text in web browser (like chrome, mozilla). I bet you that you are going to use this command for outputting text more than any other command. So lets start it with an example without any further chit-chat.
1 2 3 4 5 6 |
<?php echo "Php keeda is best website to learn php online"; echo "<br>"; // to give space between these two statements echo "<h3>Lets start here with an example </h3>"; ?> |
Output :
Php keeda is best website to learn php online
Lets start here with an example
Both echo and print statements are used to print data. There are very small difference in them.
Echo is marginally faster than print statement.
Echo has no return value while print returns 1 thats why it is used in expressions.
Echo can have multiple parameter while print can take one argument
* We can use echo statement with or without parentheses i.e : echo () OR echo
Lets play with some examples and and clear all doubts.
Example 1.
1 2 3 4 5 6 7 |
<?php echo "Statement without parenthesis"; echo "<br>"; // to give space between these two statements echo "(Statement enclosed in parenthese)"; echo "<br>"; echo 'enclosed in single quote'; ?> |
Output :
Statement without parenthesis
Statement enclosed in parenthese
enclosed in single quote
Example 2.
1 2 3 4 5 6 7 |
<?php echo "<h4>Learning php from phpkeeda</h4>"; // adding html tag h4 echo "Hello Php<br>"; echo "I'm about to learn PHP!<br>"; echo "This ", "string ", "was ", "made ", "with multiple parameters."; // echo with multiple parameter ?> |
Output :
Learning php from phpkeeda
Hello Php
I’m about to learn PHP!
This string was made with multiple parameters.
Example 3.
1 2 3 4 5 6 7 8 9 |
<?php $txt1 = "Learn PHP"; $txt2 = "phpkeeda.com"; $x = 5; $y = 4; echo "<p>$txt1</p>"; echo "Study PHP at $txt2<br>"; echo $x + $y; ?> |
Output :
Learning php
Study PHP at phpkeea.com
9
Output data using print
1 2 3 4 5 |
<?php print "<h2>PHP is Fun!</h2>"; print "Hello php!<br>"; print "I'm about to learn PHP!"; ?> |
Output :
PHP is Fun!
Hello php!
I’m about to learn PHP!
In all the above example we echo several string value and integer. There are some points to remember while echoing strings and integer.
- It is very good in php that you can use html tags inside the php. However be careful while using html tags and quotes inside php script.
- While printing string we use quotes(” ” or ‘ ‘) at the beginning and ending of the string. But when we are echoing quotations inside string we have to escape it first.
Lets take an example to demonstrate what are the correct and incorrect way of echoing strings –
1 2 3 4 5 6 7 8 |
<?php // This won't work because of the quotes around headingH5! echo "<h5 class="headingH5">PHP is my passion</h5>"; // OK because we escaped the quotes! echo "<h5 class=\"specialH5\">PHP is my passion</h5>"; // OK because we used an apostrophe ' echo "<h5 class='specialH5'>PHP is my passion</h5>"; ?> |
Whenever you need to echo quotations inside string you can simply use ( \” ) OR ( ‘ ) at the beginning of the quotation. The backslash tells the php that you are going to use quotation inside your string (NOT going to end the string).
1 2 3 4 5 6 7 8 |
<?php $my_string = "Hello jack. My name is:"; // assigning statement inside $my_string variable echo "$my_string Ravi <br />"; // printing data with a line break echo "Hi, I'm jack. Who are you? $my_string <br />"; echo "Hi, I'm jack. Who are you? $my_string Bobetta"; ?> |
Output :
Hello jack. My name is: Ravi
Hi, I’m jack. Who are you? Hello Bob. My name is:
Hi, I’m jack. Who are you? Hello jack. My name is: Ravi
Using variable inside your string is an easier and time saving way to write code.