PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

변수형> <기본 문법
Last updated: Sun, 25 Nov 2007

view this page in

주석

PHP는 'C', 'C++', Unix 셀 스타일(shell-style)의 주석(comment)형태를 지원한다. 예를 들면:

<?php
    
echo "This is a test"// 한줄짜리 c++ 스타일 주석
    /* 여러줄의 주석
       이줄까지 주석처리 된다 */
    
echo "This is yet another test";
    echo 
"One Final Test"# 셀스타일의 한줄짜리 주석
?>

"한줄짜리" 주석은 그 줄의 끝이나, 처음에 무엇이 오든 해당 PHP코드 블록에 주석을 단다.

<h1>This is an <?php # echo "simple";?> example.</h1>
<p>The header above will say 'This is an example'.

'C' 스타일 주석을 중복 내포하지 않도록 주의해야 한다. 이와 같은 실수는 거대한 블록을 주석처리할 때 일어날수 있다.

<?php
 
/* 
    echo "This is a test"; /* 이 주석은 문제를 일으킬것이다. */
 
*/
?>

한줄짜리 주석은 그 줄의 끝이나, 처음에 무엇이 오든 해당 PHP코드 블록에 주석을 단다. 이 말의 의미는 // ?>이후에 HTML코드가 출력될것이 라는 것이다: ?> 은 PHP모드를 벗어나서 HTML모드로 돌아오게 하고, //은 이 동작에 영향을 미치지 못한다.



변수형> <기본 문법
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
주석
theblazingangel at aol dot com
28-Aug-2007 03:55
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.
fun at nybbles dot com
13-Jul-2006 10:28
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");
         }
?>
mst_NO_SPAM_TO_ME at mstsoft dot com
05-Jun-2006 05:38
This "comment ends on line break or end of PHP Block" thing can be confusing. I discovered this by accident when working with XML Output from PHP...

<?PHP

header
("Content-type: text/xml");

/*
echo "<?xml version=\"1.0\"?>";
echo "<page>multi-line comments work as expected.</page>";
*/

//echo "<?xml version=\"1.0\"
?>";
//echo "<page>single-line comments end php mode and output your code.</page>";

?>

I would expect the comment to work, but there is no parsing in comments so the String suddenly becomes a PHP  end-block tag, which is correct reading this documentation.

cheers,
martin
PS: You even see the behavior in the Syntax highlighting :-)
J Lee
25-May-2006 11:39
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 */
21-Jan-2006 01:46
M Spreij wrote, 08-May-2005 08:15...
A nice way to toggle the commenting of blocks of code can be done by mixing the two comment styles:
...
This works because a /* .. */ overrides //.

The final sentence should be the other way round, i.e.

This works because a // overrides /* .. */.
(If it didn't the /* .. */ would comment out the code regardless of whether an additional '/' is prefixed to the first line).
samuli dot karevaara at lamk dot fi
11-Nov-2005 08:30
If you want to comment out large sections of code (temporarily, usually and hopefully), consider using
<?php
if (0) {
     print(
"This code is 'commented' out");
}
?>
instead of /* comment block */. Otherwise, as noted here, you will have parse errors if the block that you commented out contains */ somewhere, like in regexp or in another comment.
hcderaad at wanadoo dot nl
29-Jun-2005 01:51
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.
M Spreij
08-May-2005 12:15
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;
}
// */
?>
Steve
15-Dec-2004 04:41
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.*/
');

*/

?>

변수형> <기본 문법
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites