PHP Comments and basics
PHP

PHP Comments and basics

Editorial Team
Editorial Team

Follow us on Discord or Telegram to get access to free tutorials and updates.

PHP script can be embedded anywhere in the document. The PHP scripts are enclosed within <?php and ?>. A PHP script file will be stored with a .php extension.

Each statements ends with a semicolon (;).

Example

<html>
<head>
    <title>My PHP Example</title>
</head>
<body>
    <?php echo "Welcome to my first PHP page";?>
</body>
</html>

The above code when executed will produce the following output.

Welcome to my first PHP page

PHP Comments

In PHP, comments are used by a programmer to:

  • Hide a block of code.
  • Describe your code.
  • Write something for others to understand.

All comments will be skipped without executing them. PHP supports single-line and multi-line comments.

A single line comment can be specified in two ways

  • Using a # symbol ¬†at the beginning of a line.
  • Using // at the beginning of a line.

Example

<!DOCTYPE html>
<html>
<body>
<?php
// This is an example of
# a single-line comment
//echo "This line will not be executed";
?>
</body>
</html>

This code will not produce any output because all the lines (PHP scripts) are commented.

In PHP, you can comment multiple lines by placing the content within /* and */.

Example

<!DOCTYPE html>
<html>
<body>
<?php
/* This is an example
of multi-line comment in PHP*/
echo "This line will /*not*/ be executed";
?>
</body>
</html>

The above program when executed will produce the following output.

This line will be executed