ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ

PHP ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ Π² стилС языков программирования C, C++ ΠΈ Π² стилС ΠΊΠΎΠΌΠ°Π½Π΄Π½ΠΎΠΉ ΠΎΠ±ΠΎΠ»ΠΎΡ‡ΠΊΠΈ Unix-систСм, которая унаслСдовала ΡΡ‚ΠΈΠ»ΡŒ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅Π² ΠΈΠ· языка Perl. НапримСр:

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅Π²

<?php

echo "Π­Ρ‚ΠΎ тСст"; // Π­Ρ‚ΠΎ однострочный ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π² стилС языка C++
/* Π­Ρ‚ΠΎ многострочный ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ
Π΅Ρ‰Ρ‘ ΠΎΠ΄Π½Π° строка коммСнтария */
echo "Π­Ρ‚ΠΎ Π΅Ρ‰Ρ‘ ΠΎΠ΄ΠΈΠ½ тСст\n";
echo
"Π•Ρ‰Ρ‘ ΠΎΠ΄ΠΈΠ½ послСдний тСст\n"; # Π­Ρ‚ΠΎ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π² стилС Unix-ΠΎΠ±ΠΎΠ»ΠΎΡ‡ΠΊΠΈ

?>

ΠžΠ΄Π½ΠΎΡΡ‚Ρ€ΠΎΡ‡Π½Ρ‹Π΅ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ Ρ€Π°ΡΠΏΡ€ΠΎΡΡ‚Ρ€Π°Π½ΡΡŽΡ‚ΡΡ Ρ‚ΠΎΠ»ΡŒΠΊΠΎ Π΄ΠΎ ΠΊΠΎΠ½Ρ†Π° строки PHP-ΠΊΠΎΠ΄Π° ΠΈΠ»ΠΈ Π½Π° Ρ‚Π΅ΠΊΡƒΡ‰ΠΈΠΉ Π±Π»ΠΎΠΊ PHP-ΠΊΠΎΠ΄Π° Π² HTML-строкС, Π² зависимости ΠΎΡ‚ Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ ΠΈΠ΄Ρ‘Ρ‚ ΠΏΠ΅Ρ€Π²Ρ‹ΠΌ. ΠŸΠΎΡΡ‚ΠΎΠΌΡƒ HTML-ΠΊΠΎΠ΄ послС Π½Π°Ρ‡Π°Π»Π° однострочного коммСнтария // ... ?> ΠΈΠ»ΠΈ # ... ?> выводится: Ρ‚Π΅Π³ ?> ΠΏΡ€Π΅Ρ€Ρ‹Π²Π°Π΅Ρ‚ Ρ€Π΅ΠΆΠΈΠΌ Ρ€Π°Π·Π±ΠΎΡ€Π° PHP-ΠΊΠΎΠ΄Π° ΠΈ Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ Ρ€Π΅ΠΆΠΈΠΌ HTML-Ρ€Π°Π·ΠΌΠ΅Ρ‚ΠΊΠΈ, ΠΈ символы // ΠΈΠ»ΠΈ # Π½Π΅ Π²Π»ΠΈΡΡŽΡ‚ Π½Π° это.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #2 ΠŸΡ€ΠΈΠΌΠ΅Ρ€ однострочных ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠ΅Π²

<h1>Π­Ρ‚ΠΎ <?php # echo "простой";?> ΠΏΡ€ΠΈΠΌΠ΅Ρ€</h1>
<p>Π—Π°Π³ΠΎΠ»ΠΎΠ²ΠΎΠΊ Π²Π²Π΅Ρ€Ρ…Ρƒ Π²Ρ‹Π²Π΅Π΄Π΅Ρ‚ 'Π­Ρ‚ΠΎ ΠΏΡ€ΠΈΠΌΠ΅Ρ€'.</p>

ΠšΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ Π² стилС языка C Π·Π°ΠΊΠ°Π½Ρ‡ΠΈΠ²Π°ΡŽΡ‚ΡΡ, ΠΊΠΎΠ³Π΄Π° обнаруТиваСтся пСрвая ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°Ρ‚Π΅Π»ΡŒΠ½ΠΎΡΡ‚ΡŒ */. ΠŸΡ€ΠΎΠ³Ρ€Π°ΠΌΠΌΠΈΡΡ‚Ρ‹ слСдят Π·Π° Ρ‚Π΅ΠΌ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ Π² стилС языка C Π½Π΅ оказались Π²Π»ΠΎΠΆΠ΅Π½Π½Ρ‹ΠΌΠΈ. ΠžΡˆΠΈΠ±ΠΊΡƒ с Π²Π»ΠΎΠΆΠ΅Π½Π½Ρ‹ΠΌΠΈ C-коммСнтариями часто Π΄ΠΎΠΏΡƒΡΠΊΠ°ΡŽΡ‚, ΠΊΠΎΠ³Π΄Π° ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚ΠΈΡ€ΡƒΡŽΡ‚ большой Π±Π»ΠΎΠΊ ΠΊΠΎΠ΄Π°.

<?php

/*
echo "Π­Ρ‚ΠΎ тСст"; /* Π­Ρ‚ΠΎΡ‚ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π²Ρ‹Π·ΠΎΠ²Π΅Ρ‚ ΠΎΡˆΠΈΠ±ΠΊΡƒ */
*/

?>
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 11 notes

up
477
J. Prettyman ΒΆ
12 years ago
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...

<?php

//======================================================================
// CATEGORY LARGE FONT
//======================================================================

//-----------------------------------------------------
// Sub-Category Smaller Font
//-----------------------------------------------------

/* 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.
?>
up
327
M Spreij ΒΆ
21 years ago
A nice way to toggle the commenting of blocks of code can be done by mixing the two comment styles:
<?php
//*
if ($foo) {
  echo $bar;
}
// */
sort($morecode);
?>

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

<?php
/*
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:
<?php
//*
if ($foo) {
  echo $bar;
}
/*/
if ($bar) {
  echo $foo;
}
// */
?>
vs
<?php
/*
if ($foo) {
  echo $bar;
}
/*/
if ($bar) {
  echo $foo;
}
// */
?>
up
49
aetonsi ΒΆ
3 years ago
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 <8:
<?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.
up
158
magnesium dot oxide dot play+php at gmail dot com ΒΆ
12 years ago
It is worth mentioning that, HTML comments have no meaning in PHP parser. So,

<!-- comment
<?php echo some_function(); ?>
-->

WILL execute some_function() and echo result inside HTML comment.
up
62
hcderaad at wanadoo dot nl ΒΆ
20 years ago
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:
<?php
/**
* The second * here opens the DocBook commentblock, which could later on<br>
* in your development cycle save you a lot of time by preventing you having to rewrite<br>
* major documentation parts to generate some usable form of documentation.
*/
?>
Some basic html-like formatting is supported with this (ie <br> tags) to create something of a layout.
up
54
J Lee ΒΆ
20 years ago
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 */
up
45
Steve ΒΆ
21 years ago
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 ;-)

<?php 

/*

 $f->setPattern('/^\d.*/');

*/

?>
up
29
theblazingangel at aol dot com ΒΆ
18 years ago
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 <?php echo 'something'; //comment ?>

<?php
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.
up
20
jballard at natoga dot com ΒΆ
15 years ago
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 ;)

<?php

// Control
echo microtime(), "<br />"; // 0.25163600 1292450508
echo microtime(), "<br />"; // 0.25186000 1292450508

// Test
echo microtime(), "<br />"; // 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(), "<br />"; // 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).
up
9
Wolfsbay at ya dot ru ΒΆ
16 years ago
If you are using editor with code highlight, it’s much easier to notice error like /* */ */.
up
12
fun at nybbles dot com ΒΆ
19 years ago
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:

<?php $ver3 = TRUE;  
       $debug2 = FALSE; 
?> 

and then deeper inside the file: 

<?php if ($ver3) {
           print("This code is included since we are testing version 3");
         }
?>

<?php if ($debug2) {
           print("This code is 'commented' out");
         }
?>