PHP – Introduction & Installation
PHP – Introduction
- Introduction to PHP
- Features of PHP
- Future scope of PHP
Introduction to PHP Hypertext Preprocessor
PHP के बारे में detail से जानने से पूर्व आइये जानते है की PHP की आवश्यकता क्यों है? HTML एक बहुत ही आसान और उपयोगी language है। HTML के द्वारा आप web pages design करते है।
HTML के साथ CSS के उपयोग से आप web pages को और भी beautiful बनाते है। लेकिन HTML सिर्फ web pages के structure और designing तक ही सिमित है। ऐसे कई tasks है जो HTML द्वारा नहीं perform किये जा सकते है। जैसे की –
- HTML के द्वारा आप information server पर store नहीं कर सकते है और server से कोई information access भी नहीं कर सकते है।
- इसके अलावा HTML के द्वारा आप databases में data store नहीं कर सकते है और database से data access भी नहीं कर सकते है।
- और HTML के द्वारा webpages में logical/mathematical tasks भी perform नहीं किये जा सकते है।
उदाहरण के लिए मान लीजिये आपने एक HTML form create किया है। इस form में user द्वारा data input किया जाता है। User द्वारा input किए गए data को आप किसी server या database में store करना चाहते है।
इसी situation में HTML में ऐसा कोई feature नहीं है जिसके द्वारा data को server पर या database में store किया जा सके। HTML के साथ PHP को use करते हुए आप webpages में computational/server/database programming कर सकते है।
HTML के द्वारा आप किसी webpage में addition भी नहीं करवा सकते है। लेकिन PHP आपको किसी programming language की capability provide करती है। PHP के द्वारा आप webpages में वो सब कर सकते है जो आप किसी programming language के द्वारा करते है।
PHP के latest versions object oriented programming को support करते है। आइये अब PHP के बारे में detail से जानने का प्रयास करते है।
What is PHP?
PHP एक open source server side scripting लैंग्वेज है। PHP dynamic webpages develop करने के लिए use की जाती है। PHP का पूरा नाम PHP Hypertext Preprocessor है। पहले इसे Personal Home Pages के नाम से जाना जाता था।
⇨ Scripting language वो language होती है जो different applications को एक साथ जोड़ती है। उदाहरण के लिए web browser और server के बीच में PHP काम करती है। इसलिए PHP एक server side scripting language कहलाती है
JavaScript भी एक scripting language है लेकिन JavaScript user के CPU और web browser के बीच काम करती है। इसलिए वह client side scripting language कहलाती है।
⇨ Dynamic web pages ऐसे web pages होते है जिनका content कुछ events के according change हो जाता है। उदाहरण के लिए एक dynamic web page load होते समय आपकी location के हिसाब से आपको आपकी city का current weather बता सकता है।
ज्यादातर PHP का syntax C language से मिलता है। यदि आपने C language पढ़ रखी है तो आप आसानी से PHP को सिख सकते है। ये एक बहुत ही simple language है।
Applications of PHP
PHP को यूज़ करते हुए आप 3 मुख्य काम कर सकते है –
- Server side scripting – यह PHP का सबसे मुख्य कार्य होता है। PHP को इसी के लिए design किया गया था। इसके लिए आपको एक PHP parser, web server और web browser की जरुरत होती है।
- Command line scripting – आप PHP code को बिना server या browser के भी run कर सकते है। इसके लिए आपको सिर्फ PHP parser की आवश्यकता होती है। इसे आप text processing के लिए use कर सकते है।
- Desktop applications – PHP के द्वारा आप desktop applications भी create कर सकते है।
Features of PHP
PHP के कुछ features नीचे दिए जा रहे है। इन features से आप PHP का quick idea लगा सकते है।
- Open source – PHP आपको freely available है। इसका code publicly download के लिए internet पर उपलब्ध है। इसको आप अपने यूज़ के according modify कर सकते है।
- Scripting language – PHP एक scripting language है।
- Can be embedded into HTML – PHP को आसानी से HTML के साथ यूज़ किया जा सकता है।
- Generates HTML – Execute होने के बाद PHP code simple HTML के रूप में show हो जाता है।
- Server side – PHP आज के ज्यादातर web servers को support करती है। PHP का code server पर ही execute किया जाता है।
- Can interact with databases – PHP के द्वारा आप databases के साथ interact कर सकते है। PHP बहुत सारे databases को भी सपोर्ट करती है। PHP को यूज़ करते हुए databases से interact करना बहुत आसान है।
- Secure – जहाँ जँहा भी आप अपने program में PHP को यूज़ करेंगे server side पर execute होने के बाद उसका result आपके webpage में PHP code को replace कर देता है और simple HTML के रूप में webpage पर show होता है। Webpage generate होने के बाद PHP का code user को show नहीं होता है। आपका PHP code hidden रहता है उसे modify नहीं किया जा सकता है।
Future Scope of PHP
PHP एक बहुत ही popular और सिखने में आसान language है। सभी छोटी और बड़ी companies PHP को use कर रही है। बीते कुछ सालों में PHP को पूरी तरह change कर दिया गया है। PHP में object oriented programming features से लेकर popular content management systems जैसे की Joomla और Drupal के लिए भी support provide किया गया है।
____________________________________
PHP – Installation
- Introduction to XAMPP
Introduction to Installing PHP
HTML webpage को execute करने के लिए किसी भी प्रकार के special software की आवश्यकता नहीं होती है। आप simply HTML code लिखते है और उसे .html extension के साथ save करते है। इसके बाद जैसे ही उस file को open करते है तो आपका code execute हो जाता है और web page के रूप में show हो जाता है। लेकिन PHP के साथ ऐसा नहीं है।
PHP एक scripting language है जो आपको web pages में logic implement करने की ability provide करती है। किसी भी दूसरी programming language की तरह PHP आपको object oriented programming features भी provide करती है। इसलिए PHP scripts को execute करने के लिए आपको PHP interpreter की आवश्यकता होती है।
क्योंकि PHP एक server side scripting language है इसलिए PHP scripts को execute करने के लिए एक server की भी आवश्यकता होती है। Server के बिना PHP scripts को execute नहीं किया जा सकता है। PHP में development सिखने के लिए Apache use किया जाने वाला सबसे common server है।
यदि आप कोई database application (Data को store करने के लिए) create कर रहे है तो आपको एक DBMS जैसे की MySQL की भी जरुरत पड़ती है।
आप चाहे तो PHP, MySQL और Apache server को separately install करके configure कर सकते है। लेकिन आपको ऐसा करने की आवश्यकता नहीं है। PHP में आसानी से development करने के लिए Apache Friends द्वारा XAMPP package provide किया गया है।
What is XAMPP ?
XAMPP एक free, open source web server solution package है। इसे Apache Friends द्वारा develop किया गया है। इसमें मुख्यतः Apache HTTP Server, MySQL Database और PHP interpreter files available है। XAMPP एक cross platform package है। यह सभी मुख्य platforms के लिए available है।
XAMPP को install करने के बाद आपको किसी भी दूसरे software को install करने की आवश्यकता नहीं होती है। यह PHP development के लिए एक complete package है। जब आप XAMPP को install करते है तो आपके computer में automatically PHP, Apache Server और MySQL आदि दूसरी services install हो जाती है।
PHP – Tags & First PHP Program
- Introduction to PHP tags
- Types of PHP tags
- Commenting PHP code
Introduction to PHP Tags
जब कोई PHP file parse होती है तो parser opening और closing tags को search करता है। Basically parser interpreter का part होता है और PHP code को अलग अलग हिस्सों में तोड़ता है ताकि पता लग सके की कौनसा हिस्सा tag है और कौनसा actual data है।
Parser वही कोड parse करता है जो opening और closing tags के बीच में हो बाकि सारा कोड parser ignore कर देता है। ऐसा हो सकता है की आपको अपने document में कई अलग अलग जगह पर PHP code यूज़ करना पड़े।
यदि आप ऐसा बिना PHP tags को define किये करते है तो parser उस PHP code को execute नहीं करेगा। इसलिए आप जँहा भी PHP code लिखेंगे वह PHP के opening और closing tags के बीच में ही होगा।
Types of PHP Tags
PHP 4 तरह के tags provide करती है। हालाँकि PHP के latest version में इनमें से कुछ tags को disallow कर दिया गया है। निचे इन tags के बारे में बताया जा रहा है।
1. PHP Common Tags
<?php
// Your PHP code here
?>
ये सबसे commonly यूज़ किये जाने वाले tags है। ज्यादातर standard applications में आप देखेंगे की इन्ही tags का इस्तेमाल किया जाता है।
2.PHP Script Tags
<script language ="PHP">
// Your PHP code here
</script>
जैसे आप javascript का code head section में script tag के द्वारा लिखते है वैसे ही आप PHP का code भी script tag में लिख सकते है।
इस type के PHP tags को PHP version 7.0.0 से disallow कर दिया गया है।
3.PHP ASP Style Tags
<%
// Your PHP code here
%>
अगर आप ASP के साथ familiar है तो आप ये ASP style tags यूज़ कर सकते है।
इस type के PHP tags को PHP version 7.0.0 से disallow कर दिया गया है।
4.PHP Short Form Tags
<?
// Your PHP code here
?>
यदि आप PHP tags की कोई short form ढूंढ रहे है तो आप ये tags यूज़ कर सकते है।
Commenting PHP Code
किसी भी दूसरी language की तरह आप PHP code को भी comment कर सकते है। Comments parser द्वारा parse नहीं किये जाते है। Comments किसी code को explain करने के लिए लिखे जाती है। Comments लिखने से कोई भी दूसरा programmer आसनी से उस code को समझ सकता है।
Comments लिखना बहुत अच्छी practice मानी जाती है। इससे सालों बाद भी यदि कोई programmer आपके code को देखता है तो वह आसनी से उसे समझ सकता है। PHP code को अलग अलग तरह से comment किया जा सकता है। इसके बारे में निचे बताया जा रहा है।
Single Line Comment
Single line comment syntax का प्रयोग तब किया जाता है जब comment किया जाने वाला text एक line से भी कम हो। Single line comment का उदाहरण निचे दिया जा रहा है।
// your comment here
इसके अलावा आप single line comment निचे दिए जा रहे तरीके से भी define कर सकते है।
# your comment here
इन दोनों तरीकों में फर्क सिर्फ इतना है की एक तरीके में comment // से start होता है और दूसरे तरीके में # से start होता है।
Multi Line Comment
Multiline comment syntax का प्रयोग तब किया जाता है जब comment के रूप में लिखा जाने वाला text एक line से अधिक होता है। Multiline comment का उदाहरण निचे दिया जा रहा है।
/* This is
your multi line
comment here */
____________________________________
PHP – First PHP Program
- Your first PHP program
- Running a PHP program
Your First PHP Program
PHP एक बहुत ही simple scripting language है। यदि आपने C language पढ़ी हुई है तो आप आसानी से PHP में web applications create कर सकते है। अब तक आप PHP के tags के बारे में जान चुके है। आइये अब PHP में एक simplest program बनाने का और उसे समझने का प्रयास करते है।
हालाँकि PHP को HTML के साथ embed किया जा सकता है। लेकिन आप चाहे तो only PHP का भी program create कर सकते है। निचे एक ऐसा ही pure PHP program दिया जा रहा है।
<?php
echo “Hello Reader <br>”;
echo “Welcome to Techpass App Master”;
?>
|
ऊपर दिए गए program में सबसे पहले <?php opening tag का प्रयोग किया गया है। जैसा की आपको पता PHP code हमेशा <?php और ?> tags के बीच में ही लिखा जाता है। इसलिए main PHP code लिखने से पूर्व इन tags को define करना आवश्यक होता है।
PHP opening tags के बाद echo function का 2 बार use किया गया है। यह function PHP में string messages और variables की values को print करने के लिए use किया जाता है। इस function में आप HTML tags को भी define कर सकते है।
जैसा की ऊपर दिए गए उदाहरण में <br> tag को define किया गया है जो की line break के लिए use किया जाता है।
यह एक बहुत ही simple उदाहरण है यह उदाहरण output के रूप में क्रमशः Hello Reader और Welcome to Techpass App Master print करता है।
Running a PHP Program
एक PHP program को आप किसी HTML program की तरह नहीं execute कर सकते है। क्योंकि PHP एक server side scripting language है, इसलिए PHP को server द्वारा execute किया जाता है।
PHP program को execute करना बताने से पहले यँहा पर assume किया जा रहा की की आपने XAMPP अपने computer में install कर लिया है और XAMPP control panel के द्वारा आपने Apache server को start कर दिया है।
XAMPP एक software package है जो आपको PHP में development करने के लिए PHP interpreter, Apache server और MySQL जैसे दूसरी services provide करता है।
यदि आपने अभी तक XAMPP install नहीं किया है तो कृपया PHP Installation वाली tutorial को follow करके आप XAMPP install कर लीजिये।
PHP files server द्वारा execute की जाती है इसलिए सभी PHP files को server पर store किया जाता है। XAMPP आपके computer में local server को install कर देता है जिससे आपका computer ही server बन जाता है।
क्योंकि आप अभी PHP में web applications create करना सिख ही रहे है इसलिए आप local server पर काम करेंगे। एक बार जब आप अच्छी तरह सिख लें तो आप live server पर भी काम कर सकते है।
चाहे local server use करें या live server concept दोनों में same ही होता है। आप server की किसी directory में अपनी files को store करते है। बाद में जब web browser द्वारा उस file के लिए request की जाती है तो server उन्हें execute करता है और result browser में show हो जाता है।
Live server पर files को upload करने के लिए कई applications available है। इस काम के लिए PHP के साथ FileZilla service अत्यधिक use की जाती है। लेकिन क्योंकि अभी आप local server पर काम करेंगे इसलिए आपको इसे use करने की आवश्यकता नहीं है।
XAMPP द्वारा create किये गए local server की directory आपको install की गयी drive में XAMPP folder के अंदर htdocs के नाम से मिलेगी। आप अपनी सभी files इसी directory में store करेंगे।
इस directory में आप directly भी अपनी PHP files को store कर सकते है या फिर अपने project के नाम से कोई directory बना कर उसके अंदर भी PHP files को store कर सकते है।
आपको समझाने के लिए उदाहरण के रूप में मैने Techpass App Master के नाम से एक directory create की है।
आप अपनी सभी PHP files को .php extension के साथ save करके ही इस folder में store करेंगे। उदाहरण के लिए मेने ऊपर create किये गए उदाहरण को First_PHP_Program.php के नाम से save किया किया है।
अब तक आपने एक PHP file create करना और उसे server directory में store करना सिख लिया है। इस file को execute करने के लिए आप सबसे पहले अपने browser की window open करेंगे।
इसके बाद आप browser window में localhost type करेंगे। जैसे ही आप enter button press करेंगे तो आपको XAMPP का welcome page show होगा इसका मतलब ये है की आपका server ठीक तरह से काम कर रहा है।
यदि आपको ये page show नहीं होता है तो या तो आपने server start नहीं किया है या फिर आपने ठीक तरह से server install नहीं किया है।
PHP file को execute करने के लिए आप localhost के आगे forward slash लगाकर उस file का नाम लिखते है। यदि आपने file को directory create करके उसमे store किया है तो localhost के बाद forward slash लगाकर पहले directory का नाम और उसके बाद forward slash लगाकर file का नाम लिखेंगे।
इसका syntax निचे दिया जा रहा है।
localhost/directoryName/fileName.php
क्योंकि मैने PHP file को directory में store किया है इसलिए में उसे directory के नाम द्वारा ही access करूँगा।
जैसे ही आप enter press करेंगे तो आपकी PHP file execute होगी। यदि आपकी coding में कोई गलती है नहीं है तो आपको सामान्य रूप से output show हो जाएगा।
Note : PHP files को browser के through इसलिए access किया जाता है क्योंकि PHP files client machine पर नहीं server पर execute होती है। हालाँकि इस case में client और server दोनों same computer ही है।
______________________________
PHP – Data Types & Variables
PHP – Data Types
- Introduction to PHP data types
- PHP Scalar data types
- PHP compound data types
Introduction to PHP Data Types
आप किस तरह की information किसी variable में store करना चाहते है ये आप PHP data types के द्वारा define करते है। जैसे की आप कोई संख्या store करना चाहते है। या फिर character store करना चाहते ये आप data types के माध्यम से compiler को दर्शाते है।
PHP में 8 तरह के data types होते है। जिनमे 4 scalar types होते है, 2 compound types होते है और 2 special types होते है।
- Scalar types
- Integer
- Float
- Boolean
- String
- Compound types
- Array
- Object
- Special types
- Resource
- NULL
Scalar Types
Scalar data types को base types भी कहा जाता है। ये data types एक programming language (PHP) by default provide करती है। इस तरह के data type एक single value को hold करते है। जैसे की int, char, float, boolean आदि।
Integer
Integers किसी संख्या को स्टोर करने के लिए यूज़ किये जाते है। Integers हमेशा पूर्ण संख्या को स्टोर करते है। आप दशमलव संख्या integer variables में स्टोर नहीं कर सकते है।
PHP में आप integer variables इस प्रकार क्रिएट कर सकते है।
<?php
// Integer type variable
$a = 1234;
?>
Float
Float types के variables दशमलव संख्या स्टोर करने के लिए यूज़ किये जाते है। PHP में आप floating point data type के variables इस प्रकार create कर सकते है।
<?php
// Float type variable
$a=3.14;
?>
Boolean
Boolean types के variables truth values store करते है। आप true और false में से कोई एक value boolean type के variables में स्टोर कर सकते है। PHP में आप boolean data types के variable इस प्रकार क्रिएट कर सकते है।
<?php
// Boolean type variable
$a = true;
?>
String
String types के variables में आप string स्टोर कर सकते है। एक स्ट्रिंग characters की sequence होती है। PHP में आप string type के variables इस प्रकार क्रिएट कर सकते है।
<?php
// String type variable
$message = "Hello Reader...";
?>
Compound Types
PHP में compound data types वो data types होते है जो programming language को यूज़ करते हुए आप अपने program में बनाते है। जैसे की arrays, objects, structure, linked-lists, queue, stack आदि।
Array
PHP में array एक map की तरह होता है। आप values को keys के द्वारा स्टोर करवाते है। इसके अलावा PHP में arrays को आप किसी list की तरह भी यूज़ कर सकते है और किसी hash-table की तरह भी यूज़ कर सकते है।
PHP में array आप इस प्रकार create कर सकते है।
Example 1
<?php
// Array declaration
$arr = array("This","is","an","array.");
?>
Example 2
<?php
// Array initialization by indexing
$arr = array(
[1] => "ONE";
[2] => "TWO";
);
?>
Objects
PHP में आप object types भी क्रिएट कर सकते है। Object types क्रिएट करने के लिए पहले आपको क्लास क्रिएट करनी होगी। PHP में क्लास और ऑब्जेक्ट क्रिएट करना किसी और प्रोग्रामिंग लैंग्वेज की तरह बिलकुल आसान है।
<?php
class myClass
{
function myFunction()
{
print "This is function of myClass";
}
}
$obj = new myClass();
$obj -> myFunction();
?>
PHP Special Types
Special type data types वो data types होते है जो किसी एक particular language के लिए ही बने होते है। PHP के special data types निचे दिए जा रहे है।
Resource
एक resource स्पेशल data type होता है जो external resources को hold करता है। जैसे की database connection या फाइल्स जो आप PHP कोड के द्वारा open करते हो।
NULL
यदि आपने कोई variable क्रिएट किया है लेकिन उसमे आप initially कोई वैल्यू स्टोर नहीं करवाना चाहते तो आप उसमे NULL स्टोर करवा सकते है। NULL का मतलब होता है की उस variable की कोई वैल्यू नहीं है।
<?php
// Assigning NULL value to variable
$a = NULL;
?>
____________________________________
PHP – Variables
- Introduction to PHP variables
- Creating PHP variables
- Displaying PHP variables
Introduction to PHP Variables
PHP और दूसरी languages में variables किसी information को store करने के लिए यूज़ किये जाते है। ये information कुछ भी हो सकती है जैसे किसी व्यक्ति का मोबाइल नंबर या उनका पता। Variables किसी भी programming language के सबसे छोटे और powerful element होते है।
जब भी आप कोई variable create करते है तो PHP उस variable के अनुसार memory में उस variable के नाम से एक space create करती है। और उसमें उस variable की value को store कर देती है। इस value को आप computer memory से variable के नाम द्वारा access कर पाते है।
Variables की values पर operations perform करने के बाद जब program exit होता है तो variables की memory automatically release हो जाती है। आप चाहे तो variables की values को permanently files में भी store कर सकते है।
Creating PHP Variables
PHP में variables create करना किसी दूसरी लैंग्वेज से different है। इसके लिए आपको निचे दी गयी बातें ध्यान में रखनी चाहिए।
- Variable के नाम से पहले $ sign लगाया जाना चाहिए।
- Variable का नाम letter या underscore से शुरू होना चाहिए।
- Variables के नाम में numbers भी use किये जा सकते है लेकिन variable का नाम number से शुरू नहीं किया जाना चाहिए।
- Variable के नाम से पहले data type define करने की आवश्यकता नहीं होती है। PHP ये automatically identify करती है। जब आप variables में value assign करवाते है तो जिस type की वैल्यू होती है variable भी उसी type का बन जाता है।
- PHP case sensitive है। यानि की num और Num दो different variables है।
- सभी variables के नाम unique होने चाहिए।
PHP में variables create करने का general syntax निचे दिया जा रहा है।
[dollarSign($)] [variableName] = value;
PHP में variables क्रिएट करने का उदाहरण निचे दिया गया है।
<?php
// String variable
$name = "Ray";
// Integer variable
$age = 30;
?>
Initializing PHP Variables
Variables को value assign करना initialization कहलाता है। ये जरुरी नहीं की आप PHP में variables को initialize करें। आप variables को create करने के बाद program में कँही भी initialize कर सकते है।
PHP में variables by default value द्वारा initialize होते है। इसका मतलब ये हुआ की यदि आप एक variable को किसी दूसरे variable को assign करते है तो उस variable की value assign किये जाने वाले variable में copy हो जाती है।
यदि आप इस प्रकार value assign करने के बाद किसी एक variable की value को change करते है तो दूसरे variable की value पर इसका कोई असर नहीं होता है। PHP में variables को value reference के द्वारा भी assign की जा सकती है।
जब value reference के द्वारा assign की जाती है तो एक variable में value change होने पर वह automatically दूसरे variable में भी change हो जाती है। ऐसा इसलिए होता है क्योंकि जब reference के द्वारा value assign की जाती है तो value के बजाय उसकी location assign की जाती है जँहा पर variable stored है।
इस प्रकार दोनों variables एक ही value को point करते है। ये एक प्रकार से अलग अलग नामों से एक ही value को access करने जैसा है।
Displaying PHP Variables
PHP variables की value display करने के लिए आपको PHP के predefined functions को यूज़ करना होता है।
जैसे की echo या print function use करके आप variable की value को display करवा सकते है। Variables की value display करवाने के लिए भी आपको variable के नाम से पहले $ sign लगाना होगा। PHP में variables की value display करने का उदाहरण नीचे दिया जा रहा है।
<?php
$name = "Ray";
// Printing variable value
echo "Name is $name";
?>
ऊपर दी गयी script निचे दिया गया output generate करती है।
Name is Ray
Scope of PHP Variables
कोई variable program के कँहा कँहा use किया जा सकता है ये उसका scope कहलाता है। PHP में variables का scope किसी दूसरी programming language से अलग होता है।
अगर दूसरी languages की बात करें तो एक बार जब आप शुरू में variables को create करते है तो बाद में उन variables को program के किसी भी function में use कर सकते है। ये variables global variables कहलाते है। लेकिन PHP के साथ ऐसा नहीं है।
यदि आप function के बाहर declare किये गए किसी variable को function के अंदर access करना चाहते है तो इसके लिए आपको उसे function के अंदर global keyword द्वारा वापस declare करना होगा। केवल तब ही आप उसे function के अंदर use कर सकते है।
उदाहरण के लिए निचे दिए program को देखिये।
<?php
$name = "Sam";
function display()
{
// Accessing global variable
global $name;
echo "<h1>$name is printed using global keyword.</h1>";
}
display();
?>
ऊपर दी गयी script निचे दिया गया output generate करती है।
Sam is printed using global keyword
ऊपर दिए गए उदाहरण में display() function के अंदर यदि बिना global keyword द्वारा $name को declare किये इस variable को access किया जाएगा तो Undefined variable error generate होगी।
इसी प्रकार आप किसी function के अंदर declare किये गए variable को function के बाहर access नहीं कर सकते है। ऐसे variables का scope local होता है। यदि किसी file को include करने से पूर्व आपने variable create किया है तो include की जाने वाली file में आप उस variable को access कर सकते है। लेकिन file किसी function में नहीं include की जानी चाहिए।
__________________________________
PHP – Constants & echo
PHP – Constants
- Introduction to PHP constants
- Syntax of PHP constants
- Example of PHP constants
Introduction to PHP Constants
PHP में variables के अलावा आप constants में भी data store कर सकते है। Constants variables की तरह ही होते है लेकिन इनकी value को change नहीं किया जा सकता है।
कई बार आपको ऐसे data को store करने की आवश्यकता होती है जिसे change नहीं किया जा सके। उदाहरण के लिए आप geometric operations perform कर रहे है जिनमें PI की value की आवश्यकता है। PI की value हमेशा 3.14 ही रहती है।
यदि आप variable के द्वारा PI की value store करते है तो यह संभव है की बाद में गलती से आपके द्वारा या किसी operation से इसकी value change हो जाये। यदि ऐसा होता है तो सभी operations wrong results produce करेंगे।
इस situation में PI को constant के रूप में define किया जा सकता है। ऐसा करने से program run होने के दौरान किसी भी प्रकार से PI की value change नहीं की जा सकती है।
Syntax of PHP Constants
PHP में constants define करने के लिए define function का प्रयोग किया जाता है। इसका general syntax निचे दिया जा रहा है।
define(“constant-name”,”constant-value”,”case-sensitive”);
|
Constant define करने के लिए define function में 3 arguments pass किये जाते है।
- constant-name – यह constant का नाम होता है। यह नाम program में unique होना चाहिए।
- constant-value – यह constant की value होती है।
- case-sensitive (optional) – यह Boolean (TRUE, FALSE) value होती है। यह एक optional argument है इसके बिना भी constant create किया जा सकता है। यदि आप चाहते है की constant non case sensitive हो तो आप इसकी value true pass करते है नहीं तो इसकी value by default false रहती है।
Constants को program में use करते समय आपको एक बात हमेशा ध्यान रखनी चाहिए की constants को dollar ($) के साथ define और प्रयोग नहीं किया जाता है।
Example of PHP Constants
PHP में constants के उपयोग को निचे उदाहरण द्वारा समझाया जा रहा है।
<html>
<head>
<title>PHP Constant Example</title>
</head>
<body>
<?php
// Defining constant
define(“PI”,3.14);
// Printing constant
echo “Value of PI is : “.PI;
?>
</body>
</html>
|
ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।
Output- Value of PI is :3.14
PHP Magic Constants
PHP आपको बहुत सारे predefined constants provide करती है। इन constants को आप किसी भी script में use कर सकते है।
इसके अलावा PHP आपको magic constants भी provide करती है। इन magic constants की value इनकी location के आधार पर change होती है। इनके बारे में निचे बताया जा रहा है।
- __LINE__ – यह constant file में current line number को बताता है।
- __FILE__ – यह constant current file का पूरा path और पूरा नाम बताता है।
- __DIR__ – यह constant current file की directory बताता है।
- __FUNCTION__ – यह constant current function का नाम बताता है।
- __CLASS__ – यह constant current class का नाम बताता है।
- __TRAIT__ – यह constant trait का नाम बताता है।
- __METHOD__ – यह constant class के method का नाम बताता है।
- __NAMESPACE__ – यह constant current namespace का नाम बताता है।
PHP magic constant का प्रयोग निचे उदाहरण द्वारा समझाया जा रहा है।
<html>
<head>
<title>PHP Magic Constant Example</title>
</head>
<body> <?php// Printing current file path using PHP magic constant.
echo __FILE__;?></body>
</html>
|
________________________________________
PHP – echo
- Introduction to PHP echo
- Syntax of PHP echo
- Examples of PHP echo
Introduction to PHP echo
PHP में echo एक language construct है। कई बार echo को function के रूप में misjudge कर लिया जाता है। लेकिन यह एक function नहीं है। यह एक special tool है जिसे PHP की core functionality में hard code किया गया है।
यदि programmer की दृष्टि से देखा जाए तो language construct भी built in functions की तरह ही काम करते है। लेकिन इनमें मुख्य difference यह होता है की PHP engine इन्हें अलग अलग तरीके से interpret करता है।
हर programming language कुछ keywords से मिलकर बनी होती है। ये keywords उस programming language के parser या compiler के द्वारा परिचित होते है। उदाहरण के लिए if, else आदि keywords है और PHP parser यह जानता है की ये keywords किस सन्दर्भ में और किस प्रकार प्रयोग किये जाते है। इनका पूरा syntax PHP parser के पास stored रहता है।
मान लीजिये किसी PHP file को parse किया जा रहा है और PHP parser को if keyword मिलता है तो PHP parser को पहले से ही पता है की इस keyword के बाद opening bracket आता है। लेकिन यदि script में ऐसा नहीं है तो PHP parser error show कर देता है।
इस situation में if एक language constructor है क्योंकि PHP parser को पहले से ही पता है की ये क्या है और कैसे काम करता है। इसके लिए उसे if की definition check करने की आवश्यकता नहीं होती है। उसी प्रकार echo के बारे में भी PHP parser को पूर्ण जानकारी होती है।
लेकिन built in functions के case में ऐसा नहीं होता है। जब भी PHP parser को कोई built in function मिलता है तो वह language definition में उसके format को match करता है। Parser check करता है की इसे सही प्रकार declare किया गया है या नहीं।
इसके बाद parser function को उन language constructs में convert करता है जिनकी जानकारी उसे है और इसके बाद ही function execute होता है। यह एक लंबी process होती है।
इसे आप इस प्रकार समझ सकते है की जैसे आप किसी article को एक तो Hindi भाषा में पढ़े और एक जर्मन भाषा में पढ़े। जब आप Hindi भाषा में किसी article को पढ़ेंगे तो आपको बार बार dictionary देखने की और grammar rules check करने की आवश्यकता नहीं होती है।
लेकिन जब आप जर्मन भाषा में उस article को पढ़ेंगे तो आपको बार बार dictionary में हर word का मतलब देखना होगा क्योंकि आप जर्मन भाषा से परिचित नहीं है।
इसी प्रकार PHP parser के लिए language constructs mother tongue की तरह होते है जिन्हें समझने के लिए उन्हें किसी प्रकार की processing करने की आवश्यकता नहीं होती है।
लेकिन PHP parser के लिए functions किसी दूसरी भाषा की तरह होते है जिससे वह परिचित नहीं है और इसके लिए उसे बार बार language definition check करनी होती है।
क्योंकि language constructs के लिए किसी प्रकार की processing नहीं होती है इसलिए language constructs functions से faster होते है। यही reason है की इन्हें functions से अधिक use किया जाता है।
इसके अलावा functions return value भी set करते है इसलिए उनकी processing और भी slow हो जाती है। लेकिन PHP echo function नहीं है इसलिए ये कोई value भी return नहीं करता है। इसलिए इसकी processing function से अधिक होती है।
PHP के opening और closing tags के अंदर आप कोई भी HTML tags define नहीं कर सकते है। ऐसा करने पर error generate होती है। लेकिन PHP echo द्वारा आप HTML को भी render करके display करवा सकते है। इसके लिए आप HTML tags को string के रूप में echo के अंदर quotes में define करते है।
Syntax of PHP echo
PHP echo का general syntax निचे दिया जा रहा है।
echo (“string $arg1 [, string $…]”);
|
जैसा की आप ऊपर दिए गए उदाहरण में देख सकते है echo में आप string और argument/variable दोनों एक साथ double quotes में pass कर सकते है। Echo द्वारा एक से अधिक arguments को process किया जा सकता है।
क्योंकि echo एक function नहीं इसलिए इसे आप बिना paranthesis के भी इसे use कर सकते है।
echo “string $arg”;
|
यदि आप echo के अंदर define की गयी string में single quote use करना चाहते है तो आपको echo के साथ double quotes use करने की आवश्यकता होती है। आप चाहे तो echo के साथ single quotes भी use कर सकते है।
echo ‘string $arg’;
|
यदि आप किसी variable की value print कर रहे तो इसके लिए आपको quotes में उसे define करने की आवश्यकता नहीं होती है। आप echo के बाद सीधे ही dollar sign के साथ varaible का नाम लिख सकते है।
echo $variableName;
|
यदि आप PHP code में HTML tags को use करना चाहते है तो इसके लिए आप HTML को echo के साथ quotes में define कर सकते है।
echo “<tagName>text</tagName>”;
|
Example of PHP echo
PHP echo को use करने का एक simple उदाहरण निचे दिया जा रहा है।
<?php
$name = “Techpass App Master Apps”;
// Printing HTML and PHP variable using echo.
echo “<h2>I Like $name</h2>”;
?>
|
ऊपर दिए गए उदाहरण में echo द्वारा string के साथ साथ variable और HTML को भी display करवाया गया है। यह उदाहरण निचे दिया गया output generate करता है।
Output- I Like Techpass App Master Apps
_________________________________
PHP – Operators & Control Statements
PHP – Operators
- Introduction to PHP operators
- Different PHP operators
Introduction to Operators
Variables के साथ operations जैसे की addition, division, comparison आदि perform करने के लिए operators का प्रयोग किया जाता है। जब आप किसी variable के साथ operator को यूज़ करते है तो वो variable operand कहलाता है। Operators 2 तरह के होते है।
- Unary
- Binary
Unary operators एक single variable के साथ यूज़ किये जाते है। इस तरह के operators या तो variable के आगे लगते है या पीछे लगते है। इस तरह के operators को variable के साथ यूज़ करने से variables का मतलब बदल जाता है।
Binary operators 2 variables के बीच में यूज़ किये जाते है जैसे की ( + ) addition। इस तरह के operators दोनों variables की value change करके तीसरी नयी वैल्यू produce करते है।
Arithmetic Operators
Arithmetic operators वेरिएबल्स के साथ arithmetic operations perform करने के लिए यूज़ किये जाते है। कुछ common arithmetic operators की list निचे दी जा रही है।
Operator
|
Explanation
|
Example
|
Negation(-) unary
|
Opposite
of variable
|
-$a
|
Addition (+)
|
Addition
of 2 variables
|
$a+$b
|
Subtraction(-)
|
Subtraction
of 2 variables
|
$a-$b
|
Multiplication(*)
|
Multiplication
of 2 variables
|
$a*$b
|
Division(/)
|
Division
of 2 variables
|
$a/$b
|
Modulus
|
Remain
from division
|
$a%$b
|
Exponentiation
|
First
variable raise to power to second variable
|
$a**$b
|
Assignment Operators
Assignment operator एक variable को value assign करने के लिए यूज़ किया जाता है। (=) assignment operator होता है। ये value आप 2 प्रकार से assign कर सकते है।
- Direct
- By reference
पहले तरीके में आप वैल्यू को directly assign करते है। Direct value assign करना बिलकुल simple है। आप एक variable create करते है और (=) assignment operator लगा कर उसके आगे value लिख देते है।
Ex. $a = 4;
Ex. $n = “yourName”;
दूसरे तरीके में आप एक variable को दूसरा variable assign करते है। ऐसा करने से right side के variable की value left side के variable को assign हो जाती है। इसे assigning by reference कहते है।
Ex. $a = 4;
$b = $a; // b contains 4
Bitwise Operators
Bitwise operators bits पर operation perform करते है। कोई भी decimal, memory level पर bits में convert होता है। यदि आप bits पर operation perform करना चाहते है तो bitwise operators यूज़ कर सकते है।
Operator
|
Explanation
|
Example
|
AND (&)
|
Common
bits from both variables are set
|
$a&$b
|
OR (|)
|
All
bits of both variables are set
|
$a|$b
|
X-OR(^)
|
Bits
which are in $a but not in $b are set
|
$a^$b
|
NOT(~)
|
All the
bit becomes opposite
|
~$a
|
Shift Left(<<)
|
Shift
$a bits $b times to left
|
$a<<$b
|
Shift Right(>>)
|
Shift
$a bits $b times to right
|
$>>$b
|
Comparison Operators
ये operators दो variables की values को compare करने के लिए यूज़ किये जाते है।Comparison operators का प्रयोग ज्यादातर control statements में किया जाता है। इनका result TRUE या FALSE होता है।
Operator
|
Explanation
|
Example
|
Equal (==)
|
Returns
true if both operands are equal
|
$a==$b;
|
Not Equal (!=)
|
Returns
true if both operands are not equal
|
$a!=$b;
|
Less than (<)
|
Returns
true if first variable less than second variable
|
$a<$b;
|
Greater than (>)
|
Returns
true if first variable greater than second variable
|
$a>$b;
|
Less than equal to (<=)
|
Returns
true if first variable is less than or equal to second variable.
|
$a<=$b;
|
Greater than equal to (>=)
|
Returns
true if first variable is greater than or equals to second variable.
|
$a>=$b;
|
Error Control Operators
PHP में एक error control operator यूज़ किया जाता है। ये operator होता है (@)। जब आप यह operator किसी statement के साथ यूज़ करते है तो उस statement से generate होने वाली errors को ignore कर दिया जाता है। ये operator ज्यादातर file handling में यूज़ किया जाता है।
Execution Operators
PHP में एक execution operator यूज़ किया जाता है। ये operator होता है (“)। Execution operator के माध्यम से आप कोई भी shell command PHP code के द्वारा execute कर सकते है।
<?php
$a=`yourCommand`;
echo $a;
?>
Increment / Decrements Operators
Increment and decrements operators के द्वारा आप किसी वेरिएबल की value को increment और decrement कर सकते है। ये operators (++) और (–) होते है। ऐसे variables 2 तरह से यूज़ किये जा सकते है।
- Post increment/decrements – इस तरह के यूज़ में operators variable के पीछे लगाये जाते है। ये variable को print करने के बाद में उसकी value को 1 नंबर से increase या decrease करते है।
Example. $a++;
- Pre increment/decrements – इस तरह के यूज़ में operators variable के पहले लगाये जाते है। ये variable को print करने से पहले increase या decrease करते है।
Example. ++$a
Logical Operators
Logical operators logic perform करने के लिए यूज़ किये जाते है। ये operators control statements में भी यूज़ किये जाते है।
Operator
|
Explanation
|
Example
|
And(&&)
|
True if
both variables are true.
|
$a&&$b
|
Or(||)
|
True if
either one variable is true.
|
$a||$b
|
Not(!)
|
True if
variable is not true.
|
!$a
|
Xor
|
True if
only one is true not both.
|
$a Xor
$b
|
String Operators
PHP में 2 तरह के string operators यूज़ किये जाते है।
- Dot (.) = ये concatenation operator होता है। ये एक string variable को दूसरे string variable के साथ जोड़ता है और तीसरा string variable बनाता है।
Ex.
$a=”bitu”;
$b=”sharma”;
$c = $a.$b;
- Dot equals (.=) = ये operator अपने right side की string को left side के variable में add करता है।
Ex .
$a=”Harry”;
$a.=”sharma”;
Array Operators
PHP कुछ ऐसे array operators भी provide करती है जो सिर्फ arrays पर apply होते है।
Operator
|
Explanation
|
Example
|
Union (+)
|
Add to
arrays.
|
$a+$b
|
Equality (==)
|
True if
both arrays have same values.
|
$a==$b
|
Identity (===)
|
True if
both arrays have same values of same type and in same order.
|
$a===$b
|
Inequality(!=)
|
True if
both arrays are not equal.
|
$a!=$b
|
Type Operators
PHP में एक type operator भी यूज़ किया जाता है। ये operator ये decide करने के लिए यूज़ किया जाता है की कोई object किसी particular क्लास का है या नहीं। PHP का type operator instanceof होता है।
Example
$ output = $a instanceof yrClass;
__________________________________
PHP – Control Statements
- Introduction to PHP control statements
- Different PHP control statements
Introduction to Control Statements
PHP Control statement आपके program के execution को control करते है। Control statements program में logic build करने के लिए यूज़ किये जाते है।
Logic के द्वारा आप decide करते है कोनसे statement आप execute करना चाहते है और कोनसे statements को आप छोड़ना चाहते है। या कौनसे statements को आप एक से ज्यादा बार execute करना चाहते है। आप चाहे तो किसी particular condition में किसी statement को execute होने से रोक सकते है या उसे एक से ज्यादा बार execute कर सकते है।
Control statements आपको program पर पूरा control provide करते है। यानि की ये आप decide करते है की program का execution कैसे होगा। PHP में control statements बाकी सब programming languages की तरह ही है साथ ही कुछ special statements भी PHP में include किये गए है।
Control statements 4 तरह के होते है –
- Selection statements
- Looping statements
- Jump statements
- Special PHP statements
Type
|
Statements
|
Explanation
|
Selection statements
|
If,
if-else, switch
|
Select
and execute statements based on logic.
|
Looping statements
|
While,
do-while, for, for each
|
Executes
given number of statements specified number of times.
|
Jump statements
|
Continue,
break and go-to
|
Start
execution from a particular position in program.
|
Special PHP statements
|
Declare,
return, require and include
|
These
are special PHP control statements
|
आइये इन सभी control statements के बारे में जानने का प्रयास करते है।
If Statement
If statement में आप condition देते है, यदि condition true है तो if statement के बाद वाले block में दिए हुए statement execute हो जाते है। और यदि condition false है तो compiler brackets को skip कर देता है और statements execute नहीं होते है।
Structure
|
Example
|
if(condition)
{
// If
condition true, statement of here will be executed
}
|
If(3<5)
{
// Execute statement here because condition is true
}
|
If else Statement
If else statement में यदि condition true है तो if के बाद वाला brackets का block execute होगा नहीं तो else के बाद वाला brackets का block execute होगा।
Structure
|
Example
|
if(condition)
{
// If
condition true, statement of here will be executed
}
Else
{
//If
condition false, statement of here will be executed
}
|
If(3>5)
{
// Can’t execute because condition is false
}
Else
{
// Execute statements here because condition is false
}
|
While Loop
While एक looping statement है। ये block में दिए हुए statements को तब तक execute करता है जब तक condition true रहती है। Condition के false होने पर while statement का execution रुक जाता है।
Loop को control करने के लिए आप एक condition देते है उस condition की value हर loop के साथ change होती है और तब तक change होती है जब तक की condition false न हो जाये। यदि आप condition variable को increase नहीं करेंगे तो condition कभी false नहीं होगी और loop का execution अनन्त समय तक चलता रहेगा।
Structure
|
Example
|
While(Condition)
{
// Statements you want to execute when condition is true
// Change condition
here (increase condition variable)
}
|
$i = 1;
While($i<5)
{
// Statements you want to execute
i++; // Increasing condition variable so that loop can terminate after
finite number of loops.
}
|
Do-While Loop
do while loop while loop की तरह ही होता है बस इसमें statements पहले execute हो जाते है और condition बाद में check होती है। Do while loop के statements शुरू में एक बार जरूर execute होते है चाहे condition true हो या false। ऐसा इसलिए होता है क्योंकि do while loop में condition बाद में check होती है।
Structure
|
Example
|
Do
{
// Execute
these statements
// Increase
condition variable
}while(Condition)
|
$i=1;
Do
{
// Execute statements of here, will be executed once for sure.
// Change condition
}while($i<5)
|
For Loop
For loop एक बहुत ही simple loop होता है। इस loop में आप loop को control करने वाले variables को initialize करते है, condition लिखते है और variable को increment करते है और ये सब एक ही जगह होता है।
Structure
|
Example
|
for(initialization;
condition; increment)
{
// Statement to execute when condition is true
}
|
for($i=1;
$i<5; $i++)
{
// Statements to execute
}
|
For Each Loop
यह loop विशेषकर arrays को iterate करने के लिए बनाया गया है। ये लूप सिर्फ arrays के साथ काम करता है। यदि आप इसे variables के साथ यूज़ करते है तो ये error शो करता है। For each loop के साथ आप array elements पर आसानी से operation perform कर सकते है।
Structure
|
Example
|
foreach($arr_name as
$value)
{
// Do
something with array elements
}
|
$arr_name
= array(2,4,6,8);
foreach($arr_name
as $value)
{
echo $value;
}
|
Continue and Break Statement
Continue statement किसी loop की iteration को skip करने के लिए यूज़ किया जाता है। जैसे की यदि आप loop में कोई ऐसी condition आये जँहा पर आप कोई भी task नहीं perform करना चाहते है तो इसके लिए आप continue statement का प्रयोग करते है।
Structure
|
Example
|
for(initialization;
condition; increment)
If(condition)
{
continue;
}
|
for($i=1;
$i<5; $i++)
{
if($i=3)
{
continue; // It will skip 3rd iteration
}
}
|
Break statement statements की sequence को break करने के लिए यूज़ किया जाता है। जैसे की यदि एक certain condition के बाद आप loop को execute नहीं कराना चाहते है तो break लगाने से loop terminate हो जायेगा। ऊपर दिए हुए उदाहरण में यदि आप continue के जगह break लगाएंगे तो 3rd iteration में ही loop terminate हो जायेगा।
Switch Statement
Switch statement if की तरह ही होता है। Switch में cases होते है। आप एक choice देते है। Switch के जिस case से choice match करती है वही execute हो जाता है। जब कोई भी case match नहीं होता है तो default case execute हो जाता है।
Structure
|
Example
|
switch(choice)
{
case1:
// Statement to execute if
choice is 1
break;
case2:
// Statement
to execute if choice is 2
break;
default:
// Statement to execute when no case matches
}
|
Switch(2)
{
case 1:
// This will not be executed
Break;
case 2:
print “Second will be executed”
Break;
Default:
// This will not be executed because case matched.
}
|
Declare
Declare statement के द्वारा आप किसी block of code के execution को निर्देशित कर सकते है।
Structure
|
Example
|
declare(directive) //
ticks or encoding
{
// Statement to
apply a directive
}
|
declare(ticks=1)
{
// You may call a function for all ticks
}
|
Return Statement
ये statement ज्यादातर functions के साथ यूज़ किया जाता है। जब return statement execute होता है तो execution function से return हो जाता है। Return statement के माध्यम से आप function से कोई value या variable भी return कर सकते है।
Include Statement
इस statement के द्वारा आप किसी दूसरी php file को अपनी किसी file में include कर सकते है। जिस जगह पर आप उस file को include करेंगे उसी जगह उसका content शो होगा। जिस file का नाम आपने दिया है यदि वो exist नहीं करती है तो compiler warning show करता है लेकिन execution नहीं रुकता है।
Structure
|
Example
|
<?php
Include file_Name.php;
?>
|
<?php
echo
“lets include another file”;
include
other_File.php;
?>
|
Require Statement
ये statement include की तरह ही होता है लेकिन इसमें यदि file exist नहीं करती है तो program का execution रुक जाता है।
Goto Statement
Goto statement program में किसी दूसरे section में जाने के लिए प्रयोग किया जाता है।
Structure
|
Example
|
<?php
// Statements
goto a;
// Statements
a:
// Statements
?>
|
<?php
goto a;
echo “
skip this “;
a:
echo
“print this first ”;
?>
|
______________________________
PHP – Functions & Constructor
PHP – Functions
- Introduction to PHP functions
- Types of PHP functions
- User-defined PHP functions
Introduction to PHP Functions
यदि आपके program में कोई ऐसा task है जिसे आपको अलग अलग जगह execute करने की जरुरत है तो आप उस task के लिए program में कई जगह पर code लिखने की बजाय PHP function create कर सकते है।
और जब भी आपको उस task को perform करने की आवश्यकता हो तो आप उस function को अलग अलग जगह पर call कर सकते है। ऐसा करने से आपका time भी बचता और computer की memory भी बचती है साथ ही आपका program भी short और readable बन जाता है। इसे code re-usability कहा जाता है। क्योंकि आप एक ही code को अलग अलग जगह पर यूज़ करते है।
No Need to Write Code Again & Again
यदि में आपसे पुछुं की किसी PHP program में echo या print function की जरुरत कितनी बार पड़ती है। तो आप कहेंगे की बहुत बार पड़ सकती है।
अब मान लीजिये यदि इन function का पूरा code आपको हर बार लिखना पड़े तो program कितना बड़ा हो जायेगा और कोई भी simple project पूरा करने में बहुत time लग जायेगा। ऐसी situations से बचने के लिए programs में functions यूज़ किये जाते है। Functions किसी भी program के basic structure होते है। C की तरह ही PHP भी एक modular language है। Function create करने के लिए आप function कीवर्ड यूज़ करते है।
PHP functions के बारे में सबसे unique बात ये है की आप किसी function में दूसरा function तो create कर ही सकते है साथ ही एक class भी create कर सकते है। Anonymous functions भी PHP में introduce किये गए है। ये java की तरह ही है। ये बिना नाम के functions होते है। ऐसे functions ज्यादातर किसी function को पहले क्रिएट करने के बजाय साथ की साथ बनाने और यूज़ करने के लिए उपयोग किये जाते है।
Types of Functions
PHP में 3 तरह के functions यूज़ किये जाते है, इनके बारे में निचे समझाया जा रहा है।
User Defined Functions
वे functions जो programmer द्वारा बनाये जाते है user defined functions कहलाते है। PHP में function क्रिएट करने के लिए आपको function keyword यूज़ करना होता है।
Structure
function func_Name(arg1, arg2, …. argn)
{
// Perform any task here
}
Example
function myAddFunction($a, $b)
{
$result = $a+$b;
echo "Addition is :" $result);
}
PHP में function create करने के लिए सबसे पहले आप function keyword लिखते है। इसके बाद function का नाम लिखा जाता है।
Function Arguments
Function के नाम के बाद brackets में arguments पास किये जाते है। Arguments वो values होती है जो function को call करवाते समय पास की जाती है। जैसे की यदि आपका PHP function 2 numbers के addition का है तो आप add करने के लिए 2 numbers pass कर सकते है।
ताकि function इन पर addition का operation perform कर सके। Arguments के बाद curly brackets में कुछ statements होते है जिन्हें आप execute करवाना चाहते है। जैसे की हमारे example में आप arguments में पास किये गए numbers को add करवा सकते है।
Function Call
PHP में function call करने के लिए आप सिर्फ function का नाम लिखते है और उसके बाद brackets में required arguments पास करते है। PHP functions को आप कई प्रकार से call कर सकते है। जैसे की echo statement में भी आप PHP functions को call कर सकते है। ऊपर दिए गए function को आप इस प्रकार call कर सकते है।
<?php
// Defining function
function myAddFunction($a, $b)
{
return $a + $b;
}
// Calling function with arguments
$result = myAddFunction(3,5);
echo "<h2>Addition is $result</h2>";
?>
ऊपर दी गयी script निचे दिया गया output generate करती है।
Output - Addition is 8
PHP में function क्रिएट करते समय आप चाहे तो default argument भी दे सकते है। Function call के दौरान जब user कोई argument नहीं पास करे तो default argument यूज़ किया जाता है।
Default arguments
किसी भी PHP function को default argument देने के लिए आप उसके आगे assignment operator (=) लगा के default argument लिख देते है।
<?php
// Defining default argument
function hello($userName = "Sir/Mam")
{
echo "<h2>Welcome $userName</h2>";
}
hello();
?>
ऊपर दिए गए उदाहरण में यदि यूजर खुद का नाम नहीं पास करता है तो Welcome के बाद Sir/Mam शो होगा। ये उदाहरण निचे दिया गया output generate करता है।
Output- Welcome Sir/Mam
Built-in Functions
PHP programmers के लिए कुछ built in functions provide करती है। ये functions programmer का काम और भी आसान कर देते है। कुछ common built-in functions की list निचे दी जा रही है।
Functions
|
Examination
|
__construct()
|
Constructor for classes.
|
mysql_connect()
|
Connects
to MySQL database.
|
date()
|
Prints
current date.
|
count()
|
Used to
count number of character in strings.
|
file_exists()
|
Checks
whether a file exists.
|
Anonymous Functions
PHP में नए तरीके के functions add किये गए है जिन्हे anonymous function कहते है। इन functions की सिर्फ body होती है, इनका कोई नाम नहीं होता है। इन functions को किसी भी expression के बीच में यूज़ कर सकते है। इसके लिए function keyword use करते है।
<?php
// Defining anonymous function
$var = function($name)
{
echo "<h2>Hello $name</h2>";
};
// Calling anonymous function
$var("Reader");
?>
ऊपर दी गयी script निचे दिया गया output generate करती है।
Output- Hello Reader
________________________________
PHP – Constructor & Destructor
- Introduction to PHP constructor
- PHP constructor with parameters
- PHP destructors
Introduction to PHP Constructor
Constructor एक function होता है जो class के अंदर declare किया जाता है। जब भी class का कोई नया object create किया जाता है तो class constructor को automatically call करती है।
Constructor उन situations के लिए महत्वपूर्ण होता है जब object को use करने से पहले आप कोई initialization perform करना चाहते है जैसे की class member variables को values assign करना, database से connection establish करना आदि।
PHP 5 आपको special constructor function declare करने की सुविधा प्रदान करती है। PHP के पुराने version (PHP 4) में constructor class के नाम जैसा function create करके declare किया जाता था। लेकिन PHP 5 आपको इसके लिए special function __construct() provide करती है।
PHP 5 को पुराने version से compatible बनाने के लिए ये छूट दी गयी है की आप दोनों में से किसी भी तरीके से constructor declare कर सकते है। जब भी कोई object create होगा तो PHP सबसे पहले __construct() function को search करके execute करने का प्रयास करेगी।
यदि PHP को __construct function नहीं मिलता है तो वह class के नाम वाले function को ढूंढेगी और उसे execute करने का प्रयास करेगी।
Syntax of PHP Constructor Function
PHP में __construct() function declare करने का general syntax निचे दिया जा रहा है।
function __construct($args-list)
{
// Statements to be executed when objects are created.
}
|
किसी भी normal function की तरह constructor function declare करने के लिए आप function keyword use करते है। इसके बाद double underscore लगाकर __construct() function का नाम लिखते है। Constructor function में आप arguments भी pass कर सकते है।
Examples of PHP Constructor
PHP में constructor create करने का simple उदाहरण निचे दिया जा रहा है।
<?php
class myClass
{
// Defining constructor function
function __construct()
{
// Display message when object gets created.
echo “Object is created…”;
}
}
// Creating object
$myclass = new myClass;
?>
|
ऊपर दिए गए उदाहरण में जैसे ही myClass का object create किया जाता है तो constructor function call होता है और Object is created… message display होता है।
जैसा की मैने आपको बताया किसी भी normal function की तरह constructor function में भी arguments pass किये जा सकते है। इसका उदाहरण निचे दिया जा रहा है।
<?php
class myClass
{
// Defining argument to constructor function
function __construct($num)
{
echo “Object number $num is created”;
}
}
// Passing argument to constructor function while creating object.
$myclass = new myClass(5);
?>
|
ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।
Output- Object number 5 is created
PHP Constructor and Inheritance
यदि child class में constructor function define किया गया है तो parent class का constructor नहीं call किया जाएगा। यदि आप parent constructor को भी call करना चाहते है तो इसके लिए आपको child class के constructor में parent class के constructor को इस प्रकार call करना होगा।
parent::__construct()
|
यदि child class कोई constructor define नहीं करती है तो implicitly parent class का constructor call किया जाता है। लेकिन ऐसा तब ही होता है जब parent class में constructor को private नहीं declare किया गया हो।
Introduction to PHP Destructors
Destructor एक special function होता है। जिस प्रकार constructor function object create करने पर call होता है उसी प्रकार destructor function object destroy होने पर call होता है।
कुछ programming languages में objects को manually destroy करना होता है, लेकिन PHP में यह काम garbage collector द्वारा किया जाता है। जैसे ही कोई object free होता है और उसकी अतिरिक्त आवश्यकता नहीं होती है तो garbage collector उसे destroy कर देता है।
Syntax of PHP Destructor
PHP में destructor __destruct() function द्वारा declare किया जाता है। इसका general syntax निचे दिया जा रहा है।
function __destruct()
{
// Statements to be executed when object gets destroyed.
}
|
Constructor function की ही तरह destructor function भी double underscore function द्वारा define किया जाता है।
Example of PHP Destructor
PHP destructor function का उदाहरण निचे दिया जा रहा है।
<?php
class myClass
{
// Constructor function
function __construct()
{
echo “Object is created…<br />”;
}
// Destructor function
function __destruct()
{
// Display message when object gets destroyed.
echo “Object is destroyed”;
}
}
$myclass = new myClass;
?>
|
ऊपर दिए उदाहरण में object create होने पर object is created message और object destroy होने पर object is destroyed message display होगा। यह उदाहरण निचे दिया गया output generate करता है।
Output- Object is created…
Object is destroyed
_______________________________
PHP – Strings & Namespaces
PHP – Strings
- Introduction to PHP strings
- Creating PHP strings
- PHP string functions
Introduction to PHP Strings
जब बहुत से characters को एक साथ sequence में लिखा जाता है तो वे एक string का निर्माण करते है। उदाहरण के लिए India एक string है। ये i,n,d,i,a characters से मिलकर बनी है। एक string निचे दिए गए elements से मिलकर बनी हो सकती है।
- Letters – एक string small letters (a – z) या capital letters (A – Z) से मिलकर बनी हो सकती है।
- Numbers – एक string में zero से लेकर 9 तक का कोई भी number हो सकता है।
- Special Characters – एक string में special characters (@#$%^&!) भी हो सकते है। Special characters को string के रूप में display करने के लिए escape sequence characters use किये जाते है।
Web pages और databases में strings का बहुत अधिक महत्व होता है। इसलिए ये जरुरी है की एक scripting language आपको strings के साथ work करने की capability provide करे।
PHP आपको strings के साथ work करने के लिए support provide करती है। PHP में strings create करना और उनके साथ work करना बहुत ही आसान है। साथ ही PHP आपको strings के साथ work करने के लिए कुछ built in functions भी provide करती है। आइये अब देखते है की आप किस प्रकार PHP में strings create कर सकते है।
Creating PHP Strings
PHP में strings आप किसी normal variable की तरह ही create करते है। इसका general syntax निचे दिया जा रहा है।
$string-name = “string”;
|
आप चाहे तो string को double quotes में ना लिखकर single quotes में भी लिख सकते है।
$string-name = ‘string’;
|
दोनों ही तरीकों में पहले आप $ symbol के साथ string variable define करते है और उसके बाद assignment operator लगाकर string लिखते है। निचे इसे एक उदाहरण के द्वारा समझाया गया है।
// Creating string variable
$MyString = “Hello Reader!”;
|
ऊपर दिए गए उदाहरण में MyString नाम से एक string create की गयी है।
Displaying PHP Strings
PHP में strings को display करने के लिए echo statement को सबसे अधिक use किया जाता है। इसका general syntax निचे दिया जा रहा है।
echo “$string-name”;
|
उदाहरण के लिए आप ऊपर create की गयी string को display करवाना चाहते है तो इसके लिए आप echo statement इस प्रकार लिखेंगे।
// Printing string variable
echo “$MyString”;
|
जैसा की मैने आपको पहले बताया कुछ characters को string के रूप में display करने के लिए आपको escape sequence characters की आवश्यकता होती है। कुछ escape sequence charachters की list निचे दी जा रही है।
- n – ये new line character होता है, इससे pointer दूसरी line में चल जाता है।
- r – ये carriage return character होता है, इससे pointer line के शुरू में चल जाता है।
- t – इसके द्वारा आप string में tab space देते है।
- $ – String में $ sign को include करने के लिए आप इस character को use करते है।
- ” – String में double quotes show करने के लिए आप इस character का इस्तेमाल करते है। इससे सिर्फ एक ही double quote show होगा।
- – String में black slash character show करने के लिए आप इस escape sequence character को use कर सकते है।
Escape sequence characters के use को निचे एक उदाहरण के माध्यम से समझाया जा रहा है।
<?php
// Printing double quotes in string
$MyString = “Techpass “App” Master”;
echo “$MyString”;
?>
|
ऊपर दिए गए उदाहरण में जब MyString नाम से create की गयी string display होगी तो Hindi को double quotes में display किया जाएगा। यह script निचे दिया गया output generate करेगी।
Output- Techpass App Master
PHP String Functions
PHP आपको लगभग 100 built in string functions provide करती है। इनमें से कुछ functions को example द्वारा निचे समझाया जा रहा है।
strlen()
इस function द्वारा आप किसी भी string की length प्राप्त कर सकते है। Argument के रूप में आप इस string में एक constant string या string variable pass करते है। ये function आपको string में जितने characters है उतनी ही संख्या return करता है। इस संख्या में spaces भी शामिल होते है।
इसे निचे उदाहरण के माध्यम से समझाया गया है।
<?php
$MyString = “Techpass App Master”;
// Displaying length of string
echo strlen($MyString);
?>
|
ऊपर दी गयी script निचे दिया गया output generate करती है।
18
|
str_word_count()
इस function द्वारा आप किसी भी string में words की संख्या count कर सकते है। Argument के रूप में आप एक string variable या constant string pass करते है। ये function आपको उस string में words की संख्या return करता है। इसे निचे उदाहरण द्वारा समझाया जा रहा है।
<?php
$MyString = “Techpass App Master”;
// Counting words of string
echo str_word_count($MyString);
?>
|
ऊपर दी गयी script निचे दिया गया output generate करती है।
3
|
str_replace()
इस function के द्वारा आप किसी string में से कोई word remove करके उस string को show कर सकते है। Argument के रूप में आप इस function में string variable या constant string और वह word जिसे आप string से remove करना चाहते है pass करते है। इसे निचे उदाहरण के द्वारा समझाया जा रहा है।
<?php
$MyString = “Techpass App Master”;
// Replacing string
echo str_replace(“App”,$MyString);
?>
|
ऊपर दिए उदाहरण में MyString string से Hindi word को remove किया गया है। ये script निचे दिया गया output generate करती है।
Techpass Master
|
strrev()
इस function के द्वारा आप किसी भी string को reverse order में display करवा सकते है। Argument के रूप में आप वह string pass करते है जिसे आप reverse order में display करवाना चाहते है। इसे निचे उदाहरण के द्वारा समझाया जा रहा है।
<?php
$MyString = “Techpass”;
// Reversing order of string
echo strrev($MyString);
?>
|
ऊपर दी गयी script निचे दिया गया output generate करती है।
ssaphceT
|
______________________________________
PHP – Namespaces
- Introduction to PHP namespaces
- Defining PHP namespaces
- Using PHP namespaces
Introduction to PHP Namespaces
PHP में namespaces एक तरह के elements को एक साथ bind करने के लिए यूज़ किये जाते है। साथ ही ये same नाम की classes एक ही project में यूज़ करने के लिए भी उपयोगी है।
जैसा की आपको पता है की आप एक ही नाम की classes अपने project में यूज़ नहीं कर सकते है।यदि आप ऐसा करेंगे तो errors शो होगी। लेकिन यदि आप इन same नाम की classes को अलग अलग namespaces में रख दे तो आप इस problem से बच सकते है।
एक PHP namespace में आप classes, interfaces और functions store करवा सकते है। ये सभी classes, interfaces और functions के नाम वाली classes, interfaces और functions यदि किसी दूसरे namespace में भी है तो आपके project में कोई error नहीं आएगी। क्योंकि दोनों अलग अलग namespaces में है।
Defining Namespaces
PHP में namespaces को declare करने के लिए namespace कीवर्ड यूज़ किया जाता है। जब भी आप किसी file में namespace declare करे तो आपको namespace declaration file में सबसे ऊपर करना चाहिए।
<?php
namespace myNamespace;
// Interfaces
// Classes
// Functions
?>
जिन classes को आप एक namespace में डालना चाहते है आपको उन सबको एक ही file में रखना जरुरी नहीं है। आप जिस भी file के content को उस namespace में डालना चाहते है उस file में टॉप पर आप वापस namespace को declare कर सकते है। ऐसा करने से उस file का content भी उसी namespace में आ जायेगा।
Sub Namespaces
किसी sub file directory की तरह ही आप PHP में sub namespaces भी क्रिएट कर सकते है।
<?php
namespace myNamespace/mySubNamespace;
// Interfaces
// Classes
// Functions
?>
Using PHP Namespaces
किसी भी classes, interfaces और functions को namespace के नाम द्वारा access कर सकते है। इसके लिए आप पहले namespace का नाम लिखते है उसके बाद (/) लगा के जो class, interface या function आप यूज़ करना चाहते है उसका नाम लिखते है।
file1.php
<?php
// Declaring namespace
namespace myNamespace;
class myClass
{
public function display()
{
echo "<h2>This is from another namespace</h2>";
}
}
?>
file2.php
<?php
namespace newNamespace;
// Including other file
require "file1.php";
// Accessing function of another namespace
$obj = new myNamespacemyClass;
$obj->display();
?>
ऊपर दिए गए उदाहरण में file1.php file में create किये गए myNamespace namespace की myClass का object file2.php file में create किया गया है। और उसके display function() को access किया गया है। इन दोनों namespaces को एक ही file में भी define किया जा सकता है।
Output- This is from another namespace
_________________________________
PHP – Classes & Abstract Classes
PHP – Classes
- Introduction to PHP classes
- Syntax of PHP classes
- Example creating PHP classes
Introduction to PHP Classes
PHP में class एक object oriented programming feature है। इसके द्वारा आप किसी data और उस पर perform होने वाले operations को एक साथ bind करते है। एक class में data properties (variables) के द्वारा represent किया जाता है। Data पर perform होने वाले operations class में function द्वारा represent किये जाते है।
Bind Data and Behaviour Together
Class c language में structure की तरह होती है। जिस प्रकार c language में structure के अंदर अलग अलग data types के variables create करके उनका एक compound type create किया जाता है। और बाद में उस type के variables create किये जाते है। उसी प्रकार एक PHP class में भी आप अलग अलग data types के variables create कर सकते है।
फर्क सिर्फ इतना होता है की class में आप variables के आलावा functions भी define कर सकते है। इस प्रकार class भी एक type होती है, जिसके variables objects कहलाते है।
Class is a Blueprint
Class को आप एक घर के नक़्शे की तरह समझ सकते है। इस नक़्शे के आधार पर कई घर (objects) बनाये जा सकते है जिनमें अलग अलग families (information/data) रह सकती है।
आसान शब्दों में कहा जाए तो class एक blueprint होता और object उसका real representation होता है। Object में ही data store होता है। Class से data secure हो जाता है। Class data को secure करने के लिए PHP आपको visibility mechanism provide करती है। Visibility के द्वारा आप data के access को control कर सकते है।
Data Separation
Class create करके आप एक तरह के data को दूसरी तरह के data से separate कर सकते है।
उदाहरण के लिए आप किसी company के marketing department का data और उससे related operations एक class द्वारा represent कर सकते है। और sales department का data और उससे related operations दूसरी PHP class द्वारा represent कर सकते है।
Data और उससे सम्बंधित functions को अलग अलग classes में define करने से program की complexity कम हो जाती है। एक class programmer को data centred approach provide करती है। Classes में पूरा focus data पर होता है जो की object oriented programming का एक feature है।
Creating PHP Classes
PHP में class आप class keyword को use करते हुए define करते है। इसका general syntax निचे दिया जा रहा है।
classKeyword className
{
// Properties & Functions here.....
}
PHP में class create करने का simple उदाहरण निचे दिया जा रहा है।
<?php
class Employee
{
// Property declaration
public $name = "Fred";
// Function declaration
public function dispFunction()
{
return $this->name;
}
}
?>
जैसा की मैने आपको पहले बताया class किसी नक़्शे की तरह होती है और जिसके आधार पर कितने भी घर बनाये जा सकते है जिनमें अलग अलग families रह सकती है।
ऊपर दिए गए उदाहरण में एक Employee class create की गयी है। इस class के आप कितने भी objects create कर सकते है। हाँलाँकि इस उदाहरण में पहले से ही property की value define कर दी गयी है लेकिन constructors के माध्यम से आप हर object की properties को अलग अलग values pass कर सकते है। इसके बारे में और अधिक आप आगे आने वाली tutorials जानेंगे।
Class Objects
जिस प्रकार Integer, float और character आदि data types होते है, उसी प्रकार PHP में class भी एक type होती है। फर्क सिर्फ इतना होता है की class एक user defined type होती जो user स्वयं अपनी आवश्यकता के अनुसार define करता है।
Access Class Elements
Class type के भी variables create किये जाते है। Class type के variables को objects कहा जाता है। इन objects के द्वारा आप class में define की गयी अलग properties में data store करवाते है।
उदाहरण के लिए आप ऊपर create की गयी Employee class के 10 objects create कर सकते है। और उन objects द्वारा 10 employees की information जैसे की उनका नाम आदि store और access कर सकते है।
जब भी आप कोई object create करते है तो PHP आपको class में define की गयी properties के अनुसार memory में space provide करती है। इस space में information आप object के नाम द्वारा store और access करते है। इस प्रकार आप अलग अलग objects द्वारा अलग अलग information store कर पाते है।
Syntax of Creating Objects
Object को normal variables की तरह declare नहीं किया जाता है। Objects declare करने के लिए new keyword use किया जाता है।
$objectName = new className;
Objects create करते समय constructor को argument भी pass किये जा सकते है। ऐसी situation में आप object declare करने के लिए निचे दिया गया syntax use कर सकते है।
$objectName = new className(arg-list);
Example
<?php
class Employee
{
// Property declaration
public $name = "Fred";
// Function declaration
public function dispFunction()
{
return $this->name;
}
}
// Creating object
$obj = new Employee();
echo "<h2>Name</h2>".$obj->dispFunction()."<h2>is printed using class object.</h2>";
?>
ऊपर दिए गए उदाहरण में Employee class create की गयी है। जैसा की आप देख सकते है इस class के अंदर एक property और एक function declare किया गया है। Property को value पहले से ही assign की गयी है।
Class properties को values आप manually assign ना करते हुए object create करते समय भी pass कर सकते है। ऐसा constructors द्वारा किया जाता है जिसके बारे में आप आगे की tutorials में जानेंगे। Function के अंदर property को return किया जा रहा है। Function में $this keyword का use किया गया है।
यह keyword current PHP class के object की तरह use किया जाता है। Class members को सिर्फ class objects द्वारा access किया जा सकता है। इसलिए $this यँहा पर Employee class के object की तरह use किया गया है। इस keyword से $name property को access किया गया है। आखिर में class के बाहर Employee class का object create किया गया है और dispFunction() को call किया गया है।
Output- Name
Fred
is printed using class object
___________________________________
PHP – Abstract Classes
- Introduction to PHP abstract classes
- Syntax of PHP abstract classes
- Example of PHP abstract classes
Introduction to PHP Abstract Classes
PHP5 में abstract classes और methods को introduce किया गया है। Abstract classes और methods object oriented programming के abstraction feature को implement करने के लिए use की जाती है।
Abstraction एक process होती है जिसमे programmer unnecessary data को hide कर लेता है और केवल वही data show करता है जो end user के सन्दर्भ में है। इससे program की complexity कम हो जाती है और efficiency (use करने में आसानी) बढ़ती है।
आइये अब abstract methods और classes के बारे में जानने का प्रयास करते है।
Abstract Methods
Abstract methods वे methods होते है –
- जिनकी कोई definition (method body) नहीं होती है।
- जिनके सिर्फ नाम और parameters declare किये जाते है।
- जिन्हें abstract keyword के साथ declare किया जाता है।
Abstract methods को सिर्फ abstract classes में define किया जाता है। इनकी body वह class provide करती है जो abstract class को inherit करती है।
Abstract Classes
Abstract class वह class होती है जिसमें एक या एक से अधिक abstract methods होते है और जिसे abstract keyword द्वारा define किया जाता है। निचे abstract classes की कुछ महत्पूर्ण charcterstics दी जा रही है जो इन्हें दूसरी classes से अलग बनाती है।
- किसी abstract class का object नहीं create किया जा सकता है। ये ऐसी classes होती है जो सिर्फ inherit करने के लिए बनायीं जाती है। Abstract classes create करने का मुख्य कारण एक ऐसी template create करना है जो inherit करने वाली class को abstract methods को implement करने के लिए force कर सके।
- यदि किसी class में एक या एक से अधिक abstract method है तो यह अनिवार्य है की वह class भी abstract हो।
- एक abstract class में abstract methods के आलावा normal properties और methods भी declare किये जा सकते है।
- यह अनिवार्य है की abstract class को inherit करने वाली class abstract class में define किये गए सभी abstract methods की body define करे।
- Abstract class को inherit करने वाली class abstract classes और abstract methods को उसी visibility के साथ या फिर उससे कम secure visibility के साथ declare कर सकती है।
- Abstract class को inherit करने वाली class और abstract class के methods का signature पूर्ण रूप से match होना चाहिए।
एक बात आपको हमेशा ध्यान रखनी चाहिए की abstract class multiple inheritance को support नहीं करती है। हालाँकि एक abstract class दूसरी abstract class को inherit कर सकती है लेकिन abstract class को inherit करने वाली normal class किसी दूसरी class द्वारा inherit नहीं की जानी चाहिए।
Syntax of PHP Abstract Classes
PHP में abstract classes define करने का general syntax निचे दिया जा रहा है।
<?php
[abstractKeyword] [classKeyword] [className]
{
Normal Properties…..
Normal Methods(with body)…….
[abstractKeyword] [visibilityMode] [functionKeyword] [functionName](arg-list);
}
?>
|
जैसा की आप ऊपर दिए गए syntax में देख सकते है class को abstract keyword के साथ define किया जाता है। Abstract method को argument list के बाद ही semicolon से end कर दिया जाता है उसकी body नहीं लिखी जाती है।
Example
निचे PHP में abstract classes को implement करने का simple उदाहरण दिया जा रहा है।
<?php
// Parent abstract class
abstract class myAbstractClass
{
// Declaring normal function
public function baseFunction()
{
echo “This is base function. <br />”;
}
// Declaring abstract function
abstract public function childFunction();
}
// Normal Child class
class myNormalClass extends myAbstractClass
{
// Providing definition of abstract function in child class of abstract class
public function childFunction()
{
echo “This is child function.”;
}
}
// Child class object
$obj = new myNormalClass;
// Calling normal function of myAbstractClass
$obj->baseFunction();
// Calling abstract function of myAbstractClass
$obj->childFunction();
?>
|
ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।
Output- This is base function
This is child function
_____________________________________
PHP – Inheritance & Interfaces
PHP – Inheritance
- Introduction to PHP inheritance
- Types of PHP inheritance
- Syntax of PHP inheritance
Introduction to PHP Inheritance
Inheritance एक object oriented programming feature है। जिस प्रकार एक संतान अपने माता पिता की सम्पति को inherit (विरासत के रूप में प्राप्त) करती है, उसी प्रकार एक class किसी दूसरी class की properties और functions को inherit कर सकती है।
जिस class को inherit किया जाता है वह parent या base class कहलाती है। Inherit करने वाली class child या sub class कहलाती है। Child class parent class के properties और functions को use करने के आलावा खुद के भी properties और functions declare कर सकती है।
Inheritance को use करने के कई benefits है। Inheritance का सबसे महत्वपूर्ण benefit ये है की base class में declare की गयी किसी भी property या function को आप child class में use कर सकते है। इससे आपका समय तो बचता ही है साथ ही program में code भी कम हो जाता है जिससे program की complexity reduce हो जाती है।
Base class के members को access करने के लिए child class को उन्हें वापस declare या define करने की आवश्यकता नहीं होती है। जिस प्रकार कोई normal class अपनी properties और functions को access करती है, उसी प्रकार एक child class base class के members को access कर सकती है।
कोई भी child class किसी parent class के केवल public और protected members को ही access कर सकती है।
Types of Inheritance
Inheritance के अलग अलग types के बारे में निचे बताया जा रहा है।
- Single Inheritance – इस तरह के inheritance में एक class को दूसरी class inherit करती है। इसे normal inheritance भी कहा जाता है।
- Multilevel Inheritance – इस तरह के inheritance में inherit करने वाली class को भी inherit किया जाता है। उदाहरण एक लिए यदि कोई class B है जो class A को inherit करती है, और कोई class C है जो class B को inherit करती है तो ऐसा inheritance multilevel inheritance कहलाता है।
- Multiple Inheritance – इस तरह के inheritance में एक class एक से अधिक classes को inherit करती है। इस प्रकार का inheritance PHP में allow नहीं है।
PHP में multiple inheritance Interfaces द्वारा implement किया जा सकता है। एक class एक से अधिक interfaces को implement कर सकती है।
Syntax of PHP Inheritance
PHP में किसी भी class को inherit करने का general syntax निचे दिया जा रहा है।
class parentClass
{
// Properties & functions of parent class
}
class childClass extends parentClass
{
// Can use public & propertected properties & functions of parent class
// Child class own properties & functions
}
|
PHP में जब कोई class किसी दूसरी class को inherit करती है तो ऐसा वह extends keyword द्वारा करती है। जैसा की आप ऊपर दिए गए syntax में देख सकते है childClass extends keyword द्वारा parentClass को inherit कर रही है।
Example
PHP में single inheritance को implement करने का simple उदाहरण निचे दिया जा रहा है।
<?php
// Parent class
class myClass
{
public $name = “Inherited Variable”;
}
// Child class
class myOtherClass extends myClass
{
public function displayName()
{
// Accessing parent class property
echo $name;
}
}
?>
|
ऊपर दिए गए उदाहरण में myOtherClass द्वारा myClass को extend किया गया है। क्योंकि myOtherClass myClass को extend कर रही है इसलिए myOtherClass में myClass की public property $name को access करके print किया गया है। यह उदाहरण निचे दिया गया output generate करता है।
Output- Inherited variable
Overriding
कई बार ऐसा हो सकता है की जो methods आपने parent class में define किये है उनकी definition आप child class में change करना चाहते है।
ऐसा कई कारणों से हो सकता है जैसे की यदि किसी method की definition child class के according नहीं है तो आप उसे child class के अनुसार redefine कर सकते है। इस प्रकार वह method child class के लिए अलग तरीके से behave करेगा।
जब parent class के किसी method को child class में change या redefine किया है तो वह process overriding कहलाती है।
Parent class के method को child class में override करने के लिए उसे child class में same signature के साथ दुबारा define किया जाता है। Same signature से यँहा मतलब same visibility mode, same method name और same argument list से है।
इसके बाद आप definition को child class के according change करके लिख सकते है। PHP में overriding को निचे उदाहरण द्वारा समझाया जा रहा है।
<?php
// Parent class
class parentClass
{
public function display()
{
echo “This is parent class display.<br />”;
}
}
// Child class
class childClass extends parentClass
{
// Overriding parent class function
public function display()
{
echo “This child class display”;
}
}
// Parent class object
$pObj = new parentClass();
// Parent class display() function
$pObj->display();
// Child class object
$cObj = new childClass();
// Child class display() function
$cObj->display();
?>
|
ऊपर दिए गए उदाहरण में parentClass के display function को childClass में override किया गया है। आप जिस class के object द्वारा display() function को call करना करेंगे उसी class का display() version call हो जाएगा। ये उदाहरण निचे दिया गया output generate करता है।
Output- This is parent class display
This child class display
यदि आप childClass में parent class के function को call करना चाहते है तो इसके लिए आपको parent:: keyword use करना होगा। मान लीजिये आप parentClass के display function को base class में call करना चाहते है तो ऐसा आप इस प्रकार कर सकते है।
parent::display();
|
parent:: के द्वारा आप parent class के methods को access करते है।
Final
जब आप चाहते है की base class के किसी method को override नहीं किया जा सके तो आप use final keyword के साथ declare करते है। Final keyword compiler को बताता है की यह method की final definition है और इसमें अब कोई change नहीं किया जा सकता है।
इसका general syntax निचे दिया जा रहा है।
finalKeyword visibilityMode functionKeyword functionName(arg-list);
|
मान लीजिये आप ऊपर दिए गए display function को parentClass में final declare करना चाहते है तो ऐसा आप इस प्रकार कर सकते है।
final public function display();
|
________________________________________
PHP – Interfaces
- Introduction to PHP interfaces
- Syntax of PHP interfaces
- Example of PHP interfaces
Introduction to PHP Interfaces
एक interface द्वारा आप ऐसे methods की list specify कर सकते है जिन्हें एक class द्वारा implement करना आवश्यक होता है। Interface में define किये गए methods की कोई body नहीं होती है।
Interface को implement करने वाली class द्वारा सभी methods की body provide किया जाना आवश्यक होता है। ऐसा नहीं होने पर PHP द्वारा error throw की जाती है।
Interfaces classes पर एक certain structure को force करते है। ये किसी template की तरह होते है जो classes को certain functionality के लिए bound करते है।
उदाहरण के लिए आप अपनी company के अलग अलग departments के लिए अलग अलग classes create कर रहे है। लेकिन आप ये चाहते है की एक method displayDepaName() जो departement का नाम display करता है वो सब class अनिवार्य रूप से define करे।
इस situation में आप एक interface define कर सकते है और उस interface में इस method को define कर सकते है। इसके बाद सभी classes द्वारा इस interface को implement करवा सकते है।
इस interface को implement करने वाली classes के लिए ये आवश्यक हो जाएगा की वो displayDepaName() नाम का method declare करे। इसके अलावा आप और भी methods define कर सकते है जो आप चाहते है की सभी classes अनिवार्य रूप से declare करे।
एक interface में सभी methods by default abstract होते है। यदि देखा जाए तो interfaces किसी abstract class की तरह लगते है। लेकिन ऐसा बिलकुल भी नहीं है। निचे abstract classes और interfaces के बीच कुछ differences बताये जा रहे है।
- एक abstract class में ये जरुरी नहीं की सभी methods abstract हो, आप normal methods भी define कर सकते है। लेकिन एक interface में सभी methods abstract होते है।
- Abstract classes में आप methods public और protected visibility के साथ define कर सकते है। लेकिन interfaces में सभी methods public visibility mode के साथ ही define होते है।
- Abstract class सिर्फ single और multilevel interface allow करती है। लेकिन interfaces द्वारा PHP में multiple interface implement किया जा सकता है।
- किसी abstract class को inherit करते समय आप किसी method को override कर सकते है। लेकिन interfaces को implement करने के बाद उसमे define किये गए सभी methods को override करते है।
Interfaces को implement करने की बजाय आप उसे किसी abstract class की तरह extend भी कर सकते है। इसके लिए आप extends keyword use करते है।
जब आप सिर्फ abstract methods subclasses को provide करना चाहते है तो आपको interface declare करना चाहिए और जब आप abstract methods के साथ साथ regular methods भी sub classes को provide करना चाहते है तो आपको abstract classes use करनी चाहिए।
Syntax of PHP Interfaces
PHP में interfaces को interface keyword के साथ declare किया जाता है। इसका general syntax निचे दिया जा रहा है।
interfaceKeyword interfaceName
{
// Define functions without body
publicMode functionKeyword functionName1(arg-list);
publicMode functionKeyword functionName2(arg-list);
….
….
publicMode functionKeyword functionNameN(arg-list);
}
|
Interface के अंदर आपको किसी भी method को abstract define करने की आवश्यकता नहीं होती है, क्योंकि interface के सभी methods by default abstract होते है।
Example
निचे PHP में interface implement करना एक simple उदाहरण द्वारा समझाया जा रहा है।
<?php
// Declaring interface
interface compInterface
{
public function dispDepaName();
}
// Class implementing interface
class salesDepa implements compInterface
{
// Providing definition for interface method
public function dispDepaName()
{
echo “Sale Department<br />”;
}
// Other local methods
}
// Other class implementing interface
class MarketingDepa implements compInterface
{
// Providing different definition for interface method
public function dispDepaName()
{
echo “Marketing Department”;
}
}
// Classes objects
$obj1 = new salesDepa;
$obj2 = new MarketingDepa;
// Calling interface methods
$obj1->dispDepaName();
$obj2->dispDepaName();
?>
|
ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।
Output- Sale Department
Marketing Department
___________________________________
PHP – Sessions & Cookies
PHP – Sessions
- Introduction to PHP sessions
- Creating PHP sessions
- Example of PHP sessions
Introduction to PHP Sessions
PHP sessions का बहुत ही सिंपल concept होता है। उदाहरण के लिए आप किसी cyber cafe पर web surfing के लिए जाते है। वँहा पर आपको बताया जाता है की 1 घंटे के लिए आपको 20 रुपए देने होंगे।
आप owner को OK कहकर net surf करना शुरू कर देते है। जब आप net surf करना शुरू करते है तब आपका session start हो जाता है। 1 घंटे तक आपका session running state में होता है। 1 घंटे बाद owner आपको बताता है की 1 घंटा पूरा हो चूका है।
जैसे ही एक घंटा पूरा होता है आपका session expire हो जाता है। और आप पैसे देकर चले जाते है। इसी प्रकार जब आप अपने email account में login करते है तो आपका session start हो जाता है और जैसे ही आप logout करते है तो आपका session expire हो जाता है। लेकिन sessions का उपयोग इतना ही नहीं होता है। कई बार किसी यूज़र के session को time से भी control किया जाता है।
उदाहरण के लिए आप किसी government एग्जाम का form भर रहे है। यदि आप उस form को आधा भरकर कुछ देर के लिए छोड़ दे, तो आपको एक message show होगा की आपका session expired हो चूका है। यदि आप form को एक time limit में नहीं भरते है, तो आपका session expire हो जाता है और आपको शुरू से form भरना पड़ता है। यँहा पर PHP sessions को website का लोड कम करने के लिए यूज़ किया गया है।
Data Throughout All Pages
Sessions का एक उपयोग और होता है। जब यूजर आपकी website की surfing करता है तो एक page से दूसरे page पर जाता है। Sessions के माध्यम से data अलग अलग pages पर available हो जाता है। जो data आप चाहते है उसे सारे pages पर पा सकते है।
उदाहरण के लिए जब आप online shopping करते है और आपको कोई item पसंद आता है तो आप उसे cart में add कर लेते है और किसी दूसरे item को देखने लग जाते है। ऐसे आपको जो जो पसंद आता जाता है आप उसे cart में add करते जाते है। अंत में जब आप payment करते है तो सभी items आपको एक जगह पर show हो जाते है ऐसा sessions के द्वारा ही किया जाता है।
Cookies Starts Old Sessions
आपके session को दुबारा start करने के लिए cookies का इस्तेमाल किया जाता है। Cookies में आपकी session ID होती है। जब आप किसी site पर जाते है, तो वो website आपके computer पर cookies के रूप में session ID store कर देती है।
जब आप उसी website पर दुबारा जाते है, तो cookies में से उसी ID के साथ session शुरू किया जाता है। यदि session ID stored नहीं है, तो नया session शुरू किया जाता है और उसकी ID cookies के रूप में store कर दी जाती है।
Creating PHP Sessions
PHP में sessions के साथ काम करना बहुत ही आसान है। इसके लिए PHP कुछ predefined variables और methods provide करती है।
session_start()
session_start() function session को start करने के लिए यूज़ किया जाता है। ये function पहले check करता है की कँही कोई session पहले से start तो नहीं है। यदि कोई session नहीं होता है तो ये function नया session start करता है। आपको इस मेथड को अपनी PHP script में सबसे पहले call करना चाहिए।
Setting Session Variables
एक बार जब session क्रिएट हो जाये तो आप उसमे variables क्रिएट कर सकते है। सभी वेरिएबल्स $_SESSION[] array में automatically store होते है। इस array में आप कोई भी value उसके नाम के साथ store कर सकते है।
$_SESSION['variable'] = "value";
Session variable set करने का उदाहरण निचे दिया जा रहा है।
$_SESSION['address']="India";
अब जब तक आपका session start रहेगा तब तक ये value आपके page पर available रहेगी। चाहे आप कितनी भी बार page को refresh कर ले या किसी दूसरे पेज पर जाकर वापस आ जाये। जब session destroy होगा तब ही ये value destroy होगी।
Destroying PHP Sessions
ज्यादातर session तब destroy होता है, जब आप browser की window को close कर देते है। तब session भी साथ ही destroy हो जाता है। आप चाहे तो sessions को manually भी destroy कर सकते है। इसके लिए PHP आपको session_destroy() और unset() methods provide करती है।
session_destroy()
session_destroy() मेथड के द्वारा आप session को destroy कर सकते है। ये function session के साथ session variables को भी destroy कर देता है। इस function को PHP script के सबसे आखिर में call करना चाहिए।
session_destroy();
unset()
यदि आप किसी एक session variable को ही destroy करना चाहते है तो इसके लिए भी PHP एक function provide करती है। unset() function के द्वारा आप किसी भी session variable को destroy कर सकते है। इसके लिए आप unset() function को call करते है और उसमे session variable पास करते है।
unset($_SESSION['Address']);
Example of PHP Sessions
sessionExample.php
<html>
<body>
<h1>No matter how many pages you visit your information will be available.</h1> <?php// Starting session
session_start();// Setting session variable Name
$_SESSION['Name']="Max";// Setting session variable Country
$_SESSION['Country'] = "India";?><a href="file1.php">Visit this page</a>
</body>
</html>
file1.php
<html>
<body> <?phpsession_start();?><h1>This is a different page but your information will be available through sessions.</h1><?phpecho "User Name".$_SESSION['Name'];
echo "User Country".$_SESSION['Country'];?></body>
</html>
ऊपर दिए गए उदाहरण में example.php file में session को start किया गया है। इसके बाद session variables के द्वारा कुछ information store की गयी है। इस page से file1.php file की link दी गयी है। जब आप इस file पर जाते है तो वँहा भी session start किया गया है।
क्योंकि existing session पिछले page का चल रहा है इसलिए नया session create नहीं होगा। इसके बाद पिछले page में जो session information store की गयी थी उसे print करवाया गया है।
Output:- No matter how many pages you visit your information will be available
Next Output:- This is a different page but your information will be available through sessions
User Name : Max
User Country : India
_____________________________________
PHP – Cookies
- Introduction to PHP cookies
- Creating PHP cookies
- Deleting PHP cookies
Introduction to PHP Cookies
PHP में cookies पुराने यूज़र को identify करने के लिए यूज़ की जाती है। Cookie एक छोटी सी file होती है। जब भी आप किसी website को visit करते है तो वह website आपके PC पर cookies को store कर देती है।
जब कुछ दिनों बाद आप वापस उसी website के लिए browser में request करते है तो request के साथ उस website की cookies भी send की जाती है। Cookies के माध्यम से उस website को पता चल जाता है की आप पहले भी website visit कर चुके है।
Types of Cookies
Cookies mainly 2 तरह की होती है।
- Analytical cookies – इस तरह की cookies के माध्यम से आप किसी पुराने visitor को recognize कर सकते है तो और visitors की संख्या का भी पता लगा सकते है।
- Functional cookies – इस तरह की cookies functional tasks perform करने के लिए यूज़ की जाती है जैसे की user की पुरानी choices आप इस तरह की cookies के द्वारा पता लगा सकते है।
Need of Cookies
Website के द्वारा cookies को कई कारणों से यूज़ किया जाता है।
- User experience को improve करने के लिए।
- User को identify करने के लिए।
- Number of visitors count करने के लिए।
- User की पुरानी choices याद रखने के लिए।
Functions for Cookies
PHP में cookies को कुछ functions की मदद से manage किया जाता है।
- setcookie() – ये function cookies create करने के लिए यूज़ किया जाता है।
- isset() – इस function के माध्यम से आप check कर सकते है की cookie पहले से created है या नहीं।
Creating Cookies
PHP में cookies create करने के लिए आप setcookie() function यूज़ करते है। इस function के 6 parameters होते है।
<?php
setcookie(name-of-cookie,value,expire-time,path,domain-name,security);
?>
- name-of-cookie – ये cookie का नाम होता है। ये एक unique नाम होता है। इसी नाम के द्वारा cookie की value access की जाती है।
- value – ये वह value होती है जिसे आप store करना चाहते है। ये value कुछ भी हो सकती है जैसे की कोई string या integer value आदि।
- expire-time – ये वो time होता है जब तक के लिए आप cookie को store करना चाहते है।
- path – ये उस directory का path होता है जँहा पर आप cookie को store करना चाहते है।
- domain-name – ये domain का नाम होता है। यदि आपकी website बड़ी है तो आप उसका नाम यँहा यूज़ कर सकते है।
- security – यदि आप इसको 1 पर set करते है तो cookie केवल secure HTTPS के माध्यम से ही भेजी जा सकती है।
PHP में cookies create करने का उदाहरण निचे दिया जा रहा है।
<?php
// Setting cookie name & country
setcookie("name","yrName",time()+3600,"/cookies/","",0);
setcookie("Country","India",time()+3600,"/cookies/","",0);
?>
Accessing Cookies
PHP में cookies $_COOKIE[ ] array में store की जाती है। यदि आप किसी एक cookie की value को access करना चाहते है तो $_COOKIE[‘ cookie-name’] variable के द्वारा access कर सकते है।
जैसे की यदि कोई पुराना user वापस आता है तो आप उसके नाम के साथ welcome message print करवा सकते है। एक ध्यान देने योग्य बात ये है की cookies को access करने से पहले आपको पता कर लेना चाहिए की यूज़र की request में कोई cookie send की गयी है या नहीं।
इसके लिए आप isset() function यूज़ करते है। पिछले उदाहरण में आपने cookie create की थी अब cookie को access करने का उदाहरण निचे दिया जा रहा है।
<?php
if(isset($_COOKIE["name"]))
{
// Accessing cookie
echo "Good morning".$_COOKIE["name"];
}
?>
Deleting Cookies
Cookies को इस्तेमाल करने के बाद आप cookies को delete भी कर सकते है। इसके लिए 2 तरीके है।
- setcookie() function को सिर्फ cookie के नाम के साथ call कीजिये।
- setcookie() function को past की किसी date के साथ call कीजिये।
<?php
setcookie("name"); // Here name is the name of cookie.
?>
Example
PHP में cookies create और use करने का simple उदाहरण निचे दिया जा रहा है।
<html>
<head>
<title>PHP Cookie Demo</title>
</head>
<body>
<form action="file1.php" method="post">
Enter Your Name : <input type="text" name="UserName"> <br />
<br />
<input type="submit" value="Submit">
</form>
</body>
</html>
file1.php
<?php
// Getting data from form
$username = $_POST["UserName"];
// Checking whether cookie is already set
if (isset($_COOKIE["name"]) && $_COOKIE["name"] == $username)
{
// Showing welcome back if cookie was already set
echo "Welcome back " . $_COOKIE["name"];
}
else
{
echo "Hello " . $username . "<br />";
echo "Cookie was not set. <br />";
echo "Setting cookie now.....<br />";
// Setting cookie for first timer
setcookie("name", $username, time() + 3600, "/", "", 0);
echo "Cookie set.";
}
?>
ऊपर दिए गए उदाहरण में एक form create किया गया है। इस form के माध्यम से user से उसका नाम enter करवाया जाता है। Form को submit करने पर file1.php file execute की जाती है।
जब user पहली बार visit कर रहा है तो उसके नाम से cookie पहले से set नहीं होगी। इसलिए user को message show किया जाएगा की cookie पहले से set नहीं थी और अब set कर दी गयी है।जब user वापस return करता है तो user को उसके नाम के साथ welcome back message show किया जाएगा।
OutPut - Enter You Name : TAM-Reader
Submit
Next Output Hello TAM-Reader
Cookie was not set
Setting cookie now...
Cookie set
Next Output Welcome Back TAM-Raeder
________________________________
PHP – File Handling
- Introduction to PHP file handling
- PHP file handling operations
Introduction to PHP File Handling
ज्यादातर सभी languages files के साथ काम कर सकती है। PHP भी ऐसे कुछ functions provide करती है, जिनकी मदद से आप easily files को handle कर सकते है।
Files को handle करने की ये ability PHP को top scripting languages के group में शामिल करती है। सभी web applications database यूज़ नहीं करती है, इसलिए simple data को text files में store करना एक अच्छा विकल्प है। PHP में files के साथ आप 6 तरह के operations perform कर सकते है।
- Opening a file
- Reading a file
- Writing to a file
- Appending to a file
- Closing a file
- Deleting a file
इन सभी operations को PHP में कैसे perform किया जाता है, आइये देखते है।
Opening a File
Files को open करने के लिए PHP fopen() मेथड provide करती है। इस method में 2 argument pass किये जाते है।
पहला argument file का नाम होता है, और दूसरा आप उसे किस mode में open करना चाहते है वह mode होता है। PHP file को कई modes में open कर सकते है। ये सभी modes एक character के द्वारा represent किये जाते है। ये सभी modes अलग अलग तरह से operations परफॉर्म करने के लिए यूज़ किये जाते है।
Modes
|
Explanation
|
r
|
इस mode से files को सिर्फ read किया जा सकता है।
|
r+
|
इस mode से file को read भी किया जा सकता है और उस file में PHP द्वारा write भी किया जा सकता है।
|
w
|
इस mode से files में सिर्फ write किया जा सकता है। यदि file पहले से नहीं है तो create हो जाती है।
|
w+
|
इस mode से file के साथ read/write दोनों operation perform किये जा सकते
है।
|
a
|
ये mode पुरानी file के content को delete नहीं करता बल्कि उसी में नए contentको add कर देता है।
|
a+
|
इस mode में आप file को read भी कर सकते है और उसमे data append भी कर सकते है।
|
x
|
इस mode से आप file में data write कर सकते है। नयी file क्रिएट की जाती है। लेकिन यदि file पहले से है तो error शो होगी।
|
x+
|
इस mode से आप read और write दोनों कर सकते है।
|
File के साथ किसी भी तरह का operation perform करने के लिए सबसे पहले file को open करना जरुरी होता है।
<?php
$myFile=fopen("myFile.txt", "w");
?>
इस उदाहरण में हमने PHP code द्वारा file को open किया है। अब आप इस file के साथ जिस mode में आपने इसे open किया है। उसके according कोई भी operation perform कर सकते है। यदि myFile नाम की कोई file पहले से नहीं होगी तो इस नाम से file create हो जायेगी।
Reading From a File
File को read करने के लिए आपको 2 functions यूज़ करने पड़ते है। इसके लिए आप fread() function यूज़ करते है। इस function में आप file stream के साथ उसकी length पास करते है।
PHP file stream fopen() method द्वारा return की जाती है। fread() method file के content को string रूप में return करता है लेकिन उसे print करने के लिए आपको echo statement को use करना होगा।
File की length इसलिए pass की जाती है, ताकि compiler को पता चले की file में content कँहा तक है। इसके लिए आप filesize() मेथड यूज़ करते है। यह method file की size integer के रूप में return करता है। इस method में आप file का नाम pass करते है।
<?php
// Opening file stream
$bhtFile=fopen("BHT.txt", "r");
// Getting file size
$size=filesize("BHT.txt");
// Reading & storing file data
$filedata=fread($bhtFile, $size);
// Printing file data
echo $filedata;
// Closing file stream
fclose($bhtFile);
?>
ऊपर दिया गया उदाहरण निचे दिया गया output generate करता है।
Writing to a File
किसी भी file में data store करने के लिए आप PHP का fwrite() function यूज़ करते है। इस function में file stream और data pass किया जाता है। File stream fopen() method द्वारा return की जाती है।
<?php
// Opening file stream
$myFile=fopen("myFile.txt", "w");
//Writing to file
fwrite($myFile, "This website is cool");
?>
ऊपर दिया गया उदाहरण current directory में myFile नाम की text file open करता है और उसमे This website is cool text store करता है। यदि file पहले से create नहीं की गयी है तो नयी file create की जाती है।
Appending to a File
जब भी आप किसी existing file में data write करते है, तो उस file का previous data delete हो जाता है। लेकिन यदि आप चाहे तो previous data को delete ना करते हुए उसी के end में new data को add कर सकते है। इसके लिए आपको file को a mode के साथ open करना होता है और उसमे PHP fwrite() function के द्वारा data आप write करते है।
<?php
$myFile=fopen("myFile.txt", "a");
// Appending to file
fwrite($myFile, "and I am also cool.");
?>
ऊपर दिया गया उदाहरण myFile.txt file के existing content के आगे pass की गयी string and I am also cool को add करता है।
Closing a File
Operations complete होने के बाद आपको file stream close कर देनी चाहिए। इसके लिए PHP fclose() method provide करती है। इस method में file pointer pass किया जाता है।
<?php
$myFile=fopen("filename.txt", "w");
// Do some operations here
fclose($myFile);
?>
Deleting a File
काम पूरा होने के बाद यदि आप file को delete करना चाहते है तो इसके लिए PHP unlink method provide करती है।
<?php
$myFile=fopen("filename.txt", "a");
// Do some operations here
unlink($myFile);
?>
PHP file handling के द्वारा आप dynamically files को handle कर सकते है।
____________________________________
PHP – Database Handling (MySQL)
- Introduction to PHP database handling
- PHP functions for database handling
- Example of PHP database handling
Introduction to PHP Database Handling
PHP आपको सभी प्रकार के databases को handle करने के लिए support provide करती है। अपनी web application का data किसी database में save करना सबसे appropriate रहता है। Database में आपका data secure रहता है। किसी भी database में data normalized form में रहता है।
Database चाहे कितना ही बड़ा क्यों ना हो उसमे आप data को बहुत fast access कर सकते है। PHP के साथ सबसे ज्यादा यूज़ होने वाला database MySQL है। MySQL एक open source database है। ये सभी को freely available है। MySQL दुनिया का सबसे ज्यादा यूज़ होने वाला database है।
MySQL data के साथ operations perform करने के लिए SQL (Structured Query Language) यूज़ करता है।
MySQL के साथ काम करने के लिए PHP आपको कुछ predefined function provide करती है। जिन्हे यूज़ करके आप आसानी से MySQL के साथ काम कर सकते है।
mysql_connect()
ये PHP function mysql database से connect करने के लिए यूज़ किया जाता है। Database के साथ काम करने के लिए सबसे पहले आप इसी function को call करेंगे। इस method में तीन arguments पास किये जाते है।
पहला argument server का port no होता है। दूसरा argument database का user name होता है और तीसरा argument database का password होता है। इस function की value एक variable में store की जाती है। ये function database का pointer return करता है।
<?php
$con = mysql_connect("server port no","username","password");
?>
mysql_select_db()
जिस MySQL database के साथ आप काम करना चाहते है, वो database आप इस PHP function के द्वारा select करते है। इस function में 2 argument pass किये जाते है। पहला argument database का नाम होता है। और दूसरा argument database connection variable होता है।
ये method कुछ भी return नहीं करता है।
<?php
mysql_select_db("database-name",$con);
?>
mysql_query()
इस PHP function में आप mysql की query pass करते है। ये function उस query को database के against execute करता है। और उस query का result एक array के रूप में return करता है। इसलिए आप जब इस function को call करते है तो एक variable में इसकी value store करवाते है।
जैसे आप mysql में query लिखते है वैसे ही इस function में भी लिखते है। Query आप inverted commas के अंदर लिखते है।
<?php
$query = mysql_query("show tables");
?>
mysql_result()
ये PHP function MySQL query का result शो करता है। इस function को आप echo statement में यूज़ कर सकते है। ये function एक बार में एक ही element शो कर सकता है। इस function में 3 argument pass किये जाते है।
पहला argument वह variable होता है जिसमे आप अपनी query का result store कर सकते है। दूसरे argument की तरह जिस row से आप data शो करना चाहते है वो row number पास करते है। तीसरे argument की तरह जिस column का data आप show करना चाहते है उसका नाम pass करते है।
<?php
echo "salary is :".mysql_result($query,1,"Tables");
?>
ऊपर दिए गए उदाहरण में 2nd row के salary column का data शो किया गया है।
mysql_fetch_array()
एक एक value शो करने की बजाय आप एक साथ भी अपनी query के result की एक पूरी row show कर सकते है। इसके लिए आप mysql_fetch_array() function यूज़ करते है। इस PHP function में MySQL query variable argument की तरह pass किया जाता है। इस function को while loop के साथ यूज़ करके अपनी query का पूरा result आप show कर सकते है।
<?php
$query = mysql_query("select name from employees");
while($row = mysql_fetch_array($query))
{
echo "Employee names are : ".$row["name"];
}
?>
ऊपर दिए गए उदाहरण में mysql_fetch_array() को call किया गया है और उसका result एक variable में store किया गया है। ये PHP function MySQL query के results में से array fetch करके return करता है।
इसके बाद उस variable पर loop चलाया गया है। ये loop तब तक चलता है जब तक की array में कोई value है। जब array में कोई value नहीं होती है तो ये function false return करता है और आपका loop terminate हो जाता है।
Example 1 – Submitting HTML Form Data to Database Using PHP
<html>
<body>
<form method="post" action="databasedemo.php">
Name : <input type="text" name="name" > <br />
Email : <input type="text" name="email" > <br />
<input type="submit" value="Submit">
</form>
</body>
</html>
databasedemo.php
<?php
// Storing HTML form input
$Name = $_POST['name'];
$Email = $_POST['email'];
// Connecting to MySQL
$con = mysql_connect("3306","root","");
// Selecting MySQL database
mysql_select_db("myDB",$con);
// Executing query
$query = mysql_query("INSERT INTO userinfo(Name, Email) VALUES('$name','$email')");
if($query)
{
echo "Information submitted successfully.";
}
// Closing MySQL connection
mysql_close();
?>
Note : PHP version 7.0.0 से MySQL extension को remove कर दिया गया है।
यदि आप PHP version 7.0.0 या उससे आगे का कोई PHP version use कर रहे है तो ऊपर बताये गए MySQL functions द्वारा आप MySQL database को handle नहीं कर सकते है। इसके लिए आपको MySQLi extension या PDO_MySQL extension को use करने की आवश्यकता है।
_________________________________
PHP – File Uploading
- Introduction to PHP file uploading
- PHP $_FILES array
- PHP move_uploaded_file() function
Introduction to PHP File Uploading
PHP आपको server पर files upload करने की ability provide करती है। PHP के द्वारा आप एक या एक से अधिक files upload कर सकते है। PHP के द्वारा आप केवल text file ही नहीं बल्कि binary file जैसे की कोई image आदि भी upload कर सकते है।
PHP आपको upload की जाने वाली file पर पूरा control provide करती है। इसके लिए PHP आपको authentication और file manipulation functions provide करती है। PHP द्वारा आप control कर सकते है की एक बार file upload करने के बाद आप उस file के साथ क्या करना चाहते है।
PHP द्वारा files को upload करने के लिए सबसे पहले आपको ये पता लगा लेना चाहिए की php.ini में file upload allow है या नहीं। यह file सभी PHP operations को control करती है। इस file में file _uploads variable की value on set होनी चाहिए। यदि इसकी value on नहीं है तो आपको इसे change करने की आवश्यकता है।
PHP File Upload Form
PHP द्वारा file को upload करने के लिए आपको एक HTML form की आवश्यकता होती है। इस form के द्वारा ही user अपने computer से file को select करेंगे।
File को select करने के बाद जब users form को submit करेंगे उसी समय आपकी PHP file execute होगी। इस PHP file में वह code होगा जो file को successfully server पर define की गई directory में upload करेगा।
File upload करने के लिए HTML form create करने का उदाहरण निचे दिया जा रहा है।
<form action=”uploader.php” method=”post” enctype=”multipart/form-data”>
Select File to Upload : <input type=”file” name=”myfile”>
<input type=”submit” value=”Upload”>
</form>
|
ऊपर दिए गए उदाहरण में form tag के अंदर क्रमशः action, method और enctype attributes define किये गए है। इनमें action attribute से आप उस php file को define करेंगे जो form के submit होने पर execute होगी। यदि आप PHP code और HTML code same ही file में लिख रहे है तो आप इस attribute को blank छोड़ सकते है।
दूसरा attribute method है जो data को send किये जाने वाले method को define करने के लिए use किया जाता है। File uploading के case में इस attribute की value आप post ही define करते है।
तीसरा attribute enctype है। यह attribute file के encoding type को define करता है। File uploading के लिए इस attribute का define किया जाना आवश्यक है।
जिस प्रकार text box create करने के लिए आप input type text define करते है। उसी प्रकार file के लिए input type file define किया जाता है। जैसा की ऊपर दिए गए उदाहरण में किया गया है।
File को select करने के बाद upload script को call करने के लिए submit button का प्रयोग किया जाता है। जब user submit button पर click करता है तो action attribute में define की गयी script call हो जाती है।
$_FILES Array
जिस प्रकार $_POST और $_GET arrays के द्वारा आप form के दूसरे elements को PHP file में access करते है। उसी प्रकार select की गयी file को PHP script में access करने के लिए PHP आपको $_FILES array provide करती है।
$_FILES array द्वारा आप file और उससे related attributes को access कर सकते है। इस array के द्वारा आप file का नाम, file का type, file की size, file का temporary name और उससे सम्बंधित errors को access कर सकते है। यह information हर file के साथ attached होती है।
निचे file की properties को access करने का code किया जा रहा है। इन codes में myfile form के input type file element के नाम attribute की value है।
$_FILES[‘myfile’][‘name’];
यह code upload की जाने वाली file के नाम को return करता है।
$_FILES[‘myfile’][‘type’];
यह code file के type को return करता है।
$_FILES[‘myfile’][‘size’];
यह code file की size को return करता है।
$_FILES[‘myfile’][‘tmp_name’];
जब file user के computer से select की जाती है तो उसे temporary नाम दिया जाता है। यह code temporary file name return करता है।
$_FILES[‘myfile’][‘error’];
यह code file से related errors को return करता है।
move_uploaded_file() Function
किसी भी file को server में move करने के लिए PHP आपको built in move_uploaded_file() function provide करती है। यह function file को user के computer से server पर move करने का कार्य करता है।
इस function का general syntax निचे दिया जा रहा है।
bool move_uploaded_file(String $file-name, String $target)
|
जैसा की आप ऊपर दिए गए syntax में देख सकते है move_uploaded_file() function एक boolean value return करता है। इस function को आप if statement के साथ use करते है।
इस function में आप दो arguments pass करते है। पहला argument upload की जाने वाली file का temporary नाम होता है और दूसरा argument वह location होती है जहाँ पर आप file को store करना चाहते है।
यह method file को server में move करने का प्रयास करता है। यदि file successfully move हो जाती है तो यह function true return करता है। यदि file successfully upload नहीं होती है तो यह function false return करता है।
Example
PHP द्वारा file upload करना निचे उदाहरण के माध्यम से समझाया जा रहा है।
<html>
<head>
<title>PHP File Upload Demo</title>
</head>
<body>
<h1>PHP File Upload Demo</h1>
<form action=”file1.php” method=”post” enctype=”multipart/form-data”>
Select a File to Upload : <input type=”file” name=”myfile”> <br /> <br />
<input type=”submit” value=”Upload File”>
</form>
</body>
</html>
|
<?php
// Target directory
$target_path = “d:/”;
// Setting base name of file to upload.
$target_path = $target_path.basename($_FILES[‘myfile’][‘name’]);
// Uploading file to target path
if(move_uploaded_file($_FILES[‘myfile’][‘tmp_name’],$target_path))
{
echo “<h2>File Uploaded Successfully.</h2>”;
}
else
{
echo “<h2>Sorry, Unable to upload file. This could be because of some error.</h2>”;
}
?>
|
ऊपर दिए गए उदाहरण में move_uploaded_file() के दूसरे argument के रूप में $target_path string pass की गयी है। यह string target location का path store करती है।
इस उदाहरण में target location d:/ drive दी गयी है। क्योंकि आप local host use कर रहे है इसलिए आपको अपने ही computer की drive का path देना होगा। इस उदाहरण में file upload होने के बाद d:/ drive में store होगी।
$target_path एक string variable है जो upload किये जाने वाले path को store करता है। Script की second line में इस string पर basename() function call किया गया है। यह function बताता है की file को किस base नाम के साथ store करना है।
जैसे ही आप file को select करके upload button पर click करेंगे file1.php file execute हो जायेगी और file d:/ drive में upload हो जायेगी। File successfully upload होने पर File uploaded successfully message show होगा नहीं तो error message show होगा।
PHP – Errors
- Introduction to PHP errors
- Types of PHP errors
- Handling PHP errors
Introduction to PHP Errors
जब भी आपके code में कोई error आती है तो PHP उसे एक report की तरह show करती है। जितनी भी errors PHP शो करती है वो किसी ना किसी type की होती है।
ये सभी types PHP में constants के द्वारा represent किये गए है और इन सभी की value integer होती है। जब किसी constant की value 1 होती है तो वो error आपको शो होती है। और जब तक उस type की error नहीं आती है तब तक constant की value 0 रहती है। आइये इन error types के बारे में detail से जानने का प्रयास करते है।
Types of PHP Errors
Constants
|
Description
|
E_Error
|
ये एक रन टाइम error होती है। इससे program रुक जाता है। जैसे की memory allocation problem.
|
E_WARNING
|
ये रन time warnings होती है इससे program रुकता नहीं है।
|
E_PARSE
|
ये compile time parse errors होती है।
|
E_NOTICE
|
ये run time में notice होता है की error generate हो सकती है।
|
E_CORE_ERROR
|
ये वो error होती है जो PHP के startup में आती है।
|
E_CORE_WARNING
|
ये वो warnings होती है जो PHP के startup में show होती है।
|
E_COMPILE_ERROR
|
ये वो errors होती है जो PHP code को compile करते समय generate होती है।
|
E_COMPILE_WARNING
|
ये वो warnings होती है जो PHP code को compile करते समय शो होती है।
|
E_USER_ERROR
|
ये वो errors होती है जो user खुद के कोड द्वारा खुद generate करता है।
|
E_USER_WARNING
|
ये यूजर के द्वारा generate की गयी warnings होती है।
|
E_USER_NOTICE
|
ये यूजर के द्वारा generate किए हुए notices होते है।
|
E_STRICT
|
ये आपके कोड को improve करने के लिए PHP द्वारा दिए हुए suggestions होते है।
|
E_RECOVERABLE_ERROR
|
ये errors आसानी से पकड़ में आने वाली होती है। इनसे आपकी application को कोई नुकसान नहीं होता है।
|
E_ALL
|
ये सभी errors को एक साथ represent करने के लिए यूज़ की जाती है।
|
PHP Error Handling
जब भी आपके program में error आये तो उसे handle करना बहुत जरुरी होता है। यदि आप error handling का काम PHP पर छोड़ेंगे तो error आने पर ज्यादातर समय आपका program रुक जाता है।
PHP die() Function
लेकिन यदि आप चाहते है की error आने पर भी आपका प्रोग्राम ना रुके तो आप उन errors को खुद handle कर सकते है। इसका पहला तरीका तो ये है की आप die() function यूज़ करे। उदाहरण के लिए आप किसी file को PHP code के द्वारा open कर रहे है। लेकिन यदि वो फाइल exist नहीं करती तो आपका program error शो करेगा और terminate हो जायेगा।
यदि आप चाहते है की आपका program error शो करे लेकिन terminate ना हो तो इसके लिए आप पहले error condition को चेक करते है। मतलब पहले आप चेक करते है की file exist करती है या नहीं और यदि file exist नहीं करती है तो आप die() function के द्वारा एक error message शो कर देते है।
<?php
if(error_condition)
{
die("Error Message");
}
?>
Error Handler Function
PHP में errors handle करने का दूसरा तरीका ये है की आप एक खुद का error handler क्रिएट करे। ऐसा करने के लिए आप error_function() use करते है। इस function में आप 2 parameters पास करते है एक तो ऊपर define किये गए error constants और दूसरा error message होता है।
error_function(error_type_constant, error_message)
{
// Handle this type of errors here
}
जब एक बार आप error handler define कर दे तो उस तरह की errors के लिए आपको उसे set भी करना पड़ता है। ताकि जब भी उस type की कोई error आये तो आपका function उसे handle कर ले। ऐसा आप इस तरह से कर सकते है।
set_error_handler("myErrorFunction");
Error handler function द्वारा error handle करने का उदाहरण निचे दिया जा रहा है।
<?php
// Error handler function
function errorHandler($errno, $errstr)
{
// Displaying error number.
echo "<h2>Error Number : $errno</h2>";
// Displaying error description.
echo "<h2>Error Description : $errstr</h2>";
}
// Setting error handler
set_error_handler(errorHandler);
// Generating error
echo "$name";
?>
ऊपर दिए गए उदाहरण में सबसे पहले एक error handler function create किया गया है। यह function program में error आने पर call होगा। इसके बाद set_error_handler() method द्वारा इसे set किया गया है। आखिर में $name को print करने का प्रयास किया गया है। यह statement error generate करता है क्योंकि $name variable को कँही define ही नहीं किया गया है।
Output- Enter Number : 8
Error Description : Undefined variable: name
PHP आपको एक और feature provide करती है। PHP में आप खुद की error भी generate कर सकते है और message शो कर सकते है। इसके लिए आप trigger_error function यूज़ करते है।उदाहरण के लिए आप चाहते है की यूज़र किसी variable के लिए 10 से maximum value input ना करे और यदि यूजर ऐसा करता है तो आपको उसे error शो करनी है। ऐसा आप इस प्रकार कर सकते है।
if($value>10)
{
trigger_error("Value can not be more than 10");
}
PHP Exceptions
PHP भी दूसरी programming languages की तरह exception handling mechanism provide करती है। इसमें आप कुछ keywords की मदद से exceptions को handle करते है।
Exceptions ऐसी errors होती है जो run time पर आती है और जिनकी वजह से प्रोग्राम का execution रुक जाता है। जैसे की यदि आप किसी number को 0 से divide कर दे तो program का execution रुक जायेगा। ये एक arithmetic exception है।
Constants
|
Description
|
Try
|
ये एक ब्लॉक होता है। इसमें आप वो code लिखते है, जिसमे error generate हो सकती है।
|
Catch
|
ये भी एक block होता है जिसमे आप exceptions को catch करते है और उनपे action लेते है।
|
Throw
|
ये एक keyword होता है, try block के बाहर से exceptions throw करने के लिए यूज़ किया जाता है।
|
Finally
|
Exception को handle करने के बाद यदि आप कुछ task perform करना चाहते है वो इस block में आता है।
|
Example
<?php
function anyFunction($num)
{
if ($num == 0) {
// Throwing exception
throw new Exception("Divide By Zero!");
}
$result = 100 / $num;
return $result;
}
try {
// Generating exception
echo anyFunction(0);
}
catch (Exception $e) {
// Printing exception description
echo "<b>Exception : </b>", $e->getMessage();
}
?>
ऊपर दिए गए उदाहरण में anyFunction को call करते समय zero pass किया गया है। Function के अंदर ये check किया जा रहा है की pass किया गया number zero है या नहीं। यदि number zero है तो exception generate होगी। यदि number zero नहीं है तो 100 को उस number से divide करके result show किया जाएगा।
_________________________________
