PHP Echo and Print Statement


In php there are two ways to get the output things in web browser : echo and print


As you saw in the previous lesson, the PHP command echo is a means of outputting text to the web browser. Throughout your PHP career you will be using the echo command more than any other. So let's give it a solid perusal!


echo and print Statements


echo and print are more or less the same. They are both used to output data to the screen.


The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters while print can take one argument. echo is marginally faster than print.


echo Statement


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


        <?php
            echo "Hello world!<br>";
            echo "I am learn PHP!<br>";
        ?> 
        

Output:


        Hello world!
I am learn PHP!

Echo with Variable


            <?php
                $txt1 = "Learn PHP";
                $txt2 = "studentsempire.com";
                $x = 1;
                $y = 2;

                echo "<h2>" . $txt1 . "</h2>";
                echo "Study PHP at " . $txt2 . "<br>";
                echo $x + $y;
            ?>
        

Output:


            

Learn PHP

Study PHP at studentsempire.com
3


print Statement


The print statement can be used with or without parentheses: print or print().


        <?php
            print "Hello world!<br>";
            print "I am learn PHP!<br>";
        ?> 
        

Output:


        Hello world!
I am learn PHP!

Print with Variable


            <?php
                $txt1 = "Learn PHP";
                $txt2 = "studentsempire.com";
                $x = 1;
                $y = 2;

                print "<h2>" . $txt1 . "</h2>";
                print "Study PHP at " . $txt2 . "<br>";
                print $x + $y;
            ?>
        

Output:


            

Learn PHP

Study PHP at studentsempire.com
3


careful when echoing quotes!


It is pretty cool that you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes! Echo uses quotes to define the beginning and end of the string, so you must use one of the following tactics if your string contains quotations:


  • Don't use quotes inside your string
  • Escape your quotes that are within the string with a backslash. To escape a quote just place a backslash directly before the quotation mark, i.e. \"
  • Use single quotes (apostrophes) for quotes inside your string.

echo - not a function


Echo is not a function, rather it is a language construct. When you use functions in PHP, they have a very particular form, which we will be going over later. For now, just know that echo is a special tool that you'll come to know





PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion