PHP First Program
In this page:
<?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
<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>
Login to try C/C++/Java/PHP code in the editor
echo Statement
echo text (या किसी expression का result) सीधे page के output में भेज देता है। यह technically एक function के बजाय एक language construct है, इसलिए आप इसे बिना parentheses के call कर सकते हैं, और एक ही call में कई comma-separated values pass कर सकते हैं।
उदाहरण: The echo Statement
<?php
// Print "Hello", " ", "World!" to the output
echo "Hello", " ", "World!";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// Print `4 + 6` to the output
echo 4 + 6;
?>
Login to try C/C++/Java/PHP code in the editor
अपनी पहली 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
// Missing semicolon below would trigger: parse error, unexpected token
echo "Script ran successfully.";
?>
Login to try C/C++/Java/PHP code in the editor
echo Helloके बाद semicolon भूल जाना, जो अगली line पर एक parse error का कारण बनता है।- opening
<?phptag छोड़ देना या एक bare<?short tag लिखना, जो कई servers पर enabled नहीं होता, ताकि code plain text की तरह print हो। - quotes को mix up करना, जैसे
echo Its here';, जहाँ apostrophe string को जल्दी खत्म कर देता है और एक syntax error का कारण बनता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: