PHP Comments

0
6KB

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

 
 

 

 

Suche
Kategorien
Mehr lesen
Andere
PHP Constants
Constants are like variables, except that once they are defined they cannot be changed or...
Von PHP Tutorial 2024-05-17 07:39:53 0 8KB
Andere
PHP Update Array Items
Update Array Item To update an existing array item, you can refer to the index number for...
Von PHP Tutorial 2024-05-17 08:02:49 0 7KB
Andere
PHP Add Array Items
Add Array Item To add items to an existing array, you can use the bracket [] syntax....
Von PHP Tutorial 2024-05-17 08:06:05 0 7KB
Andere
PHP Arrays
An array stores multiple values in one single variable: ExampleGet your own PHP Server $cars...
Von PHP Tutorial 2024-05-17 07:53:31 0 6KB
Andere
PHP while Loop
The while loop - Loops through a block of code as long as the specified condition is...
Von PHP Tutorial 2024-05-17 07:48:58 0 4KB
Sociallez https://sociallez.com