← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 6 of 13

PHP Comments

Comments आपके code पढ़ने वालों के लिए छोड़े गए छोटे notes हैं, किसी recipe पर sticky notes जैसे। Computer इन्हें पूरी तरह skip कर देता है, इसलिए ये program क्या करता है यह कभी नहीं बदलते।
Syntax
php
<?php
    // single-line comment
    # another single-line comment
    /* multi-line
       comment */
?>

Single-Line Comments

एक // या # comment PHP को बताता है कि उस point से line के अंत तक सब कुछ ignore कर दे।

ये छोटे, single-line notes के लिए सबसे अच्छे हैं — किसी workaround को flag करना, या खुद को याद दिलाना कि कोई value hardcode क्यों की गई है — बड़े blocks of logic explain करने के लिए नहीं।

उदाहरण: Single-Line Comments

php
<?php
// This is a single-line comment
# So is this
echo "Comments above are ignored";
?>

Multi-Line Comments

एक /* ... */ comment कई lines तक फैल सकता है, जो इसे लंबी explanations के लिए सही tool बनाता है: कोई function क्या करता है describe करना, expected parameters document करना, या अपनी सोच भूलने से पहले किसी tricky algorithm को temporarily annotate करना।

उदाहरण: Multi-Line Comments

php
<?php
/*
 * This function doubles a number.
 * Spans multiple lines to explain the logic.
 */
echo 5 * 2;
?>

Code Document करना

सबसे उपयोगी comments यह explain करते हैं कि कोई piece of code non-obvious form में *क्यों* exist करता है — किसी browser quirk के लिए एक workaround, एक business rule जो सिर्फ code से visible नहीं है — बजाय यह दोहराने के कि code *क्या* करता है, जो code खुद पहले से दिखाता है।

उदाहरण: Documenting Code

php
<?php
// Workaround: legacy API returns price in cents, so we divide by 100
$price = 1999 / 100;
echo $price;
?>

Code को Comment करके निकालना

किसी line (या block) को comment markers में wrap करना debugging के दौरान code को temporarily disable करने का एक तेज़ तरीका है, इसे पूरी तरह delete किए बिना।

यह debugging workflow का एक normal हिस्सा है, लेकिन shipped file में छोड़ा गया commented-out code आमतौर पर rot हो जाता है और अगले पढ़ने वाले को confuse कर देता है।

उदाहरण: Commenting Out Code

php
<?php
echo "This line runs.\n";
// echo "This line is disabled while debugging.\n";
echo "This line also runs.\n";
?>

Comment Best Practices

ऐसे Comments जो अब code से match नहीं करते no comment से भी बुरे हैं, क्योंकि वे actively misleading करते हैं। Comments को छोटा रखें, उन्हें non-obvious intent पर focused रखें, और जिस code को वे describe करते हैं वह बदलते ही उन्हें update या delete कर दें।

उदाहरण: Comment Best Practices

php
<?php
// Calculates the discounted price
function discount($price) {
    return $price * 0.9;
}
echo discount(100);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक /* */ comment को दूसरे के अंदर nest करना, जो PHP support नहीं करता -- पहला */ जो इसे मिलता है वह पूरे comment block को बंद कर देता है।
  2. एक line पर // comment ?> लिखना, जहाँ closing ?> tag एक single-line comment के अंदर भी PHP mode खत्म कर देता है।
  3. किसी multi-line block की सिर्फ पहली line पर // से code comment out करना, ताकि बाकी lines अभी भी चलें।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.