print-statement-code-blocks-comments-php

Print Statement, Code Blocks and Commenting in PHP

Posted by

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:

There are two basic ways of getting output using PHP:

  1. echo
  2. print

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:

Output:

Example:

Output:

Example:

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:

Output:

Example:

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 print
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 (;).

PHP Code Commenting:

php-commentsA 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:

  1. Single-line comments
  2. Multi-lines comments

Single-line comments:

To write a single-line comment, either start a line with two slashes (//) or a hash symbol (#).
Example:

Output:

Multi-lines comments:

Multi-line comments start with a slash followed by an asterisk (/*) and finish with an asterisk followed by an asterisk (*/).
Example:

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *