Please note, this is a STATIC archive of website technosmarter.com from July 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
 

echo() function in PHP with examples


The Echo function is known as the built-in function of PHP programming. We discussed built-in functions in previous tutorials. The echo function is used to get output parameters. Understand in the simple language, before PHP programming you learned about C programming or have done some of its programs, in which many functions are already made(Built-in functions). The echo function is already created in PHP. We use the echo function in PHP, just like we use the printf function in C programming for output the result. In PHP, we can also use print, printf functions for output. The echo function is used for more than one strings.

Definition of echo function in PHP

The echo function is a bill-in function (predefined function, readymade) which is used for more than one string. In simple words, we can use more than one strings (variables, parameters ) with echo function.

More about echo function in PHP -

echo is not really a function (it is a language construct), so you don't need to use parentheses () with it. For example, brackets are always used with a function, but there is no need to use any brackets with echo. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Many values are printed by echo, but echo does not return any value.

Why not use print, printf functions most instead of PHP echo

As you may be aware, when we do programming, a big focus on the speed(Output speed or display speed). When you create a PHP website, it is kept at the highest speed so that the output will show soon. In PHP the print function and printf can be output one string, then why do you use the echo function in PHP most often. The answer is Output speed. The speed of output of echo function to produce (execute) is higher than the printf and print functions, and echo function outputs more than one string at a time. That's why we use the echo function in PHP programming.

Synaxt of echo function in PHP

Let’s understand echo function via syntax.


 <?php 
echo " " ; 
?>

 <?php 
echo ' '; 
?>

In the syntax above, we can use both single and double quotes with echo.

echo function with Single quotes in PHP


 <?php 
 echo 'You are learning PHP on Techno Smarter website'; 

?>

Output-

You are learning PHP on Techno Smarter website

In this way, any string can be easily output by writing in single quotes.

echo function with double quotes in PHP -


 <?php 
 echo "I am learning PHP"; 

?>

Output-

I am learning PHP

Output can also be obtained by double quotes in the same manner as we did in single quotes above.In this way , we can get same output with double quotes with echo function.

Getting value of variable with echo function

Along with the string , the value of the variable can also be outputted.


<?php
$roll_no='789019';
 echo $roll_no; 
?>

Output-

789019

In the example above , we store the value in a variable and output the value of that variable by the variable name using echo.There is no need to write any single or double quotes with variables here.

Output more than one string using echo function

If you already know about string, if you do not then you can go to the PHP string tutorial. You can use any single or double quotes to display more than one string. We use commas between strings. .Let's understand with an example.


 <?php 
 echo'You',' are',' learning',' PHP',' on',' Techno',' Smarter';  

?>

Output-

You are learning PHP on Techno Smarter



 <?php 
echo "You"," are"," welcome"," on"," PHP"," Tutorials";    
?>

Output-

You are welcome on PHP Tutorials

In the examples above, you can understand how many strings can be output by single or double quotes and commas simultaneously by the echo.

Getting values of more than one variable using echo

In the examples and approaches above we have used more than one string. We can store a string as the value in variables and can use it with echo function for output values.

Lets us understand with an example.


 <?php 
$ch1='Learning'; 
  $ch2='echo'; 
   $ch3='in PHP'; 
    $ch4='with example';  
    echo  $ch1. $ch2. $ch3. $ch4; 
    echo '
'; // for space use quotes echo $ch1.' '.$ch2.' '.$ch3.' '.$ch4; echo '
'; // using double quotes echo $ch1." ".$ch2." ".$ch3." ".$ch4; ?>

Output-

Learningechoin PHPwith example
Learning echo in PHP with example
Learning echo in PHP with example

In the example above, the value of more than one variable is displayed. If you want to add some space between variable values, then you can add space by using single or double-quotes with an echo function.

How to display HTML code using echo in PHP

echo is not a function but in PHP the concept has created that it is called a function. The HTML code is also displayed by the echo function.

Let's us understand by example –


 <?php 
echo "

My website main heading

My website paragraph

" ; ?>

Output-

My website main heading

My website paragraph

In the example above you can see how the HTML code is displayed by echo function.

Displaying the value of PHP variable with HTML code by echo function in PHP

When create PHP website, we use many variables in HTML code to display the variable value.


 <?php 
$std_name="Sohan"; 
$his_mrk="160"; 
$sci_mrk="190"; 
$total=$his_mrk+$sci_mrk; 

 echo 'Hello Dear '. $std_name.'! You got '.$his_mrk.' marks in History and '.$sci_mrk.' marks in Science. You got '. $total.' total marks out of 400.';  
?>

Output-

Hello Dear Sohan! You got 160 marks in History and 190 marks in Science. You got 350 total marks out of 400.

In this way, the value of PHP variables is displayed in the HTML code.


Please Share

Recommended Posts:-