30 September 2013

What are the difference between echo() and print() in PHP ?

  • To better understand the difference, you have to know the syntax with example of echo() and print(). Syntax of echo() :
void echo ( string $arg1 [, string $... ] )

  • Example of echo():
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
<?php

    echo ("The quick brown fox.");

    echo "The quick brown fox.";

    $dept_name="HR";

    echo "My department name is :$dept_name";

    echo"<p>

    Another message 1 Another message 2

    Another message 3

    </p>";

    ?>

  • Syntax of print()
int print ( string $arg )

  • Example of print():
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
    <?php

    print("The quick brown fox.");

    print "The quick brown fox.";

    $dept_name="HR";

    print "My department name is :$dept_name";

    print"<p>

    Another message 1 Another message 2

    Another message 3

    </p>";

    ?>

  • echo() is used to output one or more strings print() is used to output a string. 
  • Both echo() and print() are not actually a real function, it is a language construct. 
  • So you are not required to use parentheses with its argument list. 
  • echo() can take more than one parameter when used without parentheses. 
  • print() only takes one parameter. 
  • echo() is not a real function ,so it does not return any value. Though print() is also not a real function, but it will always returns 1. 
  • echo() has a shortcut syntax, this short syntax only works if the short_open_tag configuration setting on php.ini is enabled. 
  • Example of short_open_tag:
1
2
3
4
5
6
7
<?php

    $blog_address="http://shafiq2410.wordpress.com/";

    ?>

    <p>My Blog address is: <a href="<?=$blog_address?>" target="_blank"><?=$blog_address?></a></p>

PHP & MySQL: Novice to Ninja: Get Up to Speed With PHP the Easy Way

No comments:

Post a Comment

Featured Post

How to Write PHP code and HTML code within a same file

A PHP file can have both PHP code and HTML code together. Any HTML code written outside of PHP <?php //php code here… ?> Code is ig...

Popular Posts