- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
PHP echo statement
The PHP echo statement is used to output some data on the screen or on the web. For example:
<?php echo "fresherearth.com"; ?>
The output of the above PHP example is:
The same (previous) example can also be created in this way:
<?php $txt = "fresherearth.com"; echo $txt; ?>
Let me create the same example in another way:
<?php $txt = "fresherearth.com"; echo($txt); ?>
It produces exactly the same output as the previous example because ($txt) is evaluated as ("fresherearth.com"), which is a valid expression.
Note: But let me tell you one thing: use parentheses only when you need to give priority to an expression being executed first. For example:
<?php echo(1+4) * 8; ?>
The output of the above PHP example is:
In the above example, if you remove the parentheses, then the output should be 33. This is the situation where we need to use parentheses. Otherwise, "echo" does not require any parentheses to output the data on screen.
PHP echo variable
<?php $x = 10; echo $x; echo "<hr>"; $x = 12.43; echo $x; echo "<hr>"; $x = "Hey!"; echo $x; echo "<hr>"; $x = "Hey,<br>What's Up?"; echo $x; ?>
The output of the above PHP example is:
PHP echo HTML
<?php echo "<h1>About Me</h1>"; echo "<p>Hey!<br>I am Lucas from Frankfurt, Germany.</p>"; echo "<p>I am able to handle multiple tasks on a daily basis.</p>"; ?>
The output of the above PHP example is:
PHP echo with multiple parameters
<?php echo "I ", "use", " a creative approach to solve a problem"; $x = "use"; $y = "a creative approach to solve a problem"; echo "<br>"; echo "I $x $y"; echo "<br>"; $a = "I "; $b = "use"; echo $a . $b . $y; echo "<br>"; echo $a . $b . " " . $y; echo "<br>"; echo $a, $b, " ", $y; $x = 10; $y = 20; $z = 25; echo "<br>"; echo $x + $y - $z; ?>
The output of the above PHP example is:
PHP echo statement example
Let me create one more example of an echo statement in PHP that may enhance your skill towards echo.
<?php echo "codes"; echo "cracker"; echo "<hr>"; echo "codes", "cracker"; echo "<hr>"; echo "Hey, I just love coding. I hope you too."; echo "<hr>"; echo "The value is: ", 2*43; ?>
The output of the above PHP example is:
Advantages of echo statement in PHP
- Simple to Use: There are no special coding skills needed to use the "echo" statement. It is a crucial component of PHP and is widely used in web development.
- Quick Output: When it comes to outputting data in PHP, the "echo" statement is quicker than print or printf. This is so that it can be executed more quickly because it doesn't return a value.
- Compatible with HTML: The "echo" statement can be used to output HTML code directly to the browser because it is compatible with HTML. This facilitates the development of dynamic web pages that can be instantly updated.
Disadvantages of echo statement in PHP
- Code Clutter: Using the "echo" statement extensively in a PHP script can result in a lot of code clutter. As a result, the code may be more challenging to read and maintain.
- Combining PHP and HTML: It can be challenging to distinguish between PHP and HTML code when using the "echo" statement to output HTML code. If errors do occur, this may make it more difficult to debug the code.
- Security Risks: If user input is not properly sanitized before being output to the browser, the "echo" statement may be subject to security risks. This may make it possible for malicious code to be injected onto the page, creating security holes.
« Previous Tutorial Next Tutorial »