SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: print VS. echo

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Toronto, Canada
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    print VS. echo

    Hi there,

    Can someone please explain to me the difference (if one exists) between using "print()" and "echo()" when making scripts?

    I've purchased a few books on PHP, one being Kevin Yank's. I noticed that Kevin uses echo(), whereas the author of my two other books (PHP For The WWW & Advanced PHP For The WWW) uses print().

    Is it just a matter of preference?

    Any clarification would be appreciated.


    Thanks,
    ...Frank
    ...Shakes


    "Whether you think you can, or you think you can't, you're right!"

  2. #2
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    have a look at this thread...i had the same question some time ago http://www.sitepointforums.com/showt...threadid=57569
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Toronto, Canada
    Posts
    92
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Redux,

    Thanks... I wasn't able to find anything when I did a search, but I've read the old thread.

    Hehehe... I guess I'm in the same boat you were in... I may just stick to print.

    Have you found any other reasons, and have you switched to echo?


    Thanks!

    ...Frank


    PS: I like the layout of Spiritlive! Is it a PHP/mySQL-drive site?
    ...Shakes


    "Whether you think you can, or you think you can't, you're right!"

  4. #4
    gingham dress, army boots... silver trophy redux's Avatar
    Join Date
    Apr 2002
    Location
    Salford / Manchester / UK
    Posts
    4,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i usually stick to print...just a personal preference, no particular reason.

    yes, spiritlive is 100% php/mysql. also, there's some perl cgi script running each night as cron jobs that sort out certain database tasks through perl dbi...
    re·dux (adj.): brought back; returned. used postpositively
    [latin : re-, re- + dux, leader; see duke.]
    WaSP Accessibility Task Force Member
    splintered.co.uk | photographia.co.uk | redux.deviantart.com

  5. #5
    SitePoint Member
    Join Date
    May 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print(); is based on C, which PHP is created from.

    I personally use echo, but it makes no difference really.

    .icecube

  6. #6
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print() also has a return value which can be assigned to a variable.

    So in an application you could use it for error logging:

    PHP Code:
    $p = print("Thanks for visiting!");
    if(
    $p != 1)
        {
         ...do 
    something herre...



  7. #7
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But why?

    Wouldn't $p = Thanks for visiting work just as well?

    Thanks,
    ~someonewhois

  8. #8
    SitePoint Addict
    Join Date
    Feb 2002
    Location
    Atlanta, GA
    Posts
    342
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you assign print() to a variable it's going to output whatever you've passed to the print() function at the point of assignment.

    Being able to check the return value for a successful execution of the print() function is one more piece of data to use for whatever whenever...


  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo's slightly faster and less to type so i use it.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  10. #10
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by DR_LaRRY_PEpPeR
    echo's slightly faster and less to type so i use it.
    glad to see i'm on the same wavelength as the doc ... personally, i don't use print unless i have to...

  11. #11
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Print brings back bad memories of basic. I stick to echo. The only time I use print is for display modification. And that isn't really using print anyways
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  12. #12
    SitePoint Wizard
    Join Date
    Mar 2002
    Location
    Bristol, UK
    Posts
    2,240
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Both of my PHP books use print instead of echo, and I've just picked up the habit of using print along the way.

    -Sam

  13. #13
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the books use print() becuase they figure that you are a programmer, and earlier did stuff with C, C++, and VB.

    This means it would be easiest for you to use the same tags, if possible, so they use print().

    I for one use echo, never used print and never thought about print before.

    Well, I thought about it when I saw it in a tutorial...

    Thanks,
    ~someonewhois


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
  •