IT

PHP에서 주석을 달기 위해 해시 기호 (#)를 사용할 수 있습니까?

lottoking 2020. 6. 27. 10:39
반응형

PHP에서 주석을 달기 위해 해시 기호 (#)를 사용할 수 있습니까?


필자는 #주석 처리를 위해 해시 ( )를 사용하는 PHP 파일을 본 적이 없다 . 그러나 오늘 나는 실제로 할 수 있음을 깨달았습니다! 나는 모두가 //대신 대신 사용하는 이유가 있다고 가정하고 있습니다 . 그래서 여기 있습니다.

개인적 선호를 제외하고 의견 //보다는 사용해야 할 이유가 #있습니까?


질문에 대한 답변 PHP에서 한 줄 주석에 "#"과 "//"를 사용 하는 것에 어떤 차이 가 있습니까? 없는 .

다른 점이 없다. PHP 소스 코드의 구문 분석 부분을 살펴보면 "#"과 "//"는 동일한 코드로 처리 되므로 동일한 동작을 갖습니다.


PHP 문서는 다양한 주석 가능성을 설명합니다. 참조 http://www.php.net/manual/en/language.basic-syntax.comments.php를

그러나 "//"와 "#"의 차이점에 대해서는 언급하지 않습니다. 따라서 기술적 차이가 없어야합니다. PHP는 C 구문을 사용하므로 대부분의 프로그래머가 C 스타일 주석 '//'을 사용하는 이유라고 생각합니다.


<?php
    echo 'This is a test'; // This is a one-line C++ style comment
    /* This is a multi-line comment.
       Yet another line of comment. */
    echo 'This is yet another test.';
    echo 'One Final Test'; # This is a one-line shell-style comment
?>

RTM


개인적 선호를 제외하고 댓글에 # 대신 //를 사용해야하는 이유가 있습니까?

나는 그것이 단지 개인적인 취향 일 뿐이라고 생각합니다. //사이에는 차이가 없습니다 #. 개인적 #으로 한 줄 주석, //코드 주석 및 /** */블록 주석에 사용합니다.

<?php
    # This is a one-line comment
    echo 'This is a test';

    // echo 'This is yet another test'; // commenting code

    /** 
     * This is a block comment
     * with multi-lines 
     */
    echo 'One final test';
?>

하나는 생각 것을 #주석의 형태는 주로 친숙한 "오두막"(#!) 표기법을 사용하여 쉘 스크립트를 확인하기위한 것입니다. 다음 스크립트에서 PHP는 주석이기 때문에 첫 줄을 무시해야합니다. 예:

#!/usr/bin/php
<?php

echo "Hello PHP\n";

실행 파일에 저장하면 다음과 같은 터미널에서 실행할 수 있습니다

./hello

출력은

Hello PHP

그러나 다음과 같은 반례가 보여 주듯이이 추론은 잘못되었습니다.

#!/usr/bin/php
#A
<?php

#B
echo "Hello PHP\n";

첫 번째 줄 (shebang 줄)은 통역사가 특별히 무시합니다. PHP 태그 앞에있는 주석 행은 PHP 태그 안에 없기 때문에 표준 출력으로 에코됩니다. PHP 태그를 시작한 후의 주석은 PHP 코드로 해석되지만 주석이므로 무시됩니다.

수정 된 버전의 출력은

#A
Hello PHP

If you establish some rule sets in your team / project... the 2 types of comments can be used to outline the purpose of the commented code.

For example I like to use # to mute / disable config settings, sub functions and in general a piece of code that is useful or important, but is just currently disabled.


There's no official PSR for that.

However, in all PSR example code, they use // for inline comments.

There's an PSR-2 extension proposal that aims to standardize it, but it's not official: https://github.com/php-fig-rectified/fig-rectified-standards/blob/master/PSR-2-R-coding-style-guide-additions.md#commenting-code

// is more commonly used in the PHP culture, but it's fine to use # too. I personally like it, for being shorter and saving bytes. It's personal taste and biased, there's no right answer for it, until, of course, it becomes a standard, which is something we should try to follow as much as possible.


Yes, however there are cross platform differences.

I use # all the time for commenting in PHP, but I have noticed an adoption difference.

On windows keyboard the # key is easy to use. On mac keyboard # key mostly isn't present.

So for mac users, [Alt] + [3] or [⌥] + [3] is more difficult to type than //, so // has become a cross platform way of displaying code with comments.

This is my observation.


From https://php.net/manual/en/migration53.deprecated.php

"Deprecated features in PHP 5.3.x ...Comments starting with '#' are now deprecated in .INI files."

There you have it. Hash '#' appears to remain as a comment option by default by not being deprecated. I plan to use it to distinguish various layers of nested if/else statements and mark their closing brackets, or use to distinguish code comments from commented out code as others have suggested in related posts. (Note: Link was valid/working as of 4/23/19, although who knows if it'll still be working when you're reading this.)


Is there any reason, aside from personal preference, to use // rather than # for comments?

I came here for the answer myself, and its good to know there is NO code difference.

However, preference-wise one could argue that you'd prefer the 'shell->perl->php' comment consistency vs the 'c->php' way.

Since I did approach php as a poor man's webby perl, I was using #.. and then I saw someone else's code and came straight to SO. ;)


Comments with "#" are deprecated with PHP 5.3. So always use // or /.../

참고URL : https://stackoverflow.com/questions/9093609/can-i-use-a-hash-sign-for-commenting-in-php

반응형