Use comments correctly in PHP
// This is a single-line comment
# This is another single-line comment
/*
* And this is a
* multi-line
* comment
*/
/*
And this is an alternate version
multi-line
comment
*/
Proper commenting on source code is an important part of a developer's job, not only in PHP but also in other languages. A good comment helps you understand how the code works. In addition, it can also benefit individual development environments and provide better code hints during development.
Fortunately, the syntax is similar to other languages. You can write single-line and multi-line comments.
Single line comments
For single-line comments, you need to use either the //
or #
characters to mark the comment.
// This is a single-line comment demonstration
$myVar = 1; // This is a single-line comment to clarify the value of $myVar
$myName = 'Max'; # This is another single line comment to clarify the value of $myName
# And the last comment
Multi-line comments
If you want to write a longer, multi-line comment, you can use the single-line comment syntax for each line, but it's much easier and more readable to use multi-line comments.
/*
This is a multi-line comment example
This comments block is used to explain the following function
*/
function myFunction() {
echo 'Hello world';
}
Nested comments
A final special case is that of nested comments. In this case, the comment is placed in the middle of the code line.
for ($i = 0; $i < 10; /* This is a nested comment */ $i++) {
echo $i . '<br/>';
}
- Remove whitespace from the beginning and end of a string in PHP
- Locate a substring within a string in PHP
- Process CSV line in PHP
- Use comments correctly in PHP
- Extract a substring from a string in PHP
- Use PHP ternary operator
- Split a string into words in PHP
- Check if JSON key exists in PHP
- Access object property with hyphen in PHP
- Replace spaces with dashes in PHP
- Replace only the first occurrence of a string in PHP
- Replace last occurrence of a string in a string in PHP
- Extract data from JSON in PHP
- Add minutes to date time in PHP
- Add days to date in PHP
- Get same day in the next week in PHP
- Get time difference in minutes in PHP
- Convert date to timestamp in PHP
- Convert timestamp to DateTime in PHP
- Get the last day of a month from date in PHP
- Remove all attributes from HTML tags in PHP
- Remove HTML tags from a string except p, b, i and br in PHP
- Remove HTML tags from a string in PHP
- Get the number of days between two dates in PHP
- Get yesterday's date in PHP
- Get tomorrow's date in PHP
- Get current time in PHP
- Get actual date in PHP