PHP Language(part-1)





Introduction to PHP namespaces 
Namespaces एक तरह के डेटा को एक साथ bind करने के लिए यूज़ किये जाते है। साथ ही ये same नाम की classes एक project में यूज़ करने के लिए भी उपयोगी है। जैसा  की आपको पता है की आप एक ही नाम की classes अपने project में यूज़ नहीं कर सकते है। यदि आप ऐसा करेंगे तो errors शो होगी। लेकिन यदि आप इन same नाम की classes को अलग अलग namespaces में रख दे तो आप इस problem से बच सकते है। 
एक namespace में आप classes, interfaces और functions store करवा सकते है। ये सभी classes, interfaces और functions के नाम वाली classes, interfaces और functions यदि किसी दूसरे namespace में भी है तो आपका project में कोई error नहीं आएगी। क्योंकि दोनों अलग अलग namespaces में है।
1. Defining namespaces 
Namespaces को declare करने के लिए namespace कीवर्ड यूज़ किया जाता है। जब भी आप किसी file में namespace declare करे तो आपको namespace declaration file में सबसे ऊपर करना चाहिए।   
    <?php
namespace myNamespace;
//classes
//interfaces
//functions?>
view rawDefneNmspc.php hosted with ❤ by GitHub
जिन classes को आप एक namespace में डालना चाहते है आपको उन सबको एक ही file में रखना जरुरी नहीं है।  आप जिस भी file के content को उस namespace में डालना चाहते है उस file में टॉप पर आप वापस namespace को declare कर सकते है। ऐसा करने से उस file का content भी उसी namespace में आ जायेगा। 
2. Sub namespaces
किसी sub file directory की तरह ही आप sub namespaces भी क्रिएट कर सकते है। 
3. Using Namespaces  
 
किसी भी classes, interfaces और functions को  namespace के नाम द्वारा access कर सकते है। इसके लिए आप पहले namespace का नाम लिखते है उसके बाद (/) लगा के जो class, interface या function आप यूज़ करना चाहते है उसका नाम लिखते है। 
    <?php
namespace myNamespace;
Class myClass
{
    public function _construct()
   {
       echo "this is a namespace";
   }
}  
view rawfile1.php hosted with ❤ by GitHub
<?php
require 'file1.php';
$obj = new /myNamespace/myclass();  ?>

 





PHP tags
Introduction to PHP tags
जब PHP किसी HTML फाइल को parse करती है तो parser (ये compiler का part होता है और PHP code को अलग अलग हिस्सों में तोड़ता है ताकि पता लग सके की कौनसा हिस्सा tag है और कौनसा actual data है।)  opening और closing tags को search करता है। Parser वही कोड parse करता है जो opening और closing tags के बीच में हो बाकि सारा कोड parser ignore कर देता है। 
ऐसा हो सकता है की आपको अपने HTML पेज में कई जगह PHP code यूज़ करना पड़े। यदि आप ऐसा बिना PHP tags के करते है तो parser PHP code को execute नहीं कर पायेगा। 
इसलिए आप जँहा भी PHP code इम्प्लीमेंट करेंगे वो opening और closing tags के बीच में होगा। 
PHP 4 तरह के opening and closing tags प्रोवाइड करती है आप अपनी सहूलियत से जो चाहे यूज़ कर सकते है। 
PHP common tags     
<?php
// your code here 
?>
ये सबसे commonly यूज़ किये जाने वाले टैग्स है। ज्यादातर standard applications में आप देखेंगे की इन्ही tags का इस्तेमाल किया जाता है। 
PHP script tags    
<script language ="PHP">
// your code here 
</script>
जैसे आप java script का code head section में script tag के द्वारा लिखते है वैसे ही आप PHP का कोड भी script tag में डाल सकते है।  
PHP ASP style tags 
<%
// your code here 
%>
अगर आप ASP के साथ familiar है तो आप ये ASP style tags यूज़ कर सकते है।  
PHP short form tags
<?
// your code here 
?>
यदि आप PHP tags की कोई short form ढूंढ रहे है तो आप ये tags यूज़ कर सकते है। 
PHP comments 
PHP में आप comments 3 तरह से कर सकते है -
// your comment here 
/* your comment here */
# your comment here 

 




Data Types
Introduction to PHP data types
आप किस तरह की information किसी variable में store करना चाहते है ये आप 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
1. Scalar types 
Scalar data types को base types भी कहा जाता है। ये data types एक programming language by default provide करती है। इस तरह के data type एक single value को hold करते है। जैसे की int, char, float, boolean आदि।
2. Integer

Integers किसी संख्या को स्टोर करने के लिए यूज़ किये जाते है। Integers हमेशा पूर्ण संख्या को स्टोर करते है। आप दशमलव संख्या integer variables में स्टोर नहीं कर सकते है।
PHP में आप integer variables इस प्रकार क्रिएट कर सकते है।
3. Float
Float types के variables दशमलव संख्या स्टोर करने के लिए यूज़ किये जाते है। PHP में आप floating point variables इस प्रकार स्टोर कर सकते है।
4. Boolean
Boolean types के variables truth values store करते है। आप true और false में से कोई एक value boolean type के variables में स्टोर कर सकते है।
PHP में आप boolean types इस प्रकार क्रिएट कर सकते है।
5. String
String types के variables में आप string स्टोर कर सकते है। एक स्ट्रिंग characters की sequence होती है। PHP में आप string type के variables इस प्रकार क्रिएट कर सकते है।
6. Compound types 
Compound data types वो data types होते है जो programming language को यूज़ करते हुए आप अपने program में बनाते है। जैसे की arrays, objects, structure, linked-lists, queue, stack आदि।
7. Array
PHP में array एक map की तरह होता है। आप values को keys के द्वारा स्टोर करवाते है। PHP में arrays को आप किसी list की तरह भी यूज़ कर सकते है और किसी hash-table की तरह भी यूज़ कर सकते है।
PHP में arrays आप इस प्रकार क्रिएट कर सकते है -
8. Objects
PHP में आप object types भी क्रिएट कर सकते है। Object types क्रिएट करने के लिए पहले आपको क्लास क्रिएट करनी होगी। PHP में क्लास और ऑब्जेक्ट क्रिएट करना किसी और प्रोग्रामिंग लैंग्वेज की तरह बिलकुल आसान है।
9. Special types

Special type data types वो data types होते है जो किसी एक particular language के लिए ही बने होते है। PHP के special data types निचे दिए जा रहे है।
10. Resource
एक resource स्पेशल टाइप होता है जो external resources को hold करता है। जैसे की database connection या फाइल्स जो आप PHP कोड के द्वारा open करते हो।
11. NULL
यदि आपने कोई variable क्रिएट किया है लेकिन उसमे आप initially कोई वैल्यू स्टोर नहीं करवाना चाहते तो आप उसमे NULL स्टोर करवा सकते है। NULL का मतलब होता है की उस variable की कोई वैल्यू नहीं है।
 





Variables
Introduction to PHP variables
Variables किसी information को store करने के लिए यूज़ किये जाते है। ये information कुछ भी हो सकती है जैसे किसी व्यक्ति का मोबाइल नंबर या उनका पता। Variables किसी भी programming language के सबसे छोटे element होते है और पावरफुल भी। 
PHP में variables create करना किसी दूसरी लैंग्वेज से different है, सबसे पहले तो में आपको बता दू की जब भी आप PHP में कोई variable क्रिएट करते है तो आप उसके नाम के आगे ($) डॉलर साइन लगाते है। 
दूसरा जब आप PHP में कोई variable declare करते है तो उसका data type mention नहीं करते है। PHP ये automatically identify करती है। जब आप variables में value assign करवाते है तो जिस type की वैल्यू होती है variable भी उसी type का बन जाता है।  
PHP में variables case sensitive है मतलब var और Var दो अलग अलग वेरिएबल्स है। 
1. Creating variables in PHP
PHP में variables क्रिएट करना बहुत ही आसान है। इसके लिए निचे 3 rules दिए जा रहे है जो आपको फॉलो करने होते है। 
* Dollar sign before variable name
* Unique variable name
* No need to mention data types
PHP में variables क्रिएट करने का उदाहरण निचे दिया गया है। 
2. Displaying PHP variables

PHP variables की वैल्यू display करने के लिए आपको PHP के predefined functions को यूज़ करना होता है। जैसे की echo या print फंक्शन। जब आप variable की value डिस्प्ले करवाएंगे तो वंहा भी आपको डॉलर साइन लगाना होगा।    
PHP में variables की value display करने का उदाहरण नीचे दिया जा रहा है - 







Operators
PHP operators
* Introduction to PHP operators in Hindi
* Arithmetic operators in Hindi
* Assignment operators in Hindi
* Bit wise operators in Hindi
* Comparison operators in Hindi
* Error control operators in Hindi
* Execution operators in Hindi
* Increment/Decrements operators in Hindi
* Logical operators in Hindi
* String operators in Hindi
* Array operators in Hindi
* Type operators in Hindi
Introduction to operators
Variables के साथ operations जैसे की addition, division, comparison आदि perform करने के लिए operators का प्रयोग किया जाता है। जब हम किसी variable के साथ operators को यूज़ करते है तो वो 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 करते है।
1. Arithmetic operators
Arithmetic operators वेरिएबल्स के साथ arithmetic operations perform करने के लिए यूज़ किये जाते है। कुछ common arithmetic operators की list निचे दी जा रही है।
* Operator --- Negation(-) unary
* Explanation - Opposite of variable
​* Example --- -$a
* Operator --- Addition (+)
* Explanation - Addition of 2 variables
* Example --- $a+$b
* Operator --- Subtraction(-)
* Explanation - Subtraction of 2 variables
* Example --- $a-$b
* Operator --- Multiplication(*)
* Explanation - Multiplication of 2 variables
* Example --- $a*$b
* Operator --- Division(/)
* Explanation - Division of 2 variables
* Example --- $a/$b
* Operator --- Modulus
* Explanation - Remain from division
* Example --- $a%$b
* Operator --- Exponentiation
* Explanation - First variable raise to power to second variable
* Example --- $a**$b





2. 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




3. Bit wise operators
Bit wise operators bits पर operation perform करते है। कोई भी decimal, memory level पर bits में convert होता है। यदि आप bits पर operation perform करना चाहते है तो bit wise operators यूज़ कर सकते है।
* Operator --- AND (&)
* Explanation -- Common bits from both variables are set
* Example --- $a&$b
* Operator --- OR (|)
* Explanation -- All bits of both variables are set
* Example --- $a|$b
* Operator --- X-OR(^)
* Explanation -- Bits which are in $a but not in $b are set
* Example --- $a^$b
* Operator --- NOT(~)
* Explanation -- All the bit becomes opposite
* Example --- ~$a
* Operator --- Shift Left(<<)
* Explanation -- Shift $a bits $b times to left
* Example --- $a<<$b
* Operator --- Shift Right(>>)
* Explanation -- Shift $a bits $b times to right
* Example --- $>>$b





4. Comparison operators
Comparison operators दो variables की values को compare करने के लिए यूज़ किये जाते है। Comparison operators का प्रयोग ज्यादातर control statements में किया जाता है। Comparison variables का result TRUE या FALSE होता है।
* Operator --- Equal (==)
* Explanation - Returns true if both operands are equal
* Example --- $a==$b;
* Operator --- Not Equal (!=)
* Explanation - Returns true if both operands are not equal
* Example --- $a!=$b;
* Operator --- Less than (<)
* Explanation - Returns true if first variable less than second variable
* Example --- $a<$b;
* Operator --- Greater than (>)
* Explanation - Returns true if first variable greater than second variable
* Example --- $a>$b;
* Operator --- Less than equal to (<=)
* Explanation - Returns true if first variable is less than or equal to second variable.
* Example --- $a<=$b;
* Operator --- Greater than equal to (>=)
* Explanation - Returns true if first variable is greater than or equals to second variable.
* Example --- $a>=$b;



5. Error control operators
PHP में एक error control operator यूज़ किया जाता है। ये operator होता है (@)। जब आप यह operator किसी statement के साथ यूज़ करते है तो उस statement से generate होने वाली errors को ignore कर दिया जाता है। ये operator ज्यादातर file handling में यूज़ किया जाता है।



6. Execution operators
PHP में एक execution operator यूज़ किया जाता है। ये operator होता है (``)। Execution operator के माध्यम से आप कोई भी shell command PHP code के द्वारा execute कर सकते है।
Ex.
<?php
$a=`yourCommand`;
echo $a; ?>







7. 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






8. Logical operators
Logical operators logic perform करने के लिए यूज़ किये जाते है। ये operators control statements में भी यूज़ किये जाते है।
* Operator --- And(&&)
* Explanation - True if both variables are true.
* Example --- $a&&$b
* Operator --- Or(||)
* Explanation - True if either one variable is true.
* Example --- $a||$b
* Operator --- Not(!)
* Explanation - True if variable is not true.
* Example --- !$a
* Operator --- Xor
* Explanation - True if only one is true not both.
* Example --- $a Xor $b







9. 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="bitu";
$a.="sharma";

10. Array operators
PHP कुछ ऐसे array operators भी provide करती है जो सिर्फ arrays पर apply होते है।
* Operator --- Union (+)
* Explanation - Add to arrays.
* Example --- $a+$b
* Operator --- Equality (==)
* Explanation - True if both arrays have same values.
* Example --- $a==$b
* Operator --- Identity (===)
* Explanation - True if both arrays have same values of same type and in same order.
* Example --- $a===$b
* Operator --- Inequality(!=)
* Explanation - True if both arrays are not equal.
* Example --- $a!=$b



11. Type operators
PHP में एक type operator भी यूज़ किया जाता है। ये operator ये decide करने के लिए यूज़ किया जाता है की कोई object किसी particular क्लास का है या नहीं।
Example
$ output = $a instanceof yrClass;






Control Statements
Introduction to PHP control statements in Hindi 
* If statement in Hindi 
* If else statement in Hindi 
* While loop in Hindi 
* Do-while loop in Hindi 
* For loop in Hindi
* For each loop in Hindi 
* Continue and break statement in Hindi
* Switch statement in Hindi
* Declare statement in Hindi
* Return statement in Hindi

* Require statement in Hindi
* Include statement in Hindi
* Go-to statement in Hindi
Introduction to control statements 
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 --- Selection statements
* Statements - If, if-else, switch
* Explanation - Select and execute statements based on logic.
* Type --- Looping statements
* Statements - While, do-while, for, for each
* Explanation - Executes given number of statements specified number of times. 
* Type --- Jump statements
* Statements - Continue, break and go-to
* Explanation - Start execution from a particular position in program.
* Type --- Special PHP statements
* Statements - Declare, return, require and include
* Explanation - These are special PHP control statements
आइये इन सभी control statements के बारे में जानने का प्रयास करते है।  

 
1. If statement
If statement में आप condition देते है, यदि condition true है तो if statement के बाद वाले block में दिए हुए statement execute हो जाते है। और यदि condition false है तो compiler brackets को skip कर देता है और statements execute नहीं होते है।
Structure
if(condition)
{
   // if condition true, statement of here will be executed
}
Example
If(3<5)
{
   // execute statement here because condition is true
}

 
2. If else statement 
If else statement में यदि condition true है तो if के बाद वाला brackets का block execute होगा नहीं तो else के बाद वाला brackets का block execute होगा। 
Structure
if(condition)
{
   // if condition true, statement of here will be executed
}
Else
{
   //if condition false, statement of here will be executed
}
Example
If(3>5)
{
   // can’t execute because condition is false
}
Else
{
   // execute statements here because condition is false
}
 
3. 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
While(Condition)
{
    //statements you want to execute when condition is true
// change condition here (increase condition variable)
}      
Example
$i = 1;
While($i<5)
{
     //statements you want to execute
     i++; // increasing condition variable so that loop can terminate after finite number of loops.
}
 
4. 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
Do
{
   // execute these statements
  // increase condition variable
}while(Condition)
Example
$i=1;
Do
{
  // execute statements of here, will be executed once for sure.
 // change condition
}while($i<5)

 
5. For loop 
For loop एक बहुत ही simple loop होता है। इस loop में आप loop को control करने वाले variables को initialize करते है, condition लिखते है और variable को increment करते है और ये सब एक ही जगह होता है। 
Structure
for(initialization; condition; increment)
{
   // statement to execute when condition is true
}
Example
for($i=1; $i<5; $i++)
{
  // statements to execute
}

 
6. For each loop 
For each loop arrays को iterate करने के लिए बनाया गया है। ये लूप सिर्फ arrays के साथ काम करता है। यदि आप इसे variables के साथ यूज़ करते है तो ये error शो करता है। For each loop के साथ आप array elements पर आसानी से operation perform कर सकते है।
Structure
foreach($arr_name as $value)
{
   // do something with array elements
}  
Example
$arr_name = array(2,4,6,8);
foreach($arr_name as $value)
{
   echo $value;
}

 
7. Continue and break statement 
Continue statement किसी loop की iteration को skip करने के लिए यूज़ किया जाता है। जैसे की यदि आप loop में कोई ऐसी condition आये जँहा पर आप कोई भी task नहीं perform करना चाहते है तो इसके लिए आप continue statement का प्रयोग करते है।
Structure
for(initialization; condition; increment)
If(condition)
{
   Continue;
}
Example
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 हो जायेगा।    

 
8. Switch statement 
Switch statement if की तरह ही होता है। Switch में cases होते है। आप एक choice देते है। Switch के जिस case से choice match करती है वही execute हो जाता है। जब कोई भी case match नहीं होता है तो default case execute हो जाता है।
Structure
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
}
Example
Switch(2)
{
  case 1:
                  //this will not be executed
                     Break;
  case 2:
                  // this will be executed
                      Break;
  Default:
                  //this will not be executed because case matched.
}

 
9. Declare
Declare statement के द्वारा आप किसी block of code के execution को निर्देशित कर सकते है।
Structure
declare(directive) // ticks or encoding
{
  //statement to apply a directive
}
Example
declare(ticks=1)
{
  //you may call a function for all ticks
}

 
10. Return statement 
ये statement ज्यादातर functions के साथ यूज़ किया जाता है। जब return statement execute होता है तो execution function से return हो जाता है। Return statement के माध्यम से आप function से कोई value या variable भी return कर सकते है।


 
11. Include statement 
इस statement के द्वारा आप किसी दूसरी php file को अपनी किसी file में include कर सकते है। जिस जगह पर आप उस file को include करेंगे उसी जगह उसका content शो होगा। जिस file का नाम आपने दिया है यदि वो exist नहीं करती है तो compiler warning show करता है लेकिन execution नहीं रुकता है।
Structure
<?php
Include file_Name.php;
?>
Example
<?php
echo “lets include another file”;
include other_File.php;
?>

 
12. Require statement 
ये statement include की तरह ही होता है लेकिन इसमें यदि file exist नहीं करती है तो program का execution रुक जाता है।
Go to statement 
Go to statement program में किसी दूसरे section में जाने के लिए प्रयोग किया जाता है।
Structure
<?php
//statements
goto a;
//statements
a:
// statements
?>
Example
<?php
goto a;
echo “ skip this  “;
a:
echo “print this first ”;
?>






Functions
Introduction to PHP functions 
यदि आपके program में कोई ऐसा task है जिसे आपको बार बार execute करने की जरुरत है तो आप उस task के लिए program में अलग अलग जगह पर code लिखने की बजाय एक function create कर सकते है। और जब भी आपको उस task को perform करने की आवश्यकता हो तो आप उस function को अलग अलग जगह पर call कर सकते है।
ऐसा करने से आपका time भी बचता और computer की memory भी बचती है साथ ही आपका program भी short और readable बन जाता है। इसे code re-usability भी कहा जाता है। क्योंकि आप एक ही code को अलग अलग जगह पर यूज़ करते है।
यदि में आपसे पुछुं की किसी program में echo या print function की जरुरत कितनी बार पड़ती है। तो आप कहेंगे की बहुत बार पड़ सकती है। अब मान लीजिये यदि इन function का पूरा code आपको हर बार लिखना पड़े तो program कितना बड़ा हो जायेगा और कोई भी simple project पूरा करने में बहुत time लग जायेगा। ऐसी situations से बचने के लिए programs में functions यूज़ किये जाते है।
Functions किसी भी program के basic structure होते है। C की तरह ही PHP भी एक modular language है। PHP में function create करने के लिए आप function कीवर्ड यूज़ करते है। PHP functions के बारे में सबसे unique बात ये है की आप किसी function में दूसरा function तो create कर ही सकते है साथ ही एक class भी create कर सकते है। 
PHP में anonymous functions भी introduce किये गए है। ये java की तरह ही है। ये बिना नाम के functions होते है। ऐसे functions ज्यादातर किसी function को पहले क्रिएट करने के बजाय साथ की साथ बनाने और यूज़ करने के लिए उपयोग किये जाते है।      
1. Types of functions 
PHP में 3 तरह के functions यूज़ किये जाते है, इनके बारे में निचे समझाया जा रहा है।
2. User-defined functions 
PHP में function क्रिएट करने के लिए आपको function keyword यूज़ करना होता है। PHP का basic function structure निचे दिया जा रहा है। 
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 के नाम के बाद brackets में arguments पास किये जाते है।
* Arguments वो values होती है जो function को call करवाते समय पास की जाती है। जैसे की यदि आपका function 2 numbers के addition का है तो आप add करने के लिए 2 numbers pass कर सकते है। ताकि function इन पर addition का operation perform कर सके।
* Arguments के बाद curly brackets में कुछ statements होते है जिन्हें आप execute करवाना चाहते है। जैसे की हमारे example में आप arguments में पास किये गए numbers को add  करवा सकते है।
* PHP में function call करने के लिए आप सिर्फ function का नाम लिखते है और उसके बाद brackets में required arguments पास करते है। PHP functions को आप कई प्रकार से call कर सकते है। जैसे की echo statement में भी आप PHP functions को call कर सकते है।
* ऊपर दिए गए function को आप इस प्रकार call कर सकते है।
* PHP में function क्रिएट करते समय आप चाहे तो default argument भी दे सकते है। Function call के दौरान जब user कोई argument नहीं पास करे तो default argument यूज़ किया जाता है। किसी भी argument को default argument देने के लिए आप उसके आगे assignment operator (=) लगा के default argument लिख देते है।  इसका example निचे दिया जा रहा है।
* ऊपर दिए गए उदाहरण में यदि यूजर खुद का नाम नहीं पास करता है तो Welcome के बाद Sir/Mam शो होगा। 
1. Built-in functions 
PHP programmers के लिए कुछ built in functions provide करती है। ये functions programmer का काम और भी आसान कर देते है। कुछ common built-in functions की list निचे दी जा रही है। 
* Functions --- echo()
* Examination - Prints text and variables, does not return value.
* Functions --- Print()
* Examination - Prints text and variables, returns value of 1
* Functions --- mysql_connect()
* Examination - Connects to MySQL database.
* Functions --- date()
* Examination - Prints current date.
* Functions --- count()
* Examination - Used to count number of character in strings.
* Functions --- file_exists()
* Examination - Checks whether a file exists.
2. Anonymous function 
PHP में नए तरीके के functions add किये गए है जिन्हे anonymous function कहते है। इन functions की सिर्फ body होती है, इनका कोई नाम नहीं होता है।  इन functions को किसी भी expression के बीच में यूज़ कर सकते है। इसके लिए function keyword use करते है। Anonymous function का example निचे दिया जा रहा है।

 






Object Oriented Programming
* PHP classes and objects 
* Introduction to PHP classes in Hindi
* Creating objects in Hindi
* PHP visibility in Hindi

* PHP properties in Hindi
* PHP classes constructor and destructors in Hindi 
* PHP abstract classes in Hindi
* PHP inheritance in Hindi
* PHP interfaces in Hindi
Introduction to PHP classes 
Classes किसी भी object oriented programming language का बहुत important concept होती है। ये different data types और functions का collection होती है। Classes के features के बारे में आगे दिया जा रहा है।
* Features of using classes 
* Data secure हो जाता है। 
* एक तरह का data दूसरे तरह के data से separate हो जाता है।  
 
* Programmer का पूरा focus डेटा पर होता है। 
* Program की complexity कम हो जाती है।   
PHP में class किसी दूसरी language की तरह ही क्रिएट की जाती है लेकिन PHP classes में कोई main method नहीं होता है। Class क्रिएट करने के लिए आप class keyword यूज़ करते है।
1. Creating objects
PHP में objects क्रिएट करना बहुत ही आसान है। इसके लिए आप new keyword यूज़ करते है। किसी simple variable की तरह ही object क्रिएट किया जाता है।
2. Visibility 
PHP में visibility का मतलब होता है की कौन कौन आपके function, property या class को access कर सकता है। ये आपकी classes, methods और properties के लिए एक protection लेवल होता है। ताकि हर कोई आपके डेटा को access न कर सके। Visibility 3 प्रकार की होती है -
* Visibility --- Private
* Purpose --- No other classes, methods or properties can access your class, method or property.
* Visibility --- Public
* Purpose --- All other classes, methods or properties can access your class, method or property.  
* Visibility --- Protected
* Purpose --- Only inherited classes can access your class, function or property.
3. PHP Properties 
Class के अंदर जो आप variables declare करते है वो properties कहलाती है। यदि property non-static है तो आप (->) operator के द्वारा access करते है। और यदि property static है तो (::) operator के द्वारा access करते है। Non static property का उदाहरण निचे दिया जा रहा है।
4. Constructors and destructors 
ऐसा हो सकता है की object को यूज़ करने से पहले आपको कुछ initialization करना हो या कोई दूसरा task परफॉर्म करने की आवश्यकता हो तो ऐसी situation में आप constructor क्रिएट कर सकते है। Constructor एक method की तरह ही होता है। जब आप किसी क्लास का object क्रिएट करते है तो उस class का constructor call होता है। और ऑब्जेक्ट को यूज़ करने से पहले जो काम जरुरी है वो इस method में perform किये जाते है।
Constructor functions की visibility public होती है। किसी function की तरह आप constructor में भी arguments पास कर सकते है।
किसी भी class का constructor क्रिएट करने के लिए आप _construct नाम का एक method define करते है। आइये इसे एक उदाहरण से देखते है।
    <?php
class myClass
{
 
    public function __construct() //constructor
    {
        echo "Hiiii";      
    }
}
$obj = new myClass(); // Calling the constructor
?>
Object destroy करने के लिए destructor का यूज़ किया जाता है। हालांकि PHP में ये काम garbage collector द्वारा automatically किया जाता है। लेकिन फिर भी यदि आप चाहे तो खुद भी किसी ऑब्जेक्ट को destroy कर सकते है।
Object को destroy करने के लिए आप _destruct नाम से एक function create करते है। Object के destroy होने पर यदि आप कोई task perform करना चाहते है तो वो आप इसमें कर सकते है। आइये इसे एक उदाहरण से समझते है।
<?php
class myClass
{
 
    public function __construct()
    {
        echo "Hiiiii";      
    }
    public function _destruct()
    {
         echo “byee!!
    }
}
$obj = new myClass();
?>
5. Abstract classes 
Abstract classes का आप object क्रिएट नहीं कर सकते है। Abstract classes इसीलिए बनायीं जाती है ताकि दूसरी classes उसे inherit कर सके। Abstract classes में आप normal function तो create कर ही सकते है साथ ही abstract function भी क्रिएट कर सकते है। Class और functions को abstract declare करने के लिए आप abstract keyword यूज़ करते है।
Abstract function वो function होते है जो सिर्फ declared होते है ऐसे functions की कोई definition नहीं होती है। जिस class में एक भी abstract function हो वो class भी abstract declared होनी जरुरी है। Abstract function की definition abstract class को inherit करने वाली class देती है।
जब भी कोई class किसी abstract class को inherit करती है तो वो सभी abstract functions की definition provide करती है।
6. Inheritance 
Inheritance एक object oriented principle है। बाकि programming languages की तरह ही PHP भी inheritance यूज़ करती है। इस features के द्वारा एक क्लास किसी दूसरी क्लास के public और protected functions को extend कर सकती है और यूज़ कर सकती है। ऐसा करने से same functions को बार बार लिखने के काम से आप बच जाते है साथ ही memory space भी बचता है। 
दूसरी class के functions को extend करके या तो आप उन्हें वैसा का वैसा यूज़ कर सकते है या फिर अपनी खुद की definition भी provide कर सकते है। किसी class को inherit करना बिलकुल simple है। इसके लिए आप extends keyword यूज़ करते है। इसका उदाहरण निचे दिया जा रहा है।  
7. Interfaces
Interfaces में आप ऐसे methods declare करते जिनकी body नहीं होती है। Interfaces किसी class को बताते है की उसे किस तरह के methods implement करना आवश्यक है। इन functions की body वो क्लास provide करती है जो interface को implement करती है। एक interface किसी दूसरे interface को भी extend कर सकता है। एक class एक से ज्यादा interfaces को implement कर सकती है। 
Interface create करना simple है इसके लिए आप interface keyword यूज़ करते है। किसी interface को implement करने के लिए आप implements कीवर्ड यूज़ करते है।  इसका उदाहरण निचे दिया जा रहा है।

 

No comments:

loading...
Powered by Blogger.