PHP Comments

0
2K

Comments in PHP

A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is to be read by someone who is looking at the code.

Comments can be used to:

  • Let others understand your code
  • Remind yourself of what you did - Most programmers have experienced coming back to their own work a year or two later and having to re-figure out what they did. Comments can remind you of what you were thinking when you wrote the code
  • Leave out some parts of your code

PHP supports several ways of commenting:

ExampleGet your own PHP Server

Syntax for comments in PHP code:

// This is a single-line comment

# This is also a single-line comment

/* This is a
multi-line comment */
Try it Yourself »

Single Line Comments

Single line comments start with //.

Any text between // and the end of the line will be ignored (will not be executed).

You can also use # for single line comments, but in this tutorial we will use //.

The following examples uses a single-line comment as an explanation:

Example

A comment before the code:

// Outputs a welcome message:
echo "Welcome Home!";
Try it Yourself »

Example

A comment at the end of a line:

echo "Welcome Home!"; // Outputs a welcome message
Try it Yourself »

Comments to Ignore Code

We can use comments to prevent code lines from being executed:

Example

Do not display a welcome message:

// echo "Welcome Home!";
Try it Yourself »

PHP Exercises

Test Yourself With Exercises

Exercise:

Insert the PHP comment syntax to make sure the following code line is not executed:

 print("Hello World");

Start the Exercise

 
 

 

 

Search
Categories
Read More
Other
PHP - Slicing Strings
Slicing You can return a range of characters by using the substr() function. Specify...
By PHP Tutorial 2024-05-17 07:31:23 1 3K
Other
الثورة البلوكتشين وتحولاتها في الصناعات المختلفة
مقدمة: شهدت تقنية البلوكتشين ثورة هائلة في السنوات الأخيرة، حيث أصبحت لديها القدرة على تغيير...
By MOHAMED ATTALLAH 2024-05-14 14:03:03 0 3K
Other
PHP Indexed Arrays
PHP Indexed Arrays In indexed arrays each item has an index number. By default, the first item...
By PHP Tutorial 2024-05-17 08:00:38 0 2K
Other
PHP Strings
A string is a sequence of characters, like "Hello world!". Strings Strings in PHP are...
By PHP Tutorial 2024-05-17 07:28:17 0 3K
Other
PHP - Modify Strings
PHP has a set of built-in functions that you can use to modify strings. Upper Case...
By PHP Tutorial 2024-05-17 07:29:19 0 3K