PHP Environment Setup
In this page:
Web Server Requirements
PHP code को एक web server चाहिए जो जानता हो कि .php requests PHP interpreter को कैसे सौंपनी हैं — Apache और Nginx सबसे common दो हैं।
हर हिस्से को अलग से install और configure करने के बजाय, ज़्यादातर beginners XAMPP या MAMP जैसा एक all-in-one bundle install करते हैं, जो एक web server, PHP, और MySQL को एक साथ package कर देता है।
उदाहरण: Web Server Requirements
<?php
// Declare `$server`, set to `$_SERVER['SERVER_SOFTWARE'] ?? 'PHP CLI'`
$server = $_SERVER['SERVER_SOFTWARE'] ?? 'PHP CLI';
// Print `"This request was handled by: " . $server` to the output
echo "This request was handled by: " . $server;
?>
Login to try C/C++/Java/PHP code in the editor
PHP Install करना
आप अपने OS package manager (या एक installer bundle) के through सीधे अपनी machine पर PHP install कर सकते हैं, या local setup पूरी तरह skip करके अभी syntax सीखते समय browser-based playground में PHP लिख सकते हैं — यह तब उपयोगी है जब आप सिर्फ बिना पूरा server खड़ा किए एक snippet test करना चाहते हैं।
उदाहरण: Installing PHP
<?php
// Run this file with: php filename.php (or via a browser-based playground)
echo "PHP is installed and running.";
?>
Login to try C/C++/Java/PHP code in the editor
अपना Code लिखना
अपना code एक plain text file में .php extension के साथ save करें ताकि web server इसे static file की तरह serve करने के बजाय PHP interpreter को route करे।
उस file के अंदर, PHP code <?php और ?> tags के बीच रहता है; उन tags के बाहर की हर चीज़ ज्यों की त्यों pass हो जाती है, जो आपको PHP और HTML को freely mix करने देता है।
उदाहरण: Writing Your Code
<?php
// Print "This runs inside the tags." to the output
echo "This runs inside the tags.";
?>
<p>This HTML passes through untouched.</p>
Login to try C/C++/Java/PHP code in the editor
PHP को Locally चलाना
आप एक .php file को दो तरीकों से चला सकते हैं: एक configured web server के through (browser में visit करके), या सीधे terminal से php filename.php के साथ, जो script execute करके इसका output terminal पर print कर देता है — बिना किसी browser को छुए quick tests के लिए handy।
उदाहरण: Running PHP Locally
<?php
// Run with: php filename.php
echo "Printed directly to the terminal, no browser needed.";
?>
Login to try C/C++/Java/PHP code in the editor
Version Verify करना
यह देखने के लिए कि कौन सा PHP version installed है terminal में php -v चलाएँ। PHP 7.4 या नए के लिए aim करें: arrow functions, typed properties, और null coalescing assignment operator (??=) जैसा modern syntax पुराने interpreters पर बस parse ही नहीं होगा।
उदाहरण: Verifying the Version
<?php
// Print `"PHP version: " . PHP_VERSION` to the output
echo "PHP version: " . PHP_VERSION;
// Print `PHP_VERSION_ID >= 70400 ? "\nModern syntax supported." : "\nUpgrade recommended."` to the output
echo PHP_VERSION_ID >= 70400 ? "\nModern syntax supported." : "\nUpgrade recommended.";
?>
Login to try C/C++/Java/PHP code in the editor
- एक
.phpfile को double-click करना या disk से browser में खोलना, जो कभी PHP invoke नहीं करता; file को एक web server के through serve किया जाना चाहिए याphp file.phpसे चलाया जाना चाहिए। - web server के document root (जैसे
htdocsया/var/www/html) के बाहर files save करना, ताकि browser URL 404 return करे भले ही code सही हो। php.iniedit करने या कोई extension enable करने के बाद Apache या Nginx restart करना भूल जाना, ताकि पुरानी settings effect में रहें।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: