PHP Comments
In this page:
<?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
// This is a single-line comment
# So is this
echo "Comments above are ignored";
?>
Login to try C/C++/Java/PHP code in the editor
Multi-Line Comments
एक /* ... */ comment कई lines तक फैल सकता है, जो इसे लंबी explanations के लिए सही tool बनाता है: कोई function क्या करता है describe करना, expected parameters document करना, या अपनी सोच भूलने से पहले किसी tricky algorithm को temporarily annotate करना।
उदाहरण: Multi-Line Comments
<?php
/*
* This function doubles a number.
* Spans multiple lines to explain the logic.
*/
echo 5 * 2;
?>
Login to try C/C++/Java/PHP code in the editor
Code Document करना
सबसे उपयोगी comments यह explain करते हैं कि कोई piece of code non-obvious form में *क्यों* exist करता है — किसी browser quirk के लिए एक workaround, एक business rule जो सिर्फ code से visible नहीं है — बजाय यह दोहराने के कि code *क्या* करता है, जो code खुद पहले से दिखाता है।
उदाहरण: Documenting Code
<?php
// Workaround: legacy API returns price in cents, so we divide by 100
$price = 1999 / 100;
echo $price;
?>
Login to try C/C++/Java/PHP code in the editor
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
echo "This line runs.\n";
// echo "This line is disabled while debugging.\n";
echo "This line also runs.\n";
?>
Login to try C/C++/Java/PHP code in the editor
Comment Best Practices
ऐसे Comments जो अब code से match नहीं करते no comment से भी बुरे हैं, क्योंकि वे actively misleading करते हैं। Comments को छोटा रखें, उन्हें non-obvious intent पर focused रखें, और जिस code को वे describe करते हैं वह बदलते ही उन्हें update या delete कर दें।
उदाहरण: Comment Best Practices
<?php
// Calculates the discounted price
function discount($price) {
return $price * 0.9;
}
echo discount(100);
?>
Login to try C/C++/Java/PHP code in the editor
- एक
/* */comment को दूसरे के अंदर nest करना, जो PHP support नहीं करता -- पहला*/जो इसे मिलता है वह पूरे comment block को बंद कर देता है। - एक line पर
// comment ?>लिखना, जहाँ closing?>tag एक single-line comment के अंदर भी PHP mode खत्म कर देता है। - किसी multi-line block की सिर्फ पहली line पर
//से code comment out करना, ताकि बाकी lines अभी भी चलें।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: