Write comments in php

Using Comments in PHP

Comments in PHP are similar to comments that are used in HTML. The PHP comment syntax always begins with a special character sequence and all text that appears between the start of the comment and the end will be ignored.

In HTML a comment’s main purpose is to serve as a note to you, the web developer or to others who may view your website’s source code. However, PHP’s comments are different in that they will not be displayed to your visitors. The only way to view PHP comments is to open the PHP file for editing. This makes PHP comments only useful to PHP programmers.

Читайте также:  Opencart html module extension

In case you forgot what an HTML comment looked like, see our example below.

HTML Code:

PHP Comment Syntax: Single Line Comment

While there is only one type of comment in HTML, PHP has two types. The first type we will discuss is the single line comment. The single line comment tells the interpreter to ignore everything that occurs on that line to the right of the comment. To do a single line comment type «//» or «#» and all text to the right will be ignored by PHP interpreter.

PHP Code:

Psst. You can't see my PHP comments!"; // echo "nothing"; // echo "My name is Humperdinkle!"; # echo "I don't do anything either"; ?>

Display:

Notice that a couple of our echo statements were not evaluated because we commented them out with the single line comment. This type of line commenting is often used for quick notes about complex and confusing code or to temporarily remove a line of PHP code.

PHP Comment Syntax: Multiple Line Comment

Similiar to the HTML comment, the multi-line PHP comment can be used to comment out large blocks of code or writing multiple line comments. The multiple line PHP comment begins with » /* » and ends with » */ «.

PHP Code:

Display:

Good Commenting Practices

One of the best commenting practices that I can recommend to new PHP programmers is. USE THEM!! So many people write complex PHP code and are either too lazy to write good comments or believe the commenting is not needed. However, do you really believe that you will remember exactly what you were thinking when looking at this code a year or more down the road?

Читайте также:  Google chrome java плагин

Let the comments permeate your code and you will be a happier PHPer in the future. Use single line comments for quick notes about a tricky part in your code and use multiple line comments when you need to describe something in greater depth than a simple note.

Download Tizag.com’s PHP Book

If you would rather download the PDF of this tutorial, check out our PHP eBook from the Tizag.com store. Print it out, write all over it, post your favorite lessons all over your wall!

Found Something Wrong in this Lesson?

Report a Bug or Comment on This Lesson — Your input is what keeps Tizag improving with time!

Источник

This is an example

The header above will say ‘This is an example’.

‘C’ style comments end at the first */ encountered. Make sure you don’t nest ‘C’ style comments. It is easy to make this mistake if you are trying to comment out a large block of code.

User Contributed Notes 12 notes

Notes can come in all sorts of shapes and sizes. They vary, and their uses are completely up to the person writing the code. However, I try to keep things consistent in my code that way it’s easy for the next person to read. So something like this might help.

/* Title Here Notice the First Letters are Capitalized */

# Option 1
# Option 2
# Option 3

/*
* This is a detailed explanation
* of something that should require
* several paragraphs of information.
*/

// This is a single line quote.
?>

A nice way to toggle the commenting of blocks of code can be done by mixing the two comment styles:
//*
if ( $foo ) echo $bar ;
>
// */
sort ( $morecode );
?>

Now by taking out one / on the first line..

/*
if ($foo) echo $bar;
>
// */
sort ( $morecode );
?>
..the block is suddenly commented out.
This works because a /* .. */ overrides //. You can even «flip» two blocks, like this:
//*
if ( $foo ) echo $bar ;
>
/*/
if ($bar) echo $foo;
>
// */
?>
vs
/*
if ($foo) echo $bar;
>
/*/
if ( $bar ) echo $foo ;
>
// */
?>

As of php 8, single line comments starting exactly with «#[» have a special meaning: they are treated as «attributes», and they must respect the expected syntax. See: https://www.php.net/manual/en/language.attributes.php

So the following code throws an error in php 8+, while it is perfectly valid in php #[~~my super cool comment~~~]
?>

To be safe, just always use «//» comments instead of «#». Maybe in the future there will be other special meanings for the «#» comments, who knows.

It is worth mentioning that, HTML comments have no meaning in PHP parser. So,

WILL execute some_function() and echo result inside HTML comment.

