PHP echo shorthand with Code Example

In this article, we will discuss the echo statement and how to write the PHP echo shorthand statement. The <php is used to identify the start of a PHP document. In PHP whenever it reads a PHP document, It looks for:

<?php ?>

It just processes the codes in between the above tags and leaves the other codes around them.

Introduction to the echo Statement

In PHP, the echo statement is use to show the output to the user. To display the output, the print statement is also commonly use. The echo command can alternatively be express as echo() in parenthesis. Unlike print, which can only produce a single expression, it may output several expressions. It has no written value and is more efficient than the print statement.

The following are simple examples of echo statements:

Example01:

<?php
echo "Hello Softhunt Community";
?> 

Output:

PHP echo img1

Note: the special case is when you write a PHP file the end tag can be omitted, Only the start tag is needed.

The below code works fine as well:

Example02:

<?php
echo "Hello Softhunt Community";

Output:

PHP echo img2

Substitute the echo Statement With the Shorthand Tag in PHP

The echo statement is a new concept to us. When dealing with PHP, we may need to employ HTML tags frequently. It’s possible that we’ll need to employ PHP inside HTML at some point. In that case, every time we want to write PHP, we must use the PHP opening tag <? php and the PHP closing tag ?>. If we want to show something, we’ll use the echo statement within the PHP tags. It’s possible that repeating this process will get boring.

We can use the <?= ?> shorthand tag of the echo tag instead of writing the echo statement inside the PHP tags. It’s a lot easier and faster than putting the echo statement between PHP tags.

For clearing the concept let’s have a look at some snippets of code.

Example03: Normal way

<?php
    $greetings = "Hello Softhunt Users!";
    echo "$greetings";
?>

Output:

img3

Example04: Shorthand way

<?php
$greetings = "Hello Softhunt Users!";
?>
  
<?= $greetings ?>
img4

Both of the above codes produce the same result, as you can see. So, we told the PHP interpreter, using short tags, that:

<? ?>

can be treated same as

<?php ?>

Example05: The above example can be further modified as follows:

<?=
$greetings = "Hello Softhunt Users!";
?>

Output:

img5

That’s all for this article if you have any confusion contact us through our website or email us at [email protected] or by using LinkedIn

Suggested Articles:

  1. How to Use PHP in HTML Pages
  2. Automatically Refresh or Reload a Page using jQuery Code Example
  3. How to Use CSS in PHP echo with code example

Leave a Comment