Differences between printf, print, echo, sprintf,vprintf, vfprintf, vsprintf in PHP

WTF!!! Look  make no mistake about it,  I do like PHP but why do we have so many different ways to output strings. It is confusing especially nowadays when  a programmer is expecting to know more than one language.  As I move from one language to another i keep forgetting the nuances between all of them.

I wish they could just create just one construct and then we could just use flags to access specific functionalities.

So what are the differences between printf, print, echo, sprintf,vprintf, vfprintf and  vsprintf  in PHP?

According to php.net here are the  differences. At least they are all on one page for quick reference.

print :

  • Output a string onto the screen – Also “print” is not a real function so you are welcome to remove the parentheses.
  • Because this is a language construct and not a function, it cannot be called using variable functions
  • Always returns a 1

echo :

  • Output one or more strings
  • Also not a real function but a language construct
  • Does not returns anything so its cannot behave as a function.

printf :

  • Output a formatted string
  • Returns the length of the outputted string

sprintf:

  • Returns a formatted string
  • Returns a string produced according to the formatting string format

vprintf:

  • Returns a formatted string
  • Operates as printf() but accepts an array of arguments, rather than a variable number of arguments.
  • Display array values as a formatted string according to format

vsprintf:

  • Display array values as a formatted string according to format
  • Operates as sprintf() but accepts an array of arguments, rather than a variable number of argument

vfprintf :

  • Write a formatted string to a stream
  • Write a string produced according to format to the stream resource specified by handle
  • Returns the length of the outputted string

Reference: http://www.php.net/manual/en/ref.strings.php

Let's share thisShare on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn