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

PHP Environment Setup

अपने computer पर PHP चलाने के लिए आपको एक छोटा helper program चाहिए जो PHP files को समझे, जैसे कोई movie दिखाने से पहले आपको projector चाहिए। एक बार set up हो जाने पर, आपका computer एक mini website server की तरह act करता है।

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

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
<?php
// Run this file with: php filename.php  (or via a browser-based playground)
echo "PHP is installed and running.";
?>

अपना 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
<?php
// Print "This runs inside the tags." to the output
echo "This runs inside the tags.";
?>
<p>This HTML passes through untouched.</p>

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
<?php
// Run with: php filename.php
echo "Printed directly to the terminal, no browser needed.";
?>

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
<?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.";
?>
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. एक .php file को double-click करना या disk से browser में खोलना, जो कभी PHP invoke नहीं करता; file को एक web server के through serve किया जाना चाहिए या php file.php से चलाया जाना चाहिए।
  2. web server के document root (जैसे htdocs या /var/www/html) के बाहर files save करना, ताकि browser URL 404 return करे भले ही code सही हो।
  3. php.ini edit करने या कोई extension enable करने के बाद Apache या Nginx restart करना भूल जाना, ताकि पुरानी settings effect में रहें।

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.