Tutorials

PHP for beginners -Lesson 2

Sending
User Rating 5 (1 vote)

Usages of echo, print, print_r, var_dump(), printf()

Echo:
1) echo does not return a value.
2) This is faster than 'print' (*)
3) You can pass multiple expression

Print:
1) print return a value
2) This is slower than echo (*)
3) it takes single expression

The working example on different way to print the output on screen is given here.
 * I tried to print 'Hello world' 10000 times using echo & print , So i found 20% time differences.
 but let me clear one more thing that, with 1000 times of loop, both was taking same time.
 
 Funny difference :
 Its a human nature to be lazy, because of this reason most of php developer use echo rather than print.
 echo is 4 character length and print is five. "T" is a extra finger point.

  <?php echo "Hello" . "aliencoders!
"; // Concatenation of two string slow down our process echo "Hello" , "
"; echo "aliencoders" , "
"; // Calling Echo multiple times still is also not good echo "Hello" , "aliencoders!" , "
"; // Best! In a large loop, this could save a couple of seconds. ?>

Conclusion : So what do we use? Echo of course!

print_r : syntax : mixed print_r(mixed $expression , bool $return);
            prints human readable information about variable.
            Generally use for printing array or object
           
printf() : Same as C programming – output a formatted string

var_dump() : void var_dump(mixed $expression,mixed $expression)
            Gives deatails information about variable,array,object etc..

Next, we will discuss about syntax of : Conditional statement –  if, else, nested if
                                        Looping – for, while, do while & foreach
                                        With Sample program

For more queries , post your comment here.

Share your Thoughts