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

PHP Multiline Comments

एक single-line comment किसी quick note के लिए ठीक है, लेकिन कभी-कभी आपको code का पूरा block temporarily disable करना होता है, या कई lines तक फैली एक लंबी explanation लिखनी होती है -- हर single line पर // दोबारा type करना थकाऊ होगा। PHP का multiline comment syntax, /* ... */, आपको किसी भी amount के code या text को एक opening और एक closing marker के बीच wrap करने देता है।
Syntax
php
/*
   multi-line comment
   spanning several lines
*/

/**
 * Documentation block above a function.
 */

Basic Multiline Comment Syntax

एक multiline comment /* से शुरू होता है और अगले */ पर खत्म होता है, और उन दोनों markers के बीच की हर चीज़ -- चाहे कितनी भी lines में हो -- PHP interpreter द्वारा पूरी तरह ignore कर दी जाती है, बिल्कुल ऐसे जैसे यह कभी लिखी ही न गई हो।

उदाहरण: Basic Multiline Comment Syntax

php
<?php
/* This entire
   multiline comment
   is ignored by PHP */
echo "Only this line runs.";
?>

Debugging के लिए Code का Block Disable करना

/* */ का एक common practical इस्तेमाल है debugging के दौरान code के एक पूरे block को बिना delete किए temporarily disable करना -- जिस block को skip करना है उसे /* और */ में wrap करें, अपनी script चलाएँ, और testing खत्म होते ही markers को वापस हटा दें।

उदाहरण: Disabling a Block of Code for Debugging

php
<?php
echo "Before the disabled block\n";
/*
echo "This block is skipped";
echo "So is this line";
*/
echo "After the disabled block\n";
?>

Functions के ऊपर Documentation Blocks लिखना

Multiline comments किसी function के ठीक ऊपर एक छोटा documentation block लिखने का standard तरीका हैं, यह explain करते हुए कि यह क्या करता है, इसे कौन से parameters चाहिए, और यह क्या return करता है -- file खोलने वाला कोई भी व्यक्ति इसे एक नज़र में पढ़ सके, बिना किसी अलग documentation page की ज़रूरत के।

उदाहरण: Writing Documentation Blocks Above Functions

php
<?php
/*
 * Adds two numbers together.
 * Parameters: $a, $b - the numbers to add
 * Returns: the sum
 */
function addNumbers($a, $b) {
    return $a + $b;
}
echo addNumbers(2, 3);
?>

Comments Nest क्यों नहीं हो सकते

PHP एक opening /* के बाद बिल्कुल पहले */ के लिए scan करता है, और comment को वहीं बंद कर देता है -- यह nesting depth track नहीं करता।

इसका मतलब है कि /* outer /* inner */ still active code */ जैसा comment असल में "inner */" के बाद बंद हो जाता है, " still active code */" को real, uncommented PHP code के रूप में छोड़ते हुए, जो आमतौर पर एक syntax error का कारण बनता है।

उदाहरण: Why Comments Cannot Be Nested

php
<?php
/* outer /* inner */
still active code
?>

Multiline Comments बनाम Single-Line Comments

PHP कुल तीन comment styles support करता है: single-line comments के लिए // और #, और multiline वालों के लिए /* */।

तीनों functionally बराबर हैं कि वे interpreter से क्या छुपाते हैं -- इनके बीच choice पूरी तरह readability और आपको कितनी lines cover करनी हैं इस पर है।

उदाहरण: Multiline Comments vs Single-Line Comments

php
<?php
// Single-line style
# Also single-line style
/* Multiline
   style */
echo "All three styles are functionally identical to PHP.";
?>
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 को बंद कर देता है, बाकी हिस्से को silently un-comment करते हुए।
  2. closing */ पूरी तरह भूल जाना, जो उस point से file के अंत तक सब कुछ comment out कर देता है, उस code सहित जिसे आप active रखना चाहते थे।
  3. एक छोटे single-line note के लिए /* */ इस्तेमाल करना जब // ज़्यादा simple है और सही से खोलना-बंद करना कम error-prone है।
चैप्टर सारांश
  • /* */ किसी comment को कई lines में wrap करता है, // के उलट जो सिर्फ एक single line के बाकी हिस्से को comment out करता है।
  • PHP multiline comments nested नहीं हो सकते -- पहला closing */ पूरे block को खत्म कर देता है।
  • Multiline comments आमतौर पर debugging के दौरान code के किसी block को temporarily disable करने, या किसी function के ऊपर एक लंबी doc-style explanation लिखने के लिए इस्तेमाल होते हैं।

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.