Several Methods To Output A Text Using PHP

There are several methods and techniques to print a message and get it rendered to your browser screen using PHP. Typically, as in C programming, PHP also supports somewhat similar and additional functionalities.The main aim of this tutorial is to show you all the methods to output same data using different available methods.

Before heading towards the full tutorial, let’s summarize the possibilities, we’ll discuss it later.
  1. Print()
  2. Printf()
  3. Echo()
  4. Heredoc
With these four methods, we can print any message. For better understanding we’ll use following data to print in the browser, which must result the same thing.
This is “quoted” sentences!
We’ll use all those 5 methods to print the same statement.

PRINT()

Let’s begin with Print().
Print() simply outputs the data which is passed to it. Let’s print our sample sentence using Print().
<?php
print ("This is \"quoted\" sentence!");
?>
This will output the following screen:

Note that we’ve used backslash (\) before the quote symbol.  This says the PHP to print the symbol after backslash as it is.
Similarly let’s try it little bit differently.
<?php
$word = "\"quoted\"";
print (“This is $word sentence!”);
?>

No comments

Powered by Blogger.