Echo and Print Statement

there are two basic ways to get output: echo and print.


PHP echo and print Statements

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

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 "<h2> hello world </h2>";
        ?>
    

Display Variables

The following example shows how to output text and variables with the echo statement:

    
        <?php
        $txt1 = "hello";
        $txt2 = "world";
        $x = 5;
        $y = 4;

        echo "<h2>" . $txt1 . "</h2>";
        echo  $txt2;
        echo $x + $y;
        ?>
    

Print Statement

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

    
        <?php
            print "Hello world !";
        ?>