PHP Syntax क्या है?
Complete Guide in Hindi
PHP Syntax का पूरा परिचय हिंदी में — Opening/Closing Tags, echo, print, Comments, Case Sensitivity, Semicolon Rules, और Whitespace। Real examples के साथ।
📋 इस Article में क्या-क्या है (Table of Contents)
- PHP Syntax क्या है?
- PHP Tags — Opening & Closing
- Semicolon Rule
- echo और print
- PHP Comments
- Case Sensitivity
- Whitespace & Indentation
- HTML के साथ PHP
- Quick Reference Table
- FAQ और Conclusion
PHP Syntax वो rules हैं जो define करते हैं कि PHP code कैसे लिखा जाए। जैसे Hindi grammar के rules होते हैं वाक्य लिखने के लिए — वैसे ही PHP के भी rules हैं जो बताते हैं कि code का structure क्या होना चाहिए।
Standard Opening Tag — <?php
यह सबसे recommended और universally supported tag है। हमेशा इसी को use करें। सभी servers पर काम करता है।
Closing Tag — ?>
PHP code block को close करता है। Pure PHP files में closing tag optional है — और actually best practice है closing tag न लिखना (trailing whitespace issues से बचाता है)।
Short Tag — <? (Avoid करें)
यह short tag है जो server configuration पर depend करता है। सभी servers पर काम नहीं करता। Production code में use न करें।
Echo Short Tag — <?=
यह <?php echo का shortcut है। PHP 5.4+ में हमेशा enabled रहता है। HTML templates में use होता है।
<?php
echo "Hello World";
?>
// ✅ Echo Short Tag — HTML में useful
<h1>Welcome, <?= $naam ?></h1>
// ❌ Short Tag — avoid करें
<? echo "Risky!"; ?>
// ✅ Pure PHP file — closing tag नहीं लिखते
<?php
// No closing tag here — best practice
$name = "Rahul";
✅ सही — Semicolon है
echo "Hello";
$x = 10;
$y = 20;
echo $x + $y;
❌ गलत — Semicolon नहीं
echo "Hello" // Error!
$x = 10 // Error!
$y = 20 // Error!
echo $x + $y // Error!
Parse error: syntax error, unexpected token — यह देखते ही semicolon check करो।
echo — सबसे ज़्यादा use होता है
Multiple values print कर सकता है। कोई return value नहीं। print से थोड़ा faster। Most common choice।
print — कम use होता है
एक value print करता है। Return value 1 देता है। Expressions में use हो सकता है। echo से slightly slow।
// echo — multiple values एक साथ
echo "नमस्ते", " ", "दुनिया";
echo "मेरा नाम Rahul है।";
// echo — variables के साथ
$naam = "Priya";
echo "Hello, " . $naam . "!";
// echo — Double quotes में variable directly
echo "Hello, $naam! कैसे हो?";
// print — single value
print "यह print से आया है।";
// echo — HTML भी print होता है
echo "<h1>PHP सीखो</h1>";
echo "<p style='color:red'>लाल text</p>";
?>
echo "Hello $naam"; → Hello Priyaecho 'Hello $naam'; → Hello $naam
// यह single-line comment है (सबसे ज़्यादा use होता है)
echo "Hello"; // यह same line पर comment है
# यह भी single-line comment है (Python style)
$naam = "Rahul"; # naam store कर रहे हैं
/* यह multi-line comment है। यहाँ multiple lines लिख सकते हो। Function या class explain करने के लिए use होता है। */
function add($a, $b) {
return $a + $b;
}
/** * यह PHPDoc comment है — professional code में use होता है। * @param int $a पहला number * @param int $b दूसरा number * @return int दोनों का sum */
function multiply(int $a, int $b): int {
return $a * $b;
}
?>
Single-line — // (Recommended)
सबसे ज़्यादा use होता है। Line के बाद या line के बीच में। Short explanations के लिए।
Single-line — # (Alternative)
Python/Bash style। PHP में valid है लेकिन कम use होता है। // prefer करें।
Multi-line — /* */
Multiple lines के लिए। Functions, classes explain करने के लिए। Code temporarily disable करने के लिए।
PHPDoc — /** */
Professional documentation के लिए। IDE tools इसे read करते हैं। @param, @return जैसे tags use होते हैं।
❌ Bad:
$x = $x + 1; // x में 1 add करो — यह obvious है।✅ Good:
$attempts++; // Max 3 login attempts allowed
✅ Case-INSENSITIVE (कोई फर्क नहीं)
echo "Hello";
ECHO "Hello";
Echo "Hello";
// Functions — same hai
strlen("PHP");
STRLEN("PHP");
Strlen("PHP");
❌ Case-SENSITIVE (फर्क पड़ता है!)
$naam = "Rahul";
$Naam = "Priya";
$NAAM = "Amit";
// तीनों अलग variables हैं!
echo $naam; // Rahul
echo $Naam; // Priya
echo $NAAM; // Amit
| PHP Element | Case Sensitive? | Example |
|---|---|---|
| Variables ($naam) | ✅ हाँ — sensitive | $naam ≠ $Naam ≠ $NAAM |
| Constants (DEFINE) | ✅ हाँ — sensitive | MAX ≠ max ≠ Max |
| Keywords (echo, if) | ❌ नहीं — insensitive | echo = ECHO = Echo |
| Functions (strlen) | ❌ नहीं — insensitive | strlen() = STRLEN() |
| Class Names | ❌ नहीं — insensitive | new Car() = new car() |
| Array Keys | ✅ हाँ — sensitive | $a['Name'] ≠ $a['name'] |
// यह दोनों same result देते हैं:
// Version 1 — compressed
$x=5;$y=10;echo $x+$y;
// Version 2 — readable (always prefer this)
$x = 5;
$y = 10;
echo $x + $y;
// Indentation example — if block
if ($x < $y) {
echo "x छोटा है"; // 4 spaces indent
}
?>
<html lang="hi">
<body>
<?php
$naam = "Rahul";
$umar = 25;
$city = "Delhi";
?>
<h1>नमस्ते, <?= $naam ?>!</h1>
<p>उम्र: <?= $umar ?> साल</p>
<p>शहर: <?= $city ?></p>
<?php if ($umar >= 18) : ?>
<p>आप adult हैं।</p>
<?php endif; ?>
</body>
</html>
| Syntax Element | Example | काम क्या है? |
|---|---|---|
| <?php | <?php echo "Hi"; | PHP block शुरू करता है |
| ?> | echo "Hi"; ?> | PHP block बंद करता है |
| <?= | <?= $naam ?> | echo का shortcut — HTML में use |
| ; | $x = 5; | Statement end — हमेशा ज़रूरी |
| echo | echo "Hello"; | Output print करना |
| print "Hello"; | Output print (return 1 देता है) | |
| // | // यह comment है | Single-line comment |
| # | # यह भी comment है | Single-line comment (alternative) |
| /* */ | /* multi line */ | Multi-line comment |
| /** */ | /** @param int $x */ | PHPDoc documentation comment |
| $variable | $naam = "Rahul"; | Variable — case sensitive, $ से start |
| . | "Hello" . $naam | String concatenation (जोड़ना) |
echo "Hello" . " " . $naam; — यह JavaScript के + जैसा है। Double quotes में variable directly भी expand होता है: echo "Hello $naam";PHP Syntax वो foundation है जिस पर बाकी सब कुछ build होता है। Tags सही लिखना, semicolon याद रखना, comments का सही use — यह सब habits आपको एक cleaner और better developer बनाती हैं।
<?php से शुरू करो — यह सबसे important और universal tag है।
हर statement के बाद semicolon — यह most common beginner mistake है।
echo use करो print की जगह — faster और more versatile है।
Variables case-sensitive हैं — keywords और functions नहीं।
Comments लिखो — future में खुद को और दूसरों को help करेगा।
Readable code लिखो — whitespace और indentation से code professional बनता है।