PHP Syntax और Structure
In this page:
<?php
statement_one; // each statement ends with a semicolon
statement_two;
if (condition) {
// block of code in braces
}
?>
Semicolons
PHP हर statement के अंत को mark करने के लिए एक semicolon इस्तेमाल करता है, ठीक वैसे ही जैसे एक period किसी sentence को खत्म करता है — यह वह तरीका है जिससे interpreter जानता है कि एक instruction खत्म हो गया और अगला शुरू हो रहा है।
इसे छोड़ देना सबसे common beginner errors में से एक है, और यह आमतौर पर *अगली* line पर एक 'parse error, unexpected token' produce करता है, missing वाली line पर नहीं।
उदाहरण: Semicolons
<?php
echo "First statement";
echo "Second statement";
// Each statement ends with a semicolon, like a period ends a sentence
?>
Login to try C/C++/Java/PHP code in the editor
PHP में Case Sensitivity
PHP में language keywords और built-in function names (if, echo, strlen) case-insensitive हैं, इसलिए ECHO और echo एक जैसे behave करते हैं।
Variable names इसके उलट हैं: $Name और $name दो पूरी तरह अलग variables हैं, इसलिए consistent casing variables के लिए keywords से कहीं ज़्यादा मायने रखता है।
उदाहरण: Case Sensitivity in PHP
<?php
ECHO "Keywords are case-insensitive.\n";
// Declare `$Name`, set to "Alice"
$Name = "Alice";
// Declare `$name`, set to "Bob"
$name = "Bob";
// Print "$Name and $name are different variables." to the output
echo "$Name and $name are different variables.";
?>
Login to try C/C++/Java/PHP code in the editor
Braces और Blocks
Curly braces { } mark करते हैं कि code का एक block कहाँ शुरू और खत्म होता है — किसी function की body, if/else की branches, या किसी loop की body।
Braces के अंदर की हर चीज़ एक unit की तरह साथ execute होती है, जो आपको एक single condition या loop के तहत कई statements को group करने देता है।
उदाहरण: Braces and Blocks
<?php
// Check whether `true`
if (true) {
// Print "Statement one in the block.\n" to the output
echo "Statement one in the block.\n";
// Print "Statement two in the block.\n" to the output
echo "Statement two in the block.\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
Whitespace और Layout
PHP indentation या extra blank lines की उतनी परवाह नहीं करता जितना कुछ languages करती हैं — वे सिर्फ human readability के लिए हैं।
फिर भी, consistent indentation nested blocks (functions के अंदर conditions के अंदर loops) को एक नज़र में पढ़ना कहीं ज़्यादा आसान बनाती है, इसलिए ज़्यादातर style guides इसे enforce करती हैं भले ही interpreter इसे ज़रूरी न बनाए।
उदाहरण: Whitespace and Layout
<?php
// Print "Indentation is ignored by PHP" to the output
echo "Indentation is ignored by PHP";
// Print "but blank lines don't matter either." to the output
echo "but blank lines don't matter either.";
?>
Login to try C/C++/Java/PHP code in the editor
Code Nesting
क्योंकि PHP tags किसी HTML file के अंदर कहीं भी आ सकते हैं, आप छोटे PHP snippets सीधे template में डाल सकते हैं — एक <h1> में username print करना, मान लीजिए — बिना पूरी file को PHP में बदले।
Server सिर्फ वही execute करता है जो <?php और ?> के बीच है; आसपास का HTML ज्यों का त्यों pass हो जाता है।
उदाहरण: Code Nesting
<?php $username = "Alice"; ?>
<?php // The PHP tag below prints $username directly into the HTML ?>
<h1>Welcome, <?php echo $username; ?></h1>
<p>This HTML passes through untouched.</p>
Login to try C/C++/Java/PHP code in the editor
- किसी statement के अंत में semicolon भूल जाना, ताकि PHP semicolon missing होने की जगह के बजाय अगली line पर एक parse error report करे।
- एक जगह
$Nameलिखना और दूसरी जगह$name, क्योंकि variable names case-sensitive हैं और दूसरा एक अलग, undefined variable है। - एक
{को unclosed या mismatched छोड़ना, जो असली गलती से दूर एक 'unexpected end of file' parse error का कारण बनता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: