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

PHP Syntax और Structure

PHP syntax language का grammar है। जैसे sentences एक period से खत्म होते हैं, वैसे ही हर PHP instruction एक semicolon से खत्म होता है ताकि computer को पता चले कि एक order कहाँ रुकता है और अगला कहाँ शुरू होता है।
Syntax
php
<?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
<?php
echo "First statement";
echo "Second statement";
// Each statement ends with a semicolon, like a period ends a sentence
?>

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
<?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.";
?>

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
<?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";
}
?>

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
<?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.";
?>

Code Nesting

क्योंकि PHP tags किसी HTML file के अंदर कहीं भी आ सकते हैं, आप छोटे PHP snippets सीधे template में डाल सकते हैं — एक <h1> में username print करना, मान लीजिए — बिना पूरी file को PHP में बदले।

Server सिर्फ वही execute करता है जो <?php और ?> के बीच है; आसपास का HTML ज्यों का त्यों pass हो जाता है।

उदाहरण: Code Nesting

php
<?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>
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. किसी statement के अंत में semicolon भूल जाना, ताकि PHP semicolon missing होने की जगह के बजाय अगली line पर एक parse error report करे।
  2. एक जगह $Name लिखना और दूसरी जगह $name, क्योंकि variable names case-sensitive हैं और दूसरा एक अलग, undefined variable है।
  3. एक { को unclosed या mismatched छोड़ना, जो असली गलती से दूर एक 'unexpected end of file' parse error का कारण बनता है।

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.