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.
- Print()
- Printf()
- Echo()
- 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!”);?>
This will print the same result.
Literally, if we want to put $word as it is then you must add backslash to it like this:
<?php
$word = "\"quoted\"";
print (“This is \$word sentence!”);?>
This will render the following screen:
The variable $word will remain unused in this case.
Ok this was with print(). Before using print() you must know it’s prototype. The prototype of print() is int print(argument). With int, you must know that it returns a value and this will not be practical for core programming. But it’s ok if you are just using it to print only a statement and as far as you are sure about the value return does not affect your script.
ECHO()
If you don’t find printf() good for you, alternatively, you can use echo(). Echo() has it’s prototype void echo(string argument).
The echo() does not return any value, and also echo() can handle multiple strings. Here’s how we can get the result we want using echo.
<?phpecho ("This is \"quoted\" sentence!");
?>
We’ll get exactly same result from this method.
Talking about the speed between ECHO and PRINT, we’ll get faster result from ECHO as it does not returns any value. But the speed difference is almost negligible.
PRINTF()
Like in C programming, you can specify the placeholder within the string itself. This is not particle to our example (This is “quoted” sentence!). Because we don’t need any place holder here. However you can place one of them.
Try the following example,
<?php
$value="\"quoted\"";
printf(“This is %s sentence!”, $value);
?>
Note that %s, specifies that it must hold the value of string and not anything else.
For clearer view, here is another example.
<?php
$variable_value=100;
printf(“%d is a decimal!”, $variable_value);?>
This will result:
We’ve specified the placeholder to put a decimal value there. Again, here is another example demonstrating how PHP recognizes the float, string and decimal value.
<?php
$variable_value=100;
$variable_string=”String”;
$variable_float=10.155;
printf(“%d is a decimal! and %f is a float! and %s is a string”, $variable_value, $variable_float, $variable_string);?>
Here is the output:
HEREDOC
Heredoc is somewhat different from the above examples. Heredoc syntax offers to output large amount of texts. In Heredoc, rather than delimiting strings with double or single quotes, two identical identifiers are employed. Another good thing about heredoc is you don’t need to escape (use backslash) to get the quotes and other symbols.
Refer the following example:
<?php
echo <<<TEXT
This is “quoted” sentence!
TEXT;?>
This will render the same result as with others. Note that, the delimiter TEXT that we’ve assigned must be identical to the closing delimiter. Also note that we’ve used three left angled brackets before the delimiter. Also remember that you must not leave any spaces between the delimeter and main text.
For example, this will be assumed wrong code.
<?php
echo <<<TEXTThis is "quoted" sentence!TEXT;
?>
This will also be assumed wrong:
<?php
echo <<<TEXT
This is “quoted” sentence!
TEXT;
?>
Because, we’ve white space just before TEXT;.
Also another wrong way of Heredoc implementation:
<?php
echo <<<TEXT
This is “quoted” sentence!
text;?>
Note that TEXT is not identical to text.
So these were the four methods to get output in your screen. To choose the better method is dependent upon your choice.