PHP TUTORIALPHP TutorialPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP CommentsPHP VariablesPHP Variable ScopePHP ConstantsPHP StringsPHP OutputPHP Data TypesPHP Type CastingPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP String FunctionsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP AdvancedPHP Include and RequirePHP HTTP & HTTPSPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms FinishingPHP OOPPHP OOP IntroductionPHP OOP ClassesPHP OOP PropertiesPHP OOP ObjectsPHP OOP MethodsPHP OOP $this KeywordPHP OOP Constructors and DestructorsPHP OOP VisibilityPHP OOP InheritancePHP OOP Abstract Classes and MethodsPHP OOP InterfacesPHP OOP TraitsPHP OOP ConstantsPHP OOP StaticPHP OOP NamespacesPHP OOP Autoloading

PHP Comments

Good programmers write perfect code with plenty of comments.

  • Comments are used to understand the code while reading.
  • They can also be used to leave a part from the code. (As we did in the last example of the last chapter)
  • Comments are really important when trying to understand other's or your own code after years.
  • Comments can explain what the code does.
  • Comments does not need to follow language syntax rules.

PHP parser ignores all the comments.

There are two ways of commenting

  • Single-Line Commenting - // your comment or # your comment
  • Multi-Line Commenting - /* your comment */

PHP Comments Example


// this is a single-line comment

# this is a single-line comment too

	This is a multi-line comment
	This line is a comment

// to remove a part of code
echo 5 /* + 2 */  + 5;

Run Example ››

Block Comments

Developers use a special way of commenting to make it easy to understand when you read it again. Note that this is not a rule, but an effective way of increasing the readability of your comments. This is called block comments. Here's the common way of writing block comments.

PHP Block Comments

* This is a block comment
* The first line starts with the normal /* and one more *
* All the next lines starts with *
* Finally, it ends with * and / in the last line

Run Example ››

Block comments are used to provide verbose information about variables, functions, and more. We will learn how to use them in a real world script in the next chapters.

Visit PHP Help group to get some help from experts.
Profile Picture
Supun Kavinda
I'm the Founder of Hyvor, Web Developer, Physics Lover, Flutist, and a Table Tennis Player.
My Websites