Hi,

Can you explain what is different between echo and print ?

Many thanks

kula

    nothing. just peronsal preference. echo has 4 letters print has 5 _

      This one confused me for a while, although Guru is right - it's just personal preference. I think there is one important difference, which is that you can use echo to call a function:

      echo my_function($params);
      

      I don't think you can use print for this. However, I'm not sure if echo is even necessary to call the function. I use it because that's what it says in my book and I'm just learning.

      I personally use print for creating HTML tags - i.e. things you can't see in the browser - and echo for things that you can see in the browser. The distinction is rarely clean, but it helps me to keep things straight in my head.

      Norman

        Some people claim that echo() is a more streamlined function than print() but I have no idea if that is true. I use echo() because it is less to type but when I started I used print() because it was easier to understand when skimming through code.

        Btw Norman you can call a function just by doing my_function(); if used with echo the echo should print out what (if anything) the function returns. Here is a quick example

        function bob(){
        $text = "bob";
        return $text;
        }

        echo(bob());

        browser output
        bob

          I just found this out myself a few days ago. Amazing what is in the manual.

          print only takes one argument and displays it.

          echo can take multiple arguments separated by commas.

          echo 'Hey there ',
          a_function_call(),
          "Or something $else";

          echo '<option>',
          $content,
          '</option>;

          Note that you don't need to use the . operator to get one long string. I have not dug into the code but it seems reasonable that using commas instead of dots will improve performance a bit.

            Write a Reply...