echo and print statements in PHP



echo and print statements in php


In PHP two basic ways to get output: echo and print. Means they are both used to output data to the screen.

Echo statement

In PHP ‘echo’ statement is a language construct and not a function, so it can be used without paranthesis. But we are allowed to use paranthesis with echo statement when we are using more than one arguments with it. The end of echo statement is semi-colon (;).

We can use ‘echo’ to output strings or variables.
  • Displaying string
  • Displaying multiple arguments strings
  • Displaying Variables


Example:
<?php
            echo “This is single string.”;
            echo “First arguments”, ”Second arguments”, ”Third arguments”;
            /* echo with variable */
            $text = “Cyber Teak”;
            $number1 = 100;
            $number2 = 200;
            echo $text;
            echo $number1.”+”.$number2.”=”;
            echo $number1+$number2;
?>


Print Statement

The PHP print statement is similar to the echo statement and can be used alteranative to echo at many times.It is also language construct and so we may not use parenthesis : print or print(). The main difference between the print and echo statement is that print statement can have only one agrument at a time and thus can print a single string.

Print statement always a value 1.

Print statement can also be used to print strings and variables.

·       1. Displaying string
2. Displaying Variables

Example:

<?php
     print “Welcome in www.cyberteak.com”;

     $text = “Hello Cyber Teak”;

     $num1 = 100;

     $num2 = 200;

     print $num1.”+”.$num2.”=”;

     print $num1+$num2;
?>

Comparison between Echo and Print



echo
print
type
This is a type of output string.
This is a type of output string.
parenthesis
Not written with parenthesis.
It can or can’t be written with parenthesis.
strings
It can output one or more strings.
It can output only one string at a time, and returns 1.
functionality
echo is faster than print.
print is slower then echo.
argument
echo($arg1[, $arg2…])
Print($arg)

Post a Comment

0 Comments