PHP echo and print statement
PHP echo and print statement:
There are two basic methods in PHP to get output.
-
echo
-
print
We have used echo (print) in most of the examples in this PHP tutorial. These two output statements are lightly discussed below.
PHP echo and print statement:
The echo and print statements are almost identical. Because both are used to take output in browser / console.
Although the two statements work the same, there is a slight difference: echo has no return value. But the return value of print is 1. So print can also be used in expressions.
echo can accept more than one parameter but print can only accept one argument.
echo works relatively faster than print.
PHP echo statement:
Echo statements can be used with or without parentheses. E.g. echo or echo ()
We will see with the help of the following example how to get text output through echo statement. You will notice that the text also contains HTML markup:
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- echo "Hello World! <br>";
- echo "Hello PHP! <br>";
- echo "Only Learn 24 ", "Academy ", "is the largest", "online / offline", "free IT academy", "in Bangladesh.";
- ?>
- </body>
- </html>
Output:
Hello World!
Hello PHP!
Only Learn 24 Academy is the largestonline / offlinefree IT academyin Bangladesh.
With the help of the following example we will see how to take text and variable output:
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- $text1 = "We are all";
- $text2 = "IT lovers";
- echo "$text1 $text2 <br>";
- echo "We are all IT lovers";
- ?>
- </body>
- </html>
Output:
We are all IT lovers
We are all IT lovers
PHP print statement:
The print statement can be used with or without parentheses. E.g. print or print ()
We will see with the help of the following example how to get the text output through print statement. You will notice that the text also contains HTML markup:
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- print "Hello World! <br>";
- print "Hello PHP! <br>";
- print "Hello Only Learn 24";
- ?>
- </body>
- </html>
Output:
Hello World!
Hello PHP!
Hello Only Learn 24
With the help of the following example we will see how to take text and variable output through print statement:
Example:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <title>This is php tutorial</title>
- <link rel="stylesheet" href="">
- </head>
- <body>
- <?php
- $textPartOne = "We are all";
- $textPartTwo = "IT lovers";
- print "$textPartOne $textPartTwo <br>";
- print "We are all $textPartTwo <br>";
- ?>
- </body>
- </html>
Output:
We are all IT lovers
We are all IT lovers
shelleyakter
plz give me new post