PHP echo Function Applications Syntax and Example Code

PHP echo Function

echo function is used to print some text output in the plain HTML markup. In the PHP block, echo function is the simplest way to print any text output.

echo function can be used to print text or variables in the PHP script.

Warning: In the PHP block, if you create a variable, the value of that variable is not displayed in the HTML output file.

Note: In the PHP block, if you perform an arithmetic operation the result of that operation must be printed with the help of the echo function.

PHP echo Function Example with String to Print

The most common use of echo function is to print some static string in the processed HTML output file.

Example: This PHP file have one PHP block. And it is showing how to use echo function code syntax.

<!DOCTYPE html>
<html>
	<body>
		<p>This example shows one echo function to print string.</p>
		<?php
			echo "String Output from PHP Block.";
		?>
		
		<p>This PHP block prints static string in the processed HTML output.</p>
	</body>
</html>

In the code above we have used one PHP blocks, having single echo function call. Now you can see the processed HTML file received by the browser.

echo Function Example with Printing Variable Values

In the PHP web server scripting language you can create a variable and you can perform arithmetic and logical operations on them.

To print the value of variable in the processed HTML output file, we mostly use the echo function.

Caution The PHP process engine actually evaluate the PHP block code. And the final processed file contain only HTML code, all the PHP block is replaced by its HTML output only.

Example: This PHP file have two PHP block. And it is showing, how you can use variables with echo function.

<!DOCTYPE html>
<html>
	<body>
		<p>This example shows PHP block with variables.</p>
		<?php
			$x=23;
			$y=45;
			$z=$x + $y;
			echo $z;
		?>
		
		<p>This PHP block prints addition of two number in the processed HTML output.</p>
		<?php
			echo "The addition of two numbers is " . $z;
		?>
	</body>
</html>

In the code above we have used two PHP blocks. In the screenshot below shows the browser output.

In the second PHP blocks we have used dot(.) operator, which is known as concatenation operator. The concatenation operator is used to join string in the PHP.

PHP echo Function Example and Code Syntax
Fig.1 – echo Function Example Output Screenshot.

Fig 1 clearly shows how you can print the value of variables in the HTML output with the help of echo function.

Note The PHP code is evaluated at the server side only, the client computer browser can run HTML code only.

echo Function Example with Printing HTML tags also

The echo string output is without any HTML markup by default, but you can print any HTML markup easily with echo function just like a simple string.

Example: This file have two echo function calls. The string output in the first echo is without HTML markup and second echo function with h2 heading markup.

<!DOCTYPE html>
<html>
	<body>
		<p>This example shows echo function and HTML tags.</p>
		<?php
			echo "This string is shown in normal default body text style";
			echo "<h2>This string is shown in h2 text style</h2>";
		?>
	</body>
</html>

If you are not including any HTML tag or markup than the string is shown in normal text style.

echo Example with HTML tag
Fig.2 – echo Example Output Screenshot with HTML tags.
You can leave a response, or trackback from your own site.

5 Responses to “PHP echo Function Applications Syntax and Example Code”

  1. Hello there! Would you mind if I share your blog with my twitter group? There’s a lot of people that I think would really appreciate your content. Please let me know. Cheers

  2. I am regular visitor, how are you everybody? This paragraph posted at this web page is actually good.|

  3. You should take part in a contest for one of the most useful websites on the net. I’m going to recommend this site!|

  4. Tory Halpin says:

    It’s a shame you don’t have a donate button! I’d certainly donate to this brilliant blog! I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account. I look forward to new updates and will share this blog with my Facebook group. Talk soon!|

  5. That is very attention-grabbing, You’re an excessively professional blogger. I’ve joined your feed and look forward to looking for more of your magnificent post. Additionally, I have shared your web site in my social networks|

Leave a Reply to the article


Learn PHP

Learning & Certifications
Follow Us
Facebook Icon   Linked In Icon   Twitter Icon  
Validation and Recognition

Valid CSS! Valid HTML5!          Protected by Copyscape