What the different between print and echo

27 replies
Hi old warrior member, What is difference between print and echo
#echo #print
  • Profile picture of the author mywebwork
    Who are you calling "old"????

    I assume you are speaking about PHP here, as many other languages use similar statements.

    Technically echo is a bit faster than print, although we are probably talking nanoseconds here. Print also has a return value (i.e true or false), where as echo doesn't. However echo can accept multiple statements but print can't (i.e echo "The first", "the second";).

    For all practical purposes use the one that makes you happy! I guess echo has one less character to type!

    Bill
    {{ DiscussionBoard.errors[2335224].message }}
  • Profile picture of the author omonoiavas
    Not much difference between print and echo.
    The main differnce is that echo can take multiple expressions and its a bit faster than print
    Signature

    {{ DiscussionBoard.errors[2335551].message }}
  • Profile picture of the author godwinsam
    print does the same thing as echo except echo is a language construct and not a real function so it's syntax is different.

    echo is a statement
    while print is a function
    we can't print whole array by using echo or print. For this we have to use the print_r function
    {{ DiscussionBoard.errors[2336180].message }}
  • Profile picture of the author jaratvit
    A lot of people seem to prefer echo because it runs a little faster (very likely an insignificant amount of time will be gained).

    There are a few situations in more complicated code where print is preferred because it can return a true/false where echo does not.

    Very few people care about these technical differences and use them interchangeably.
    {{ DiscussionBoard.errors[2336253].message }}
  • Profile picture of the author Luxmo
    Personally, I use echo for generic printing to the document..specially when there is a lot of printing to be done. I only use the print function when there is further formatting or processing needed...like making sure something was printed using the bool return from the print function or formatting strings (like currency formats). Echo is faster than print and there is a noticeable speed difference when printing large strings.

    To sum it up:
    Use echo when printing anything.
    Only use print when you need to print something in a special way or for bool testing.

    <My two pennies>
    {{ DiscussionBoard.errors[2424664].message }}
  • Profile picture of the author ginny.smith73
    echo can take multiple expressions + bit faster than print
    {{ DiscussionBoard.errors[2427877].message }}
    • Profile picture of the author rockmartinn24
      The different between print and echo :

      - Echo works faster than print()
      - Echo and print are language construct
      - Echo is not suitable for conditional construct
      - Print is suitable for conditional construct
      - Print process is slower than echo
      - Print returns an integer values
      - Echo does not returns any values
      {{ DiscussionBoard.errors[2557237].message }}
      • Profile picture of the author abhishekcis
        Yes not much difference between print and echo.
        Only syntactically echo is different with print
        {{ DiscussionBoard.errors[3373255].message }}
        • Profile picture of the author tirupati
          Print : Prints the Line with formatted output
          Echo : Just print and goes to next line
          {{ DiscussionBoard.errors[3373372].message }}
  • Profile picture of the author sixpack01
    The difference between print() & echo()?

    1. Echo() can take multiple expression
    eg: Echo 'sundar','MCA'; // sundarMCA

    2. Print() cannot take multiple expression
    eg print 'sundar','MCA'; // Parse Error

    3. Echo() doesn`t return any values.

    4.print() return values.
    {{ DiscussionBoard.errors[3571090].message }}
  • Profile picture of the author gtk29
    Basic difference is that echo is not a function like print(). As such echo runs faster than print().
    {{ DiscussionBoard.errors[3571250].message }}
    • Profile picture of the author joshepmacarona
      Prints the Line with formatted output. Echo is just print and goes to next line. Echo is not best for conditionally construct. Print is best for conditionally construct.
      {{ DiscussionBoard.errors[3614822].message }}
  • Profile picture of the author esoomllub
    The funny thing to me is that even though I know they are pretty close to the same, I find myself constantly changing prints to echos when I take over code from someone else. I realize that it is a waste of time, but still can not find the willpower to stop myself. Argh.
    Signature
    {{ DiscussionBoard.errors[3616377].message }}
  • Profile picture of the author yourmobisite
    Some people seem to prefer echo because it runs a little faster.There are a few situations in more complicated code where print is preferred because it can return a true/false where echo does not.
    {{ DiscussionBoard.errors[3624887].message }}
  • Profile picture of the author Steve Wells

    Print:
    a process for reproducing text and image, typically with ink on paper using a printing press. It is often carried out as a large-scale industrial process, and is an essential part of publishing and transaction printing

    Echo: Repetition of a sound by reflection of sound waves from a surface

    Signature
    Need Custom Graphics Work? - Message Me For A Design Quote!
    {{ DiscussionBoard.errors[3630693].message }}
    • Profile picture of the author hhunt
      Originally Posted by Steve Wells View Post


      Print:
      a process for reproducing text and image, typically with ink on paper using a printing press. It is often carried out as a large-scale industrial process, and is an essential part of publishing and transaction printing

      Echo: Repetition of a sound by reflection of sound waves from a surface

      You may have misunderstood the question - the print and echo in this context relate to programming languages; rather than the real world interpretation of it.
      {{ DiscussionBoard.errors[3631458].message }}
      • Profile picture of the author Steve Wells
        Originally Posted by hhunt View Post

        You may have misunderstood the question - the print and echo in this context relate to programming languages; rather than the real world interpretation of it.
        Naay, just having fun....
        Signature
        Need Custom Graphics Work? - Message Me For A Design Quote!
        {{ DiscussionBoard.errors[3688376].message }}
  • Profile picture of the author hireava
    Main difference between echo() and print() is that echo is just an statement not a function and doesn't return's value or it just prints a value whereas print() is an function which prints a value and also it returns value.
    Signature
    {{ DiscussionBoard.errors[3631817].message }}
  • Profile picture of the author alexacc
    Lagest difference is: Print is suitable for conditional construct - but echo - not
    {{ DiscussionBoard.errors[3658436].message }}
  • Profile picture of the author kitchensimply
    echo has the slight performance advantage because it doesn't have a return value. They both have the same functions but have different procedures that result to the same output.
    {{ DiscussionBoard.errors[3691955].message }}
  • Profile picture of the author Janessabourne
    As for echo statement.it doesn't return value of , the print which always return one and 0.

    {{ DiscussionBoard.errors[3703925].message }}
    • Profile picture of the author xrvel
      For example, you can use this with print.
      PHP Code:
      mysql_query() or print(mysql_error()); 
      You can't do it with echo
      PHP Code:
      mysql_query() or echo(mysql_error());// will fail 
      Signature

      Hello world

      {{ DiscussionBoard.errors[3704285].message }}
  • Profile picture of the author ronspals
    Echo can take multiple parameters to echo.This is not true with print. The "echo" and "print" tests can be tailored depending on what you are actually using it for. In general echo is faster, although there's cases where print can be faster.
    {{ DiscussionBoard.errors[3704801].message }}
  • Profile picture of the author oknoorap
    echo vs. print

    Benchmarks :

    Code:
    echo ''
    Total time: 159 µs

    Code:
    print ''
    Total time: 138 µs

    Code:
    echo 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    Total time: 194 µs

    Code:
    print 'aaaaaaaaaaaaaaaaaaaaaaaaaaaa'
    Total time: 178 µs

    Code:
    echo 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'
    Total time: 217 µs

    Code:
    echo 'aaaaaaa','aaaaaaa','aaaaaaa','aaaaaaa'
    Total time: 521 µs

    Code:
    print 'aaaaaaa'.'aaaaaaa'.'aaaaaaa'.'aaaaaaa'
    Total time: 245 µs

    Code:
    $ var = 'aaaaaaa';
    echo 'aaaaaaa'.$ var.'aaaaaaa'.
    Total time: 622 µs

    Code:
    $ var = 'aaaaaaa';
    echo 'aaaaaaa',$ var,'aaaaaaa',
    Total time: 794 µs

    Code:
    $ var = 'aaaaaaa';
    print 'aaaaaaa'.$ var.'aaaaaaa'.
    Total time: 890 µs

    Code:
    $ var = 'aaaaaaa';
    echo $ var.$ var.$ var.$ var
    Total time: 937 µs

    Code:
    $ var = 'aaaaaaa';
    echo $ var,$ var,$ var,$ var
    Total time: 894 µs

    Code:
    $ var = 'aaaaaaa';
    print $ var,$ var,$ var,$ var
    Total time: 1009 µs

    remove space between dollar and var.
    Signature

    Just Web Developer.

    {{ DiscussionBoard.errors[3725727].message }}
  • Profile picture of the author dealsbells
    Echo is a statement print is a function.echo print the multiple function but print function print statement.The only difference between print & echo is that both of them displays leads to different format.
    {{ DiscussionBoard.errors[3796674].message }}
  • Profile picture of the author CodeMyConcept
    Both do the same job, but there is a slight difference between print and echo so it usually comes down to a matter of personal preference.
    {{ DiscussionBoard.errors[3797491].message }}
  • Profile picture of the author UMS
    Given the programming language wasn't specified, I'll assume bash as the language.

    In which case,

    echo is a builtin bash function, whereas print is an external command and therefore requires the overhead (albeit small) to fork a new process.
    {{ DiscussionBoard.errors[3804448].message }}

Trending Topics