What is the main difference between php 'echo' and 'print'?

Discussion in 'Website Development & Design' started by bmwillsmith, Mar 18, 2014.

  1. bmwillsmith

    bmwillsmith
    uix_expand uix_collapse
    Member

    Joined:
    Jan 23, 2014
    Messages:
    380
    Likes Received:
    10
    I am not satisfied with my five years of web design experience and i am not settle down. As a human being, in order to compete this fastest growing world with advanced technology we have to learn every day, always...
    Recently i have started learning php also.

    Meanwhile i am seeing print and echo tags? can anyone clearly explain the difference between these two things?
     
  2. andrewc

    andrewc
    uix_expand uix_collapse
    Member

    Joined:
    Nov 12, 2013
    Messages:
    175
    Likes Received:
    21
    Speed. There is a difference between the two, but speed-wise it should be irrelevant which one you use. echo is marginally faster since it doesn't set a return value if you really want to get down to the nitty gritty.

    Expression. print() behaves like a function in that you can do: $ret = print "Hello World"; And $ret will be

    That means that print can be used as part of a more complex expression where echo cannot. An example from the PHP Manual:

    $b ? print "true" : print "false";

    print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only "," AND, OR and XOR are lower.

    Parameter(s). The grammar is: echo expression [, expression[, expression] ... ] But echo ( expression, expression ) is not valid. This would be valid: echo ("howdy"),("partner"); the same as: echo "howdy","partner";
    (Putting the brackets in that simple example serves no purpose since there is no operator precedence issue with a single term like that.)
    So, echo without parentheses can take multiple parameters, which get concatenated:

    echo "and a ", 1, 2, 3; // comma-separated without parentheses
    echo ("and a 123"); // just one parameter with parentheses

    print() can only take one parameter:

    print ("and a 123"); print "and a 123";
     
    • Like Like x 1
  3. bmwillsmith

    bmwillsmith
    uix_expand uix_collapse
    Member

    Joined:
    Jan 23, 2014
    Messages:
    380
    Likes Received:
    10
    Dear Andrew thank you so much for your clear explanation about echo and print. Actually I'm a beginner in php. In future also I need support from you....
     
  4. marklee

    marklee
    uix_expand uix_collapse
    Member

    Joined:
    Feb 11, 2014
    Messages:
    122
    Likes Received:
    4
    Both echo and print are the language constructs in PHP,In Echo we can take more than one parameter while in Print takes one parameter.Echo does not return any value while print always return Integer value.
     
  5. bmwillsmith

    bmwillsmith
    uix_expand uix_collapse
    Member

    Joined:
    Jan 23, 2014
    Messages:
    380
    Likes Received:
    10
    I think it is better to use echo in php coding?
     
  6. kailashkhatri

    kailashkhatri
    uix_expand uix_collapse
    Member

    Joined:
    Apr 1, 2014
    Messages:
    33
    Likes Received:
    0
    Echo for print the value..
    Print shows the array..
     
  7. thaversantosh

    thaversantosh
    uix_expand uix_collapse
    Member

    Joined:
    Jul 9, 2014
    Messages:
    68
    Likes Received:
    2
    In php you can use two function to print the line $ echo and $ print . Both use the same work . but there is some different .
    By using $ echo you can many words or line can be print . But if you use $ print then you can print only one world .
     
  8. kaufenpreis

    kaufenpreis
    uix_expand uix_collapse
    Member

    Joined:
    Jun 12, 2014
    Messages:
    68
    Likes Received:
    1
    I use echo and print_r much, and almost never use print.

    I feel echo is a macro, and print_r is an alias of var_dump.

    But that's not the standard way to explain the differences.
     
  9. bmwillsmith

    bmwillsmith
    uix_expand uix_collapse
    Member

    Joined:
    Jan 23, 2014
    Messages:
    380
    Likes Received:
    10
    You have explained me in a single sentence. Thank you it is very helpful for me.
     
  10. EUWindowsHost

    EUWindowsHost
    uix_expand uix_collapse
    Member

    Joined:
    Nov 19, 2013
    Messages:
    41
    Likes Received:
    0
    the difference is in the paramater.
    print can only takes one parameter and always return Integer value, while Echo can more than one and does not return any value
     
  11. australianlogos

    australianlogos
    uix_expand uix_collapse
    New Member

    Joined:
    Aug 9, 2014
    Messages:
    16
    Likes Received:
    0
    IN echo you can use multi parameters and in print only one parameter
     
  12. Anh

    Anh
    uix_expand uix_collapse
    Member

    Joined:
    Feb 24, 2011
    Messages:
    48
    Likes Received:
    1
    Echo is marginally faster compared to print as echo does not return any value.
     
  13. Topnews123

    Topnews123
    uix_expand uix_collapse
    Member

    Joined:
    Jul 17, 2014
    Messages:
    45
    Likes Received:
    0
    Parameters: echo can take more than one parameter when used without parentheses. The syntax is echo expression [, expression[, expression] ... ]. Note that echo ($arg1,$arg2) is invalid. Print only takes one parameter.
    Return value: echo does not return any value print always returns 1 (integer)
    Syntax: void echo ( string $arg1 [, string $... ] int print ( string $arg )
    What is it? In PHP, echo is not a function but a language construct.
    In PHP, print is not a really function but a language construct. However, it behaves like a function in that it returns a value.
     
  14. AJKpeter

    AJKpeter
    uix_expand uix_collapse
    Member

    Joined:
    Nov 14, 2011
    Messages:
    390
    Likes Received:
    7
    I believe that OP already got it!
     
  15. anirban09P

    anirban09P
    uix_expand uix_collapse
    New Member

    Joined:
    Dec 19, 2013
    Messages:
    28
    Likes Received:
    0
    PHP Print and Echo both output data to the screen in a same pattern but PHP Echo and PHP Print differ based on how they are structured.
     

Share This Page