PHP echo | 3 examples to learn echo statement

The PHP echo statement

The echo statement in PHP is used to display the output of a given expression.

See an example of using echo

You can use the echo statement to display any type of data in PHP. For example numbers, strings, variables or any other expression.

Following is the syntax to use the echo statement which is followed by examples.

How to use the echo statement

Following is the general syntax of echo statement:

echo <expression|variable>;

An example of using echo

This is how you can use the echo PHP statement to display a string or variable’s value.



You can see, echo keyword is followed by the string in double quotes. Similarly, you can use a variable name with a $ sign to display the variable value.

Another Example of displaying with echo

The following piece of code shows how echo statement is used in different ways, including displaying different strings.



Dealing with quotes in echo

In many cases, you will need to display quotes (“”) inside your paragraphs or strings. Be careful when you are doing this with PHP echo statement. Since the echo uses quotes in the beginning and end of strings. In that case, you should use one of the methods below when your strings contain quotations.

  • Avoid using quotes inside the strings.
  • Use a backslash () within the strings to escape quotes. Before the quotation mark just place a backslash.
  • Inside your strings use single quotes (‘ ‘)

Check out the example below in order to get q better idea:



How to echo PHP Variables

Displaying variables in PHP by using the echo statement is quite simple. This is mentioned in the above section as well. See the following examples online to see how it works.

In the first example, three variables are used and assigned the values. After that, echo statement is used to display the values of those variables.

PHP echo

Experience this example online



Output is : Hello Mike. My name is: 4a

See another example with numbers

In this example, two variables with numbers are defined. The third variable is assigned the sum of other two variables. Finally, we used the echo statement to display the third variable.

Experience this example online



Output is : 30


Was this article helpful?

Related Articles

Leave A Comment?