Comments in PHP can be used for several purposes, a very interesting one being that you can generate API documentation directly from them by using PHPDocumentor (http://www.phpdoc.org/).

Therefor one has to use a JavaDoc-like comment syntax (conforms to the DocBook DTD), example:
/**
* The second * here opens the DocBook commentblock, which could later on

* in your development cycle save you a lot of time by preventing you having to rewrite

* major documentation parts to generate some usable form of documentation.
*/
?>
Some basic html-like formatting is supported with this (ie
tags) to create something of a layout.

MSpreij (8-May-2005) says /* .. */ overrides //
Anonymous (26-Jan-2006) says // overrides /* .. */

Actually, both are correct. Once a comment is opened, *everything* is ignored until the end of the comment (or the end of the php block) is reached.

Thus, if a comment is opened with:
// then /* and */ are «overridden» until after end-of-line
/* then // is «overridden» until after */

Be careful when commenting out regular expressions.

E.g. the following causes a parser error.

I do prefer using # as regexp delimiter anyway so it won’t hurt me 😉

Comments do NOT take up processing power.

So, for all the people who argue that comments are undesired because they take up processing power now have no reason to comment 😉

// Control
echo microtime (), «
» ; // 0.25163600 1292450508
echo microtime (), «
» ; // 0.25186000 1292450508

// Test
echo microtime (), «
» ; // 0.25189700 1292450508
# TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST TEST
# .. Above comment repeated 18809 times ..
echo microtime (), «
» ; // 0.25192100 1292450508

?>

They take up about the same amount of time (about meaning on a repeated testing, sometimes the difference between the control and the test was negative and sometimes positive).

it’s perhaps not obvious to some, but the following code will cause a parse error! the ?> in //?> is not treated as commented text, this is a result of having to handle code on one line such as

if( 1 == 1 )
// ?>
>
?>

i discovered this «anomally» when i commented out a line of code containing a regex which itself contained ?>, with the // style comment.
e.g. //preg_match(‘/^(?>c|b)at$/’, ‘cat’, $matches);
will cause an error while commented! using /**/ style comments provides a solution. i don’t know about # style comments, i don’t ever personally use them.

a trick I have used in all languages to temporarily block out large sections (usually for test/debug/new-feature purposes), is to set (or define) a var at the top, and use that to conditionally comment the blocks; an added benefit over if(0) (samuli’s comment from nov’05) is that u can have several versions or tests running at once, and u dont require cleanup later if u want to keep the blocks in: just reset the var.

personally, I use this more to conditionally include code for new feature testing, than to block it out. but hey, to each their own 🙂

this is also the only safe way I know of to easily nest comments in any language, and great for multi-file use, if the conditional variables are placed in an include 🙂

for example, placed at top of file:

and then deeper inside the file:

print( «This code is included since we are testing version 3» );
>
?>

print( «This code is ‘commented’ out» );
>
?>

In php there are 3 types of comments
1.single line c++ style comment(//)
2.single line Unix shell stype comment(#)
3.multi line c style comment(/*/)

single or multi line comment comes to the end of the line or come first to the current block of php code.

HTML code will be printed after //. > or #. >
closing tag breaks the php mode and return to html mode.

different comments in different tags:
===================================

Standard tag: single line c++ style comment

The header above will break php mode and return html mode and show ‘Standard tag:single line c++ style comment’

Standard tag: single line unix shell style comment

The header above will break php mode and return html mode and show ‘Standard tag:single line unix shell style comment’

Standard tag: multi line c style comment

The header above will break php mode and return html mode and show ‘Standard tag:multi line c style comment’

short echo tag: single line c++ style comment

The header above will break php mode show a unexpected syntex error’

short echo tag: single line c++ style comment

The header above will break php mode show a unexpected syntex error’

short echo tag: multiple line c style comment

The header above will break php mode show a unexpected syntex error’

Short tag: single line c++ style comment

The header above will break php mode and return html mode and show ‘Short tag:single line c++ style comment’

Short tag: single line unix shell style comment

The header above will break php mode and return html mode and show ‘Short tag:single line unix shell style comment’

Short tag: multi line c style comment

The header above will break php mode and return html mode and show ‘Short tag:multi line c style comment’

Script tag: single line c++ style comment

The header above will break php mode and return html mode and show ‘Script tag:single line c++ style comment’

Script tag: multi line c style comment

The header above will break php mode and return html mode and show ‘Script tag:multi line c style comment’

Script tag: single line unix shell style comment

The header above will not break php mode

Источник

PHP Comments

Summary: in this tutorial, you’ll learn how to use PHP comments to document your code.

Comments are important parts of the code. Comments provide useful information that will help you and other developers understand the meaning of the code more quickly later.

PHP supports two types of comments:

One-line comments

The one-line comment is placed at the end of the line or at the current block.

A one-line comment starts with the pound ( # ) or double forward-slash ( // ). The rest of the text after the (//) is ignored by the PHP interpreter.

The following example uses the // for a one-line comment:

 $rate = 100; $hours = 173; $payout = $hours * $rate; // payout calculationCode language: HTML, XML (xml)

And the following example uses the # for a one-line comment:

 $title = 'PHP comment'; # set default titleCode language: HTML, XML (xml)

Multi-line comments

A Multi-line comment start with /* and end with */ . For example:

 /* This is an example of a multi-line comment, which can span multiple lines. */Code language: HTML, XML (xml)

In practice, you use the multi-line comment when you need to span comments multiple lines.

Writing meaningful comments

To document your code effectively, you use the following guidelines:

1) Making the code speak for itself without using comments by naming meaningful identifiers. For example, you can use the following:

$is_completed = true;Code language: PHP (php)

Instead of using a cryptic name with a comment:

$ic = true; // is completedCode language: PHP (php)

The code itself can be good comments.

2) Don’t write a comment to explain what code does, instead, explain why it does so. For example:

// complete the task $is_completed = trueCode language: PHP (php)

3) When writing a comment, make it as concise as possible.

Summary

  • Comments are important parts of the code because they explain why code does what it is supposed to do.
  • PHP supports both one-line and multi-line comments.
  • A one-line comment starts with the # or // .
  • A multi-line comment starts with /* and end with */ .

Источник

Оцените статью