Echo and Print statements

Echo and print Statements.
  • In PHP there are two basic ways to get the output: echo and print.
  • echo and print are almost both are same. They are used to output data to the screen.
Echo Statement
  • PHP echo statement can be used to get the output of the string, multi-line strings, escaping characters, variable, array, etc.
  • echo is faster than the print statement.
  • PHP echo is a language construct, not a function.
  • echo can be used both types: echo(), and echo.
  • echo does not return any value.
  • We can pass multiple strings separated by a comma , in echo.
  • examples of echo statement
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    echo "<h3>PHP is Fun!</h3>";
    
    echo "Hello PHP Coders!<br>";
    
    echo "This ", "string ", "was ", "made ", "with multiple parameters.<br/>";
    
    echo "I'm about to learn PHP from PHP Coders point!<br/>";
    
    echo "Hello PHP coders
    this is multi line
    text printed by
    PHP echo statement.<br/>";
    
    echo "Hello PHP Coders escaping characters : \"sequence\" characters.";
    ?>
    </body>
    </html>
    
  • examples of echo statement with variables
  • <!DOCTYPE html>
    <html>
    <body>
    <?php
    $var1 = "Learn PHP with php coders point";
    $var2 = "phpcoderspoint.com";
    $a = 1;
    $b = 2;
    echo "<h2>" . $var1 . "</h2>";
    echo "Study PHP at " . $var2 . "<br/>";
    echo $var1." at " . $var2 . "<br/>";
    echo $a + $b;
    ?>
    </body>
    </html>
    


    Print Statement
    • The print statement can be used with or without parentheses: print or print().
    • PHP print statement can be used to print the string, multi-line strings, escaping characters, variable, array, etc.
    • print is a statement, used as an alternative to echo at many times to display the output.
    • print always returns an integer value, which is 1.
    • Using print, we cannot pass multiple arguments.
    • print is slower than the echo statement.
  • examples of print statement
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    print "<h3>PHP is Fun!</h3>";
    
    print "Hello world!<br>";
    
    print ("I'm about to learn PHP!<br>");
    
    print "Hello by PHP print
    this is multi line
    text printed by
    PHP print statement<br>";
    
    print "Hello PHP Coders escaping characters : \"sequence\" characters.<br>";
    
    ?>
    
    </body>
    </html>
    
  • examples of print statement with variables
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $var1 = "Learn PHP with php coders point";
    $var2 = "phpcoderspoint.com";
    $a = 1;
    $b = 2;
    print "<h2>" . $var1 . "</h2>";
    print "Study PHP at " . $var2 . "<br/>";
    print $var1." at " . $var2 . "<br/>";
    print $a + $b;
    ?>
    
    </body>
    </html>
    
  • sometimes echo or print can't work
  • <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $name1 = "am";
    $name2 = "it";
    echo "My name is: ".$name1,$name2;
    print "My name is: ".$name1,$name2;
    ?>
    
    </body>
    </html>
    

    Output:

    My name is: amit
    Parse error: syntax error, unexpected ',' in C:\xampp\htdocs\example\p5.php on line 9
    Note: hear the print statement can't work
    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    $lang = "Learn PHP";
    $echstm = ehco $lang." with php coders point. with echo statement";
    $pristm = print $lang." with php coders point. with print statement ";
    
    echo "echo statement : ".$echstm;
    print "print statement: ".$pristm;
    ?>
    
    </body>
    </html>
    

    Output:

    Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\xampp\htdocs\example\p7.php on line 7
    Learn PHP with php coders point. with print statement.
    echo statement : Learn PHP with php coders point.
    print statement: 1