004PHP_ Comments - Manual

download 004PHP_ Comments - Manual

of 8

Transcript of 004PHP_ Comments - Manual

  • 7/28/2019 004PHP_ Comments - Manual

    1/8

    [edit] Last updated: Fri, 20 Apr 2012

    Comments

    PHP supports 'C', 'C++' and Uni x shell-style (Perl style) comments. F or example:

    The "one-line" comment styles only comment to the end of the line or the current block of PHP code, whichever comes first. This means that HTML code

    after // ... ?> or # ... ?> WILL be printed: ?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that. If the asp_tags

    configuration directive is enabled, it behaves the same with // %> and # %>. However, the tag doesn't break out of PHP mode in a one-line

    comment.

    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 Comments

    team at researchbib dot com 13-Sep-2011 07:25

    PHP: Comments - Manual http://www.php.net/manual/en/language.basic-syntax.comments.php

    1 of 8 26/04/2012 11:03

  • 7/28/2019 004PHP_ Comments - Manual

    2/8

    1 of 8 26/04/2012 11:03

    when the comment string contains '?>', you should be careful.

    e.g. output code 1= code 2 is different with code 3

    1. with //

    2. with #

    3. with /* */

    philip-php at dago dot yourweb dot de 05-Mar-2011 10:29

    It's true, comments do not take up PROCESSING time, but they do take some PARSING time in case you are not using a compile cache of some

    kind.

    jballard at natoga dot com 15-Dec-2010 02:28

    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 ;)

  • 7/28/2019 004PHP_ Comments - Manual

    3/8

    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).

    benny at bennyborn dot de 08-Dec-2010 03:16

    This regex should do the job when trying to parse comments

    (\/\*(.*?)\*\/)|(^|\s+)\/\/(.*?)(\n|$)|( |\s+)#(.*?)(\n|$)

    Wolfsbay at ya dot ru 12-May-2010 02:10

    If you are using editor with code highlight, its much easier to notice error like /* */ */.

    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

    }

    ?>

    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

    PHP: Comments - Manual http://www.php.net/manual/en/language.basic-syntax.comments.php

    3 of 8 26/04/2012 11:03

  • 7/28/2019 004PHP_ Comments - Manual

    4/8

    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:

  • 7/28/2019 004PHP_ Comments - Manual

    5/8

    */

    //echo "";

    //echo "single-line comments end php mode and output your code.";

    ?>

    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: Comments - Manual http://www.php.net/manual/en/language.basic-syntax.comments.php

    5 of 8 26/04/2012 11:03PHP C M l h // h / l/ /l b i h

  • 7/28/2019 004PHP_ Comments - Manual

    6/8

  • 7/28/2019 004PHP_ Comments - Manual

    7/8

    }

    // */

    sort($morecode);

    ?>

    ..the block is suddenly commented out.

    This works because a /* .. */ overrides //. You can even "flip" two blocks, like this:

    vs

    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 ;-)

  • 7/28/2019 004PHP_ Comments - Manual

    8/8

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

    */

    ?>

    Copyright 2001-2012 The PHP GroupAll rights reserved.

    PHP: Comments - Manual http://www.php.net/manual/en/language.basic-syntax.comments.php