PHP is one of the most frequently used Web languages. On the back-end, the vast majority of websites that you visit are built with PHP. PHP is fast but even better for writing web applications, it is easy to learn and easy to use.
In this article, you will learn:
Print Statement:
There are two basic ways of getting output using PHP:
- echo
echo:
Since echo is a language construct that isn’t a function, you can use it with and without parentheses e.g. echo or echo(). If you want to pass more than one parameter to echo, however, parameters are not enclosed within parentheses.
Example:
1 2 3 | <?php echo "Hello World"; ?> |
Output:
1 | Hello World |
Example:
1 2 3 4 5 6 7 | <?php echo "Hello World"; echo "<br>"; echo "John","Bret Lee"; echo "<br>"; echo ("Sample String"); ?> |
Output:
1 2 3 | Hello World JohnBret Lee Sample String |
Example:
1 2 3 | <?php echo ("John", "Bret Lee"); ?> |
Output:
Above mentioned PHP code will show an error because you can not pass multiple values in a comma-separated in enclosed parentheses.
print:
Also, print is a language construct that isn’t a function, you can use it with and without parentheses e.g. print or print(). It accepts single data.
Example:
1 2 3 4 5 | <?php print "Hello World!"; print "<br>"; print ("troposal.com"); ?> |
Output:
1 2 | Hello World! troposal.com |
Example:
1 2 3 4 | <?php print "John","Bret Lee"; print ("John", "Bret Lee"); ?> |
Output:
Above mentioned PHP codes will show an error because you can not pass multiple values in a comma-separated in a print statement.
Difference between echo and print in PHP:
echo | |
---|---|
This statement can pass multiple strings separated by ‘,’. | Multiple strings can not pass. |
echo returns no values. | print always returns 1. |
echo is faster than print. | print is slower than echo. |
Code Blocks:
The PHP script begins with “<?php” and finishes with “?>” and each PHP command finishes with a semi-colon (;).
1 2 3 | <?php echo "troposal.com"; ?> |
PHP Code Commenting:
A comment is simply a text which the PHP engine ignores. Comments are aimed to make the code easier to read. It could help developers understand what you’ve been trying to do with the PHP.
There are two ways to comment in PHP:
- Single-line comments
- Multi-lines comments
Single-line comments:
To write a single-line comment, either start a line with two slashes (//) or a hash symbol (#).
Example:
1 2 3 4 5 | <?php //This is a single line comment #This is also a single line comment echo "troposal.com"; // Printing domain name ?> |
Output:
1 | troposal.com |
Multi-lines comments:
Multi-line comments start with a slash followed by an asterisk (/*) and finish with an asterisk followed by an asterisk (*/).
Example:
1 2 3 4 5 6 7 8 | <?php /* Author: Rakesh Comment Type: Multi-lines Language: PHP */ echo "Sample Code"; ?> |
Output:
1 | Sample Code |