Results 1 to 1 of 1

Thread: PHP 5 echo and print Statement

  1. #1
    Join Date
    Nov 2009
    Posts
    76,596

    Default PHP 5 echo and print Statement

    PHP echo and print Statements

    There are some differences between echo and print:

    echo - can output one or more strings
    print - can only output one string, and returns always 1

    Tip: echo is marginally faster compared to print as echo does not return any value.

    The PHP echo Statement

    echo is a language construct, and can be used with or without parentheses: echo or echo().

    Display Strings

    The following example shows how to display different strings with the echo command.

    Code:
    <?php
    echo "<h2>PHP is fun!</h2>";
    echo "Hello world!<br>";
    echo "I'm about to learn PHP!<br>";
    echo "This", " string", " was", " made", " with multiple parameters.";
    ?>
    Display Variables

    The following example shows how to display strings and variables with the echo command:
    Code:
    <?php
    $txt1="Learn Programming";
    $txt2="BizHat.com";
    $cars=array("Volvo","BMW","Toyota");
    
    echo $txt1;
    echo      "<br>";
    echo "Study PHP at $txt2";
    echo "My car is a {$cars[0]}";
    ?>
    The PHP print Statement

    print is also a language construct, and can be used with or without parentheses: print or print().

    Display Strings

    The following example shows how to display different strings with the print command

    Code:
    <?php
    print "<h2>PHP is fun!</h2>";
    print "Hello world!<br>";
    print "I'm about to learn PHP!";
    ?>
    Display Variables

    The following example shows how to display strings and variables with the print command:

    Code:
    <?php
    $txt1="Learn PHP";
    $txt2="hostonnet.com";
    $cars=array("Volvo","BMW","Toyota");
    
    print $txt1;
    print "<br>";
    print "Study PHP at $txt2";
    print "My car is a {$cars[0]}";
    ?>
    Last edited by sherlyk; 04-16-2014 at 05:30 AM.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •