Php echo

In this tutorial, I am going to explain about echo. In php there are two ways of getting the output.

  • Echo
  • print

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.

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.

Output :
Statement without parenthesis
Statement enclosed in parenthese
enclosed in single quote

Example 2.

Output : 
Learning php from phpkeeda
Hello Php
I’m about to learn PHP!
This string was made with multiple parameters.

Example 3.

Output : 
Learning php
Study PHP at phpkeea.com
9

Output data using print

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.

  1. 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.
  2. 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 –

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

 

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.

Leave a Reply

Your email address will not be published. Required fields are marked *