Quick Reach
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.
1
2
3
4
5
|
<?php
echo “My code is working”;
echo $variable_name; //To display by variable names
?>
|
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.
1
2
3
4
5
6
7
8
|
<?php
echo “Hello”;
//Outputs a string
echo $variable;
//Outputs a variable
echo “String with variables “ . $on . ” one line”;
//Outputs a string, then a variable, then a string. All are separated with a [.] period
?>
|
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
<?php
// This is wrong approach and produces an error
echo “<h1 class=”headerh1“>I love using PHP!</h1>”;
/* The compiler takes first 2 quotes “<h5 class=” as a complete echo statement
while start interpreting following code as php’s and not able to identify it
*/
// Escaping the code
echo “<h1 class=”headerh1“>I love using PHP!</h1>”;
// Using apostrophe
echo “<h1 class=”headerh1“>I love using PHP!</h1>”;
?>
|
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.
Experience this example online
1
2
3
4
5
6
7
8
|
<?php
$my_name = “Hello Bob. My name is: “;
$my_no = 4;
$my_letter = ‘a’;
echo $my_name;
echo $my_no;
echo $my_letter;
?>
|
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
1
2
3
4
5
6
7
8
9
10
11
|
<?php
$inta = 10;
$intb = 20;
$intc = $inta + $intb;
echo $intc;
?>
|
Output is : 30
Leave A Comment?