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

PHP First Program

आपका पहला PHP program आपके ज़ोर से बोले गए पहले Hello जैसा है: एक छोटा सा code जो page को कुछ कहलाता है। PHP code special opening और closing markers के अंदर जाता है ताकि computer को पता चले कि यह कहाँ शुरू और खत्म होता है।
Syntax
php
<?php
    echo "text to output";
?>

Opening और Closing Tags

PHP code का हर block <?php से शुरू होता है और ?> से खत्म होता है।

Web server उन tags के बीच की किसी भी चीज़ को PHP मानकर execute करता है, और बाकी सब — आसपास के HTML सहित — को ज्यों का त्यों pass होने वाला plain text मानता है, जो एक ही file में markup और logic mix करने देता है।

उदाहरण: Opening and Closing Tags

php
<p>Before the tag: plain HTML</p>
<?php
// Print "Inside the tag: PHP executes this." to the output
echo "Inside the tag: PHP executes this.";
?>
<p>After the tag: plain HTML again</p>

echo Statement

echo text (या किसी expression का result) सीधे page के output में भेज देता है। यह technically एक function के बजाय एक language construct है, इसलिए आप इसे बिना parentheses के call कर सकते हैं, और एक ही call में कई comma-separated values pass कर सकते हैं।

उदाहरण: The echo Statement

php
<?php
// Print "Hello", " ", "World!" to the output
echo "Hello", " ", "World!";
?>

Simple Strings Handle करना

PHP में text single या double quotes के अंदर जाता है, लेकिन वे अलग behave करते हैं: single quotes लगभग हर चीज़ literally print करती हैं (एक $variable reference को plain text की तरह सहित), जबकि double quotes actively variables evaluate करती हैं और \n जैसे escape sequences को newline के रूप में interpret करती हैं।

उदाहरण: Handling Simple Strings

php
<?php
// Declare `$name`, set to "Alice"
$name = "Alice";
// Print 'Single quotes: $name stays literal' to the output
echo 'Single quotes: $name stays literal';
// Print "\n" to the output
echo "\n";
// Print "Double quotes: $name is substituted\n" to the output
echo "Double quotes: $name is substituted\n";
?>

Simple Math Output

PHP arithmetic expressions को पहुँचते ही evaluate कर देता है, इसलिए echo 4 + 6; text '4 + 6' print नहीं करता — यह पहले sum compute करके 10 output करता है।

यह बाद में numbers के साथ आप जो भी ज़्यादा complex काम करेंगे उसका आधार है, जैसे totals calculate करना या prices format करना।

उदाहरण: Simple Math Output

php
<?php
// Print `4 + 6` to the output
echo 4 + 6;
?>

अपनी पहली Script चलाना

एक बार आप एक script लिखकर चलाएँ, output को अपनी उम्मीद के against carefully check करें — एक missing semicolon या एक unclosed quote सबसे common first-script error है, और PHP का error message आमतौर पर आपको exactly उस line पर पहुँचाएगा जहाँ यह confused हुआ।

उदाहरण: Running Your First Script

php
<?php
// Missing semicolon below would trigger: parse error, unexpected token
echo "Script ran successfully.";
?>
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. echo Hello के बाद semicolon भूल जाना, जो अगली line पर एक parse error का कारण बनता है।
  2. opening <?php tag छोड़ देना या एक bare <? short tag लिखना, जो कई servers पर enabled नहीं होता, ताकि code plain text की तरह print हो।
  3. quotes को mix up करना, जैसे echo Its here';, जहाँ apostrophe string को जल्दी खत्म कर देता है और एक syntax 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.