Introduction to Basic Commands in PHP in Hindi
RGPV University / DIPLOMA_CSE / Web Technology
Introduction to Basic Commands in PHP
Table of Contents
Introduction to Basic Commands in PHP
PHP (Hypertext Preprocessor) एक server-side scripting language है, जो dynamic web pages बनाने के लिए इस्तेमाल होती है। PHP को mainly web development में उपयोग किया जाता है और ये एक powerful tool है जो HTML में embedded होता है। इस लेख में हम PHP के कुछ basic commands को समझेंगे, जो एक student के लिए बहुत important हैं। PHP के fundamental commands को समझकर, आप web development के projects में आसानी से काम कर सकते हैं।
1. Echo and Print in PHP
PHP में output generate करने के लिए सबसे commonly used commands हैं echo और print। ये दोनों commands web page पर text display करने के लिए उपयोग होते हैं, लेकिन इनके बीच कुछ differences भी हैं।
- Echo - यह एक language construct है जो data को output में भेजने के लिए उपयोग होता है। इसमें multiple parameters को comma से separate करके भेज सकते हैं। इसे parentheses के साथ या बिना parentheses के इस्तेमाल किया जा सकता है।
- Print - यह एक expression है और इसे सिर्फ एक argument के साथ use किया जा सकता है। print हमेशा 1 return करता है, जबकि echo कुछ return नहीं करता।
Code Example:
2. Variables in PHP
PHP में variables किसी भी data ko store करने के लिए उपयोग होते हैं। हर variable की शुरुआत $ symbol से होती है, और variable name को alphabet या underscore से शुरू करना होता है। PHP में variables case-sensitive होते हैं, मतलब $Var और $var अलग-अलग variables हैं।
- Variable Declaration: PHP में variable declare करने के लिए आपको value assign करनी होती है। Example:
$name = "John";
- Data Types in Variables: PHP में variable की data type dynamic होती है, इसका मतलब है कि आप एक ही variable में different types की values store कर सकते हैं।
Code Example:
3. Data Types in PHP
PHP में data types की 4 main categories होती हैं:
- Scalar Types: इनमें integers, floating point numbers (floats), booleans और strings आते हैं।
- Compound Types: इनमें arrays और objects आते हैं।
- Special Types: इसमें NULL और resources आते हैं।
Code Example (Different Data Types):
4. Arrays in PHP
PHP में arrays का उपयोग एक से अधिक values को store करने के लिए किया जाता है। Arrays indexed (numeric) या associative (key-value pair) हो सकते हैं।
- Indexed Arrays: इसमें elements index numbers के जरिए store होते हैं।
- Associative Arrays: इसमें elements के लिए custom keys assign की जाती हैं।
Code Example:
"John", "age" => 25); // Associative Array
echo $person["name"]; // Output: John
?>
5. Loops in PHP
Loops का उपयोग किसी task को multiple times perform करने के लिए किया जाता है। PHP में 3 प्रकार के loops होते हैं:
- For Loop: जब आपको fixed number of times loop run करना हो, तब for loop का उपयोग करते हैं।
- While Loop: जब तक condition true रहे, loop execute होता रहेगा।
- Foreach Loop: यह loop primarily arrays और objects के लिए उपयोग होता है।
Code Example (For Loop):
";
}
?>
Code Example (While Loop):
";
$i++;
}
?>
Code Example (Foreach Loop):
";
}
?>
FAQs
PHP (Hypertext Preprocessor) एक server-side scripting language है, जो dynamic web pages और applications बनाने के लिए उपयोग होती है। इसे HTML के साथ embed करके web pages पर output generate किया जाता है।
Echo: Echo एक language construct है, जो multiple parameters accept कर सकता है।
Print: Print एक expression है, जो सिर्फ एक parameter accept करता है और हमेशा 1 return करता है, जबकि echo कुछ return नहीं करता।
PHP में variables एक placeholder होते हैं जिनमें data store किया जाता है। सभी variables $ symbol से शुरू होते हैं और इनमें numeric, string, boolean जैसे data types store किए जा सकते हैं।
PHP में data types 4 categories में बाँटे जाते हैं:
Scalar Types: Integer, Float, String, Boolean
Compound Types: Arrays, Objects
Special Types: NULL, Resource
PHP में arrays का उपयोग multiple values store करने के लिए किया जाता है। Arrays दो प्रकार के होते हैं: Indexed Arrays और Associative Arrays। Indexed Arrays में values index numbers से होती हैं, जबकि Associative Arrays में custom keys assign की जाती हैं।
For Loop: For loop का उपयोग तब किया जाता है जब हमको fixed number of iterations चाहिए होती हैं।
While Loop: While loop तब तक execute होता है जब तक उसकी condition true रहती है, यह number of iterations पर depend नहीं करता।