Echo Statement In PHP

In this article, you will learn how to effectively use PHP echo statement in your PHP code, whether you are a beginner or an experienced developer. Learn how to use PHP echo, the versatile command that effortlessly injects dynamic content into your web pages, making them come alive with personalized messages, data, and more.

Let’s dive into the PHP echo statement and explore its world.

PHP echo Statement

In PHP programming, the echo statement is one of the most commonly used constructs for showing data on the screen.

A dynamic and interactive web page is created by using PHP echo, which is fundamental and essential to PHP programming.

It’s easy to use echo; we may consider it as PHP basics. Simply add your desired string or value inside the parentheses of the echo statement.

As an example, the following code displays the string “This is how echo works” on the screen:

echo Example: 1 

<?php echo ("This is how echo works"); ?>

echo can be used with or without parentheses: echo or echo().

Here is a simple example of how you can output text using the echo command (note that the text can be formatted in HTML markup):

Example: 

<?php echo "<h2>PHP Echo Statement!</h2>"; echo "PHP is scripting language"; echo "Learning PHP is great"; echo "This ", "is made ", "from ", "multiple parameters ", "in PHP. "; ?>

Example: 

<?php echo "<h2>ChatGPT</h2>"; echo "ChatGPT is a chatbot"; echo "It is Pre-trained Transformer"; echo "GPT ", "use data ", "and computation ", "techniques"; ?>

With the echo statement, you can output text and variables, as shown in the following example:

Example: 

<?php $string1 = "PHP is great"; $string2 = "Learn PHP from mrexamples.com"; $a = 4; $b = 7; echo "<h2>" . $string1 . "</h2>"; echo "<p>" . $string2 . "</p>"; echo $a + $b; ?>

Example: 

<?php $string1 = "ChatGPT"; $string2 = "Chat generative pre-trained Transformar"; $a = 3.1; $b = 7.4; echo "<h2>" . $string1 . "</h2>: <p>" . $string2 . "</p>"; echo $a + $b; ?>

Additionally, you can use PHP echo to concatenate multiple strings.

To do this, we use the period (.) operator, which joins two strings together.

Consider the following examples:

Example: 

<?php $first_name = "Elon"; $last_name = "Musk"; echo "This is " . $first_name . " " . $last_name . "."; ?>
PHP echo example output
PHP Echo

Example Explaination:

The code declares two variables, $first_name and $last_name, and assigns the strings “Elon” and “Musk” to them respectively.

The echo statement is used to output a string to the screen.

The string being output is “This is Elon Musk.”, which is constructed by concatenating several strings and variables using the dot (.) operator.

The dot operator is used to combine (or concatenate) multiple strings into one string.

 

 

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *