PHP in Hindi




PHP एक Server Side Scripting Language है. PHP का उपयोग Webpages को Dynamic Develop करने के लिए किया जाता है. PHP एक ऐसी Scripting Language है जिसका उपयोग Web Applications और Webpage को Server Side मे Control करने के लिए किया जाता है

PHP Web पर उपयोग होने वाली सबसे लोकप्रिय Scripting Language है . PHP का उपयोग करके आप User का Name और Password का Login Page बना सकते है. PHP के उपयोग से आप Forum भी बना सकते है.

PHP Advantages

  • Open source − PHP उपयोग के लिए स्वतंत्र रूप से उपलब्ध है . PHP Open Source Developers का समुदाय तकनीकी सहायता प्रदान करता है और Core PHP Functionalities को Updates करने में लगातार सुधार कर रहा है.

  • Cross-Platform − PHP Leading Operating Systems और Web Servers जैसी High Consistency प्रदान करता है जिससे कि यह आसानी से कई विभिन्न Platforms में तैनात किया जा सके. PHP Scripts Operating Systems जैसे कि Linux, Windows, Solaris, OpenBSD, Mac OSX इत्यादि में चला सकते हैं और Apache, IIS, iPlanet इत्यादि जैसे सभी प्रमुख Web Servers के लिए समर्थन भी प्रदान कर सकते है.

  • Power − कई Web Tasks को अब आसानी से PHP का उपयोग करके किया जा सकता है. अब हम छोटी Websites से विशाल Business और Organizational Websites Informative Forums, Chatting Platforms, CRM Solutions, E-commerce Shopping Carts, Community Websites, e-business, Shopping Carts और विशाल Database Sites से Develop कर सकते है.

  • User Friendly − User के अनुकूल तरीके से बनाया गया PHP C, C++ और ASP से अधिक Flexibility देता है और Site पर Traffic बढ़ाने में मदद करता है.

  • Quick − PHP को Web के साथ अच्छी तरह से काम करने के लिए Design किया गया है और इस प्रकार GET और POST को Access करने और HTML और URLs के साथ काम करने जैसी चीजें PHP Language मे Manufactured है .

PHP Disadvantages

  • PHP बड़ी Applications के लिए उपयुक्त नहीं है इसे बनाए रखने मे Difficul होती है क्योंकि यह बहुत Modular नहीं है.

  • PHP Framework मे एक खराब Error Handling Method है. यह PHP Developers के लिए उचित Solution नहीं है इसलिए यह एक योग्य PHP Developer के रूप मे आपको इस Problem का सामना करना होता है.

PHP in Hindi Introduction

PHP (Hypertext Preprocessor) एक open-source HTML-embedded सर्वर साइड स्क्रिप्टिंग भाषा है जो web applications को dynamic और interactive बनाने के लिये उपयोग कि जाती है और इसे सामान्य programming भाषा के रूप में भी इस्तेमाल किया जाता है.

Popular Features of PHP

  • किसी भी newcomer के लिये PHP का उपयोग सबसे आसान है क्योकि इसमे उन्नत सुविधाओं और professional programming भी जोड़ी की गई है.

  • PHP सीखने के लिए आसान है और ये server side पर कुशलतापूर्वक runs भी होती है.

  • PHP कई operating systems जैसे Linux, Windows, Mac OS X और कई अन्य पर कार्य करती है.

  • PHP बहुत से databases जैसे MySQL, MS-SQL , Oracle, Sybase, PostgreSQL का समर्थन करती है.

  • PHP website बनाने के लिये बहुत कम समय लेती है .

PHP Hello World Example

<!DOCTYPE html>
<html>   
   <head>
      <title>PHP Hello World Example</title>
   </head>   
   <body>
   <?php
      echo "Hello World...! ";
      echo "This is basic example";
   ?>
   </body>
</html>

PHP in Hindi Variables

एक Variable Data Type के लिए एक Holder होता है . इसलिए इसके Type के आधार पर, एक Variable Numbers, Strings, Booleans, Objects के Resources को hold कर सकता है या यह NULL कर सकता है .

PHP में सभी Variable एक sign "$" से शुरू होते हैं और इसमे "=" operator का उपयोग करके Value को Assign किया जा सकता है . Dollar Sign तकनीकी रूप से Variable के Name का हिस्सा नहीं है लेकिन PHP Parser के पहले वर्ण के रूप में इस तरह के Variable को पहचानना आवश्यक होता है .

PHP मे एक और महत्वपूर्ण बात यह है कि सभी Statements को एक semicolon ";" के साथ ही समाप्त करना चाहिए. PHP मे हमे Variable के Type को Specify करने की ज़रूरत नहीं है क्योंकि यह Assign की गई Value से Data Type को लेता है.

एक Variable को Declare करने के लिए आपको इसे अपनी Script में शामिल करना होगा. आप एक Variable को Declare कर सकते हैं और उसे एक ही Statement मे एक Value के साथ Assign कर सकते है.

Rules for Variable Declaration

  • Variable $ sign से शुरू होता है और Variable Name के साथ.

  • Variable Name Letter या Underscore sign से शुरू होते है उसके बाद Underscore Number या Characters होते है.

  • Variable Numbers से शुरू नहीं होता है.

  • Variable को alpha-numeric Characters का उपयोग करके और केवल Underscores करके Define किया जा सकता है.

PHP Variable Scope

Scope केवल Rules के अलावा कुछ नहीं होता है जब दो अलग-अलग Places के नाम का अर्थ समान अर्थ है. PHP मे Places को Define करने के लिये तीन Variable Scope है .

  • Local Variable Scope

  • Global Variable Scope

  • Static Variable Scope

Local Variable Scope

Local Variable को Function के भीतर Define और Access किया जाता है . ये Function के बाहर Accessible नहीं है .

<!DOCTYPE html>
<html>   
   <head>
      <title>Local Variables Example</title>
   </head>   
   <body>
      <?php 
         function myTest() {
            $x = 6; 
            echo "<p>Variable x inside function is: $x</p>";
         } 
         myTest();
         echo "<p>Variable x outside function is: $x</p>"; 
      ?>
   </body>
</html>

Global Variable Scope

Global Variable को Function के बाहर Define किया जाता है. Function के बाहर Script में कहीं भी आप Global Variable का उपयोग कर सकते है . लेकिन Global Variable को Function के अंदर भी Access किया जा सकता है Variable को Declared होने से पहले Keyword Global का उपयोग करके .

<!DOCTYPE html>
<html>   
   <head>
      <title>Global Variables Example</title>
   </head>   
   <body>
      <?php 
         $x = 6; 
         function myTest() {    
            echo "<p>Variable x inside function is: $x</p>";
         } 
         myTest();
         echo "<p>Variable x outside function is: $x</p>"; 
      ?>
   </body>
</html>

Static Variable Scope

आम तौर पर, जब कोई Function completed/executed होता है तो उसके सभी Variables Delete कर दिये जाते है. हालांकि कभी-कभी हम एक Local Variable को हटाना नहीं चाहते है. हमें इसे और Further के लिए चाहिए होता है . ऐसा करने के लिए आपको Static Keyword का उपयोग करके और पहले Variable को Declare करना होता है

<!DOCTYPE html>
<html>   
   <head>
      <title>Static Variables Example</title>
   </head>   
   <body>
      <?php 
         $a = 70; 
            $b = 105; 
             
            function myVariables() { 
            static $a, $b; 
            $b = $a + $b; 
            }
              
            myVariables();
            echo "The valuse of b is:". $b; 
      ?>
   </body>
</html>

PHP in Hindi Data Types

Data Types सामान्य Type के होते हैं जो किसी विशेष Type की Value को Storage करने की अनुमति देते है. Data Type आकार और Type की Value को को Specified करते है जिनको Stored किया जा सकता है. Variable को Data Type की Value को Declared करने की ज़रूरत नहीं होती है . और PHP मे Data Type की जांच के लिए gettype() Function का उपयोग करते है.

PHP मे कुछ Defined Variables Values को Stored करते है जब भी आप किसी भी Variable को बनाने का प्रयास करते है आपको तो हर बार variable के लिए Data Type को Define करने की आवश्यकता नहीं होती है. लेकिन कभी कभी Situation ऐसे हो सकती है की जब Data Type की पहचान करने की आवश्यकता होती है तो तब Data Type आपके Type के Data को Specified करता है. जिसको आप अपने Variable मे Stored कर सकते है .

PHP निम्नलिखित Data Type को Supports करता है .

  • String

  • Integer

  • Float

  • Boolean

  • Array

  • Object

  • NULL

  • Resource

PHP String

एक string characters का अनुक्रम होता है.

<!DOCTYPE html>
<html>   
   <head>
      <title>PHP String Example</title>
   </head>   
   <body>
      <?php 
         $x = "Hello world!";
         $y = 'Hello world!';

         echo $x;
         echo "<br>"; 
         echo $y;
      ?>

   </body>
</html>

PHP Integer

कोई भी Integer एक data type और non-decimal number के बीच होता है -2,147,483,648 and 2,147,483,647.

<!DOCTYPE html>
<html>   
   <head>
      <title>PHP Integer Example</title>
   </head>   
   <body>
      <?php 
         $x = 5985;
         var_dump($x);
      ?>

   </body>
</html>

PHP Float

एक Float Decimal Digits के साथ एक Number या Exponent Form मे एक Number होता है.

<!DOCTYPE html>
<html>   
   <head>
      <title>PHP Float Example</title>
   </head>   
   <body>
      <?php 
         $x = 20.465;
         var_dump($x);
      ?>
   </body>
</html>

PHP Boolean

Boolean एक switch की तरह होता है और इसमे केवल दो Possible Values होती है TRUE या FALSE.

<!DOCTYPE html>
<html>   
   <head>
      <title>PHP Boolean Example</title>
   </head>   
   <body>
      <?php 
         $x = true;
         $Y = false;
      ?>
   </body>
</html>

PHP Array

एक Array multiple values को एक single variable मे Store करता है .

<!DOCTYPE html>
<html>   
   <head>
      <title>PHP Array Example</title>
   </head>   
   <body>
      <?php 
        $cars = array("Audi","BMW","Benz");
        var_dump($cars);
      ?>
   </body>
</html>

PHP Object

Object एक data type है जो conjunct data और जानकारी को store करता है.

<!DOCTYPE html>
<html>   
   <head>
      <title>PHP Object Example</title>
   </head>   
   <body>
      <?php 
         class Car {
            function Car() {
              $this->model = "VW";
            }
         }
         // create an object
         $herbie = new Car();

         // show object properties
         echo $herbie->model;
      ?>
   </body>
</html>

PHP NULL Value

Null Value एक विशेष data type है जिसमें केवल एक value होती है.

<!DOCTYPE html>
<html>   
   <head>
      <title>PHP NULL Value Example</title>
   </head>   
   <body>
      <?php 
         $x = "Hello world!";
         $x = null;
         var_dump($x);
      ?>

   </body>
</html>

PHP in Hindi Operators

Operators का उपयोग Data को Developed करने के लिए किया जाता है. Programming Languages मे Operators को Mathematics से लिया जाता है और Operator Programmers Data के साथ काम करते है.

Operator एक Symbols की एक Chain है जो कुछ Values के साथ प्रयोग किया जाता है . variables और values पर संचालन करने के लिए हम Operators का उपयोग करते है.

PHP भाषा बहुत से प्रकार के Operators का समर्थन करती है.

  • Arithmetic Operators

  • Comparison Operators

  • Assignment Operators

  • Logical (or Relational) Operators

  • Conditional Operators

Arithmetic Operators

Arithmetic operators को variables के साथ arithmetic operations perform करने के लिए उपयोग किया जाता है. PHP भाषा मे इन + , - , * , / , % , ++ , -- arithmetic operators का उपयोग किया जाता है.

Operatos Description
+ Add
- Subtract
* Multiply
/ Divide and return quotient
% Divide and return modulus

Comparison Operators

Comparison operators दो variables की values को compare करने के लिए उपयोग किया जाता है. PHP भाषा मे इन == , != , > , < , >= , <= Comparison operators का उपयोग किया जाता है.

Operatos Description
== Equal to
=== Equal to and of the same type
!= Not equal to
!== Not equal to and of the same type
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Assignment Operators

Assignment operators को एक variable में value लिखने के लिए numeric values के साथ उपयोग किया जाता है. PHP भाषा मे इन = , += , -= , *= , /= , %= assignment operators का उपयोग किया जाता है.

Operatos Description
+= Add and assign
-= Subtract and assign
*= Multiply and assign
/= Divide and assign quotient
%= Divide and assign modulus
.= Concatenate and assign

Logical (or Relational) Operators

Logical operators logic perform करने के लिए उपयोग किया जाता है और ये operators control statements में भी प्रयोग किया जाता ह. PHP भाषा मे इन and , or , && , || , ! Logical operators का उपयोग किया जाता है.

Operatos Description
&& And
|| Or
! Not

Conditional Operators

PHP भाषा मे इन " ?: " (If Condition is true ? Then true value : Otherwise false value) conditional operators का उपयोग किया जाता है. Conditional operators को ternary operator भी कहा जाता है.

PHP in Hindi Functions

PHP मे Function का उपयोग Code के reduplication को कम करने के लिए किया जाता है . Function के उपयोग से Code की Clarity मे सुधार किया जाता है Function सरल Pieces मे Complex Problems को भी कम कर देता है.

Function Statement का एक Section होता है जिसका उपयोग किसी कार्यक्रम में बार-बार किया जा सकता है. यह Page के Load होने पर तत्काल Execute नहीं होता है इसे Function पर Call करके Execute किया जाता है.

Function Code का एक Piece है जो Parameter के रूप में Input लेता है और Processing के बाद एक Value Return देता है. PHP मे Function अन्य Programming Language के कार्यों के समान होती है इसको Declare करने के लिए Function Keywords की आवश्यकता होती है.

Function को Body मे Define या Execute किया जाता है, जो Curly bracket के बीच मे होता है .

PHP Language में हजारों built-in functions हैं.

Functions with Parameters

PHP आपको एक functions के अंदर अपने parameters को पास करने का विकल्प देता है जिससे आप ज्यादा parameters को पास कर सकते है .

<!DOCTYPE html>
<html>   
   <head>
      <title>Function with Parameters Example</title>
   </head>   
   <body>
   <body>      
      <?php
         function addNumbers($param1, $param2)
         {
            $total = $param1 + $param2;
            echo "Total of the two numbers is : $total";
         }         
         addNumbers(35, 60);     
      ?>      
   </body>
</html>

Passing Arguments by Reference

Functions को reference द्वारा arguments भेजना संभव है. इसका मतलब है कि variable के reference में functions के द्वारा variable की value को कापी के बजाय हेरफेर किया जाता है.

<!DOCTYPE html>
<html>   
   <head>
      <title>Passing Arguments by Reference Example</title>
   </head>   
   <body>     
      <?php
         $original = 30;
         function changeValue(&$param)
         {
            $param = 25;
         }    
         echo "Original Value is $original <br />";
 
         changeValue( $original );
         
         echo "Changed Original Value is $original <br />";        
      ?>     
   </body>
</html>

Dynamic Function

Function के नाम को variables के रूप में assign करना मुमकिन है और फिर इन variables को उसी तरह से व्यवहार करना चाहिए, जैसा आप function नाम ही करेंगे.

<!DOCTYPE html>
<html>   
   <head>
      <title>Dynamic Function Example</title>
   </head>   
   <body>     
      <?php
         function printText()
         {
            echo "You are calling printText function dynamically.";
         }
         $function_string = "printText";
         $function_string();        
      ?>     
   </body> 
</html>

Function Returning Value

एक function value या object के साथ संयोजन के साथ return statement का उपयोग करके एक value को वापस कर सकता है. Return function के निष्पादन को बंद कर देता है और value वापस कॉलिंग कोड को भेजता है.

<!DOCTYPE html>
<html>   
   <head>
      <title>Function Returning Value Example</title>
   </head>   
   <body>      
      <?php
         function addNumbers($param1, $param2)
         {
            $total = $param1 + $param2;
            return $total;
         }         
         $sum = addNumbers(46, 31);
         echo "Total of the two numbers is : $sum";        
      ?>      
   </body>
</html>

PHP in Hindi Strings

PHP मे String Characters का एक Sequence है जो कि Text को Stored और हेर फेर करने के लिए उपयोग किया जाता है. Strings को किसी Person, Place या किसी अन्य Character पर आधारित Value, Name, आदि को Stored करने के लिए उपयोग करते है. PHP मे एक Strings को Single या Double Quotation Marks के भीतर Text को Attachment करके बनाया जाता है.

Strings PHP का केवल एक Data Type ही नहीं है बल्कि string किसी भी Web Related Programming Language का मूल आधार होता है. PHP मे String को create करना और उनके साथ work करना बहुत ही simple है. PHP आपको Strings के साथ Work करने के लिए कुछ built in Functions Provide करती है.

PHP मे String को Specify करने के चार तरीके है .

  • single quoted

  • double quoted

  • heredoc syntax

  • newdoc syntax

Single Quoted String

हम एक single quote में text संलग्न करके PHP में स्ट्रिंग बना सकते हैं. यह PHP में string specify करने का सबसे आसान तरीका है.

<!DOCTYPE html>
<html>   
   <head>
      <title>Single Quoted String Example</title>
   </head>   
   <body>
   <body>      
      <?php
         $str='Hello text within single quote';  
         echo $str;     
      ?>      
   </body>
</html>

Double Quoted String

PHP में, हम string को double quote के भीतर text को attached करके भी specify कर सकते हैं. लेकिन escape sequences और variables को double quote PHP string का उपयोग करके व्याख्या की जाएगी.

<!DOCTYPE html>
<html>   
   <head>
      <title>Double Quoted String Example</title>
   </head>   
   <body>     
      <?php
         $str1="Hello text   
         multiple line  
         text within double quoted string";  
         $str2="Using double \"quote\" with backslash inside double quoted string";  
         $str3="Using escape sequences \n in double quoted string";  
         echo "$str1 <br/> $str2 <br/> $str3";         
      ?>     
   </body>
</html>

PHP in Hindi Form Handling

Handling Form किसी भी PHP Developer द्वारा किए गए सबसे सामान्य कार्यों में से एक है. क्योंकि सामान्य रूप से हम Form के द्वारा हमारे Web Application के साथ User Interaction को बनाते है.

PHP मे form-data को collect करने के लिए superglobals $_GET और $_POST का उपयोग किया जाता है.

<!DOCTYPE html>
<html>
   <head>
      <title>Form Handling Example</title>
   </head>
   <body>
      <h2>Form Handling Example</h2>
      <p>Please fill in this form and send us.</p>
      <form action="process-form.php" method="post">
         <p>
            <label for="inputName">Name:<sup>*</sup></label>
            <input type="text" name="name" id="inputName">
         </p>
         <p>
            <label for="inputEmail">Email:<sup>*</sup></label>
            <input type="text" name="email" id="inputEmail">
         </p>
         <p>
            <label for="inputSubject">Subject:</label>
            <input type="text" name="subject" id="inputSubject">
         </p>
         <p>
            <label for="inputComment">Message:<sup>*</sup></label>
            <textarea name="message" id="inputComment" rows="5" cols="30"></textarea>
         </p>
         <input type="submit" value="Submit">
         <input type="reset" value="Reset">
      </form>
   </body>
</html>

Capturing Form Data with PHP

किसी particular form field के value तक पहुंचने के लिए आप बहुत से superglobals variables का उपयोग कर सकते हैं.

Superglobal Description
$_GET Get method यानी (URL parameters) का उपयोग करके सभी field names और form द्वारा भेजी गई मान की एक सूची है.
$_POST Post method का उपयोग करके सभी field names और form द्वारा भेजी गई मान की एक सूची है. लेकिन इसमे data URL में दिखाई नहीं देता है .
$_REQUEST इसमें $ _GET और $ _POST variables के मान के साथ-साथ $ _COOKIE superglobal variable के मान भी शामिल होते है .

PHP in Hindi Sessions

PHP मे Sessions का उपयोग Server पर Information को Stored करने के लिए किया जाता है. Data एक Application मे सभी Pages पर उपलब्ध होता है एक Sessions कई Pages मे उपयोग की जाने वाली Information को रखने का एक Best तरीका है.

Session को आप एक Page से दूसरे Page पर Temporary रूप से Information को Stored और Pass करने के लिए उपयोग किया जाता है जब तक कि User Website को Close ना करे तब तक Cookie के द्वारा Information User के Computer मे Stored होती रहती है लेकिन Session की Information User के Computer पर Stored नहीं होती है.

Session प्रत्येक Visitors के लिए एक Unique Number बन कर और इस की Id के आधार पर Storage के लिए काम करता है . Session एक ही Webpage पर जाने के दौरान दो User के Data को एक दूसरे के साथ confused होने से रोकने में भी Help करता है.

PHP Session को शुरू करने के लिये session_start() function से Call करते है यह Function पहले Check करता है कि कोई Session पहले से शुरू हे कि नही अगर कोई पहले शुरू नही हुआ तो ये एक Session को शुरू कर देता है.

<?php
// PHP session start
session_start();
?>
<!DOCTYPE html>
<html>
   <head>
      <title>PHP session start</title>
   </head>
   <body>
      <?php  
        if($userid != '')
        {
            echo "User session is started.";
        }
        else
        {
            echo "User session is not started.";
        }
      ?>
   </body>
</html>

Destroy a PHP Session

सभी global session variables को हटाने और session को destroy करने के लिए session_unset () और session_destroy () का उपयोग करते है .

<!DOCTYPE html>
<html>   
   <head>
      <title>Destroy a PHP Session</title>
   </head>   
   <body>     
      <?php
         session_start();
         /* unset session variable */
 
         unset($_SESSION['user_id']);
 
         /* destroy all session variables */
 
         session_destroy();         
      ?>     
   </body>
</html>

PHP in Hindi Cookies

Cookie एक small text file है जो उपयोगकर्ता को computer पर small quantity में data को store करती है . इसको आम तौर पर username जैसे जानकारी का track रखने के लिए उपयोग किया जाता है . PHP के साथ, आप cookie value को create और retrieve कर सकते हैं।

Setting Cookies

Setcookie () function को PHP में cookie सेट करने के लिए उपयोग किया जाता है

<?php
   setcookie("username", "raja", time()+3600, "/","", 0);
   setcookie("name", "Waseem", time()+3600, "/", "",  0);
?>

Accessing Cookies

PHP cookies access करने के कई तरीके प्रदान करता है लेकिन सबसे आसान तरीका $ _COOKIE और $ HTTP_COOKIE_VARS variables का उपयोग करके हम cookies को access कर सकते है.

<!DOCTYPE html>
<html>
   <head>
      <title>Accessing Cookies Example</title>
   </head>
   <body>
      <?php
         echo $_COOKIE["name"]. "<br />";
         
         /* is equivalent to */
         echo $HTTP_COOKIE_VARS["name"]. "<br />";
         
         echo $_COOKIE["age"] . "<br />";
         
         /* is equivalent to */
         echo $HTTP_COOKIE_VARS["age"] . "<br />";         
      ?>     
   </body>
</html>

Deleting Cookies

किसी भी cookies को हटाना करने के लिए आपको cookies को उस date से सेट करना होगा जो पहले से ही setcookie () function में delete हो गई हो.

<?php
   setcookie("username", "raja", time()-3600, "/","", 0);
   setcookie("name", "Waseem", time()-3600, "/", "",  0);
?>

PHP in Hindi Arrays

Arrays एक complex variables है जो हमें एक से अधिक value को single variable के name से store करने की अनुमति देता हैं.

Arrays के उपयोग से Data को Quick और कुशलता अनुसार Stored करना आसन होता है. यह किसी भी Programming Language के लिए उपलब्ध अधिक उपयोगी Data Types मे से एक है.

Arrays Elements की Sorted List मे आसानी से Described किया जा सकता है . आप Array के भीतर उनकी Index Event का reference देकर अलग-अलग Elements तक आसानी से पहुंच सकते हो.

PHP मे सभी Array Supporting होते है लेकिन आप उन तक पहुंचने के लिए एक Numerical Index का उपयोग कर सकते है. Numerical Index के साथ एक Array को आमतौर पर एक Indexed Array भी कहा जाता है.

Arrays अलग-अलग 3 Type के होते है और हर एक Arrays की Value को ID C का उपयोग करते हुए Accessed किया जाता है.

  • Indexed Array

  • Associative Array

  • Multidimensional Array

Indexed Array

Indexed Array को Number से Represent करते है जो 0 से शुरू होता है. हम PHP Array में String, Number और Object को इकट्ठा कर सकते हैं.

<!DOCTYPE html>
<html>
   <head>
      <title>Indexed Array Example</title>
   </head>
   <body>
      <?php
         // Indexed Arrays Example  //
      
         $num_array = array( 1, 2, 3, 4, 5);
         
         foreach( $num_array as $val)
         {
            echo "Array value is $val <br />";
         }
      
         // store with index value
         $num_array[0] = "one";
         $num_array[1] = "two";
         $num_array[2] = "three";
         $num_array[3] = "four";
         $num_array[4] = "five";
      
         foreach( $num_array as $val)
         {
            echo "Array value is $val <br />";
         }        
      ?>
   </body>
</html>

Associative Array

Associative Array functionality की term में numeric arrays के समान होता है. लेकिन वे अपने index number के मामले में थोड़ा अलग हैं. Associative array को अपने index को string के रूप में रखना होता है जिससे index ताकि अपने key और value के बीच strong connection को कायम कर सके.

<!DOCTYPE html>
<html>   
   <head>
      <title>Associative Arrays Example</title>
   </head>   
   <body>     
      <?php
      // Associative Arrays Example  //
      
      $marks = array("raja" => 995, "waseem" => 770, "rani" => 855);
         
      echo "Total Marks gain by raja is ". $marks ['raja'] . "<br/>";
      echo "Total Marks gain by waseem is ". $marks ['waseem'] . "<br/>";
      echo "Total Marks gain by rani is ". $marks ['sita'] . "<br/>";
      
      /* Another method to store data in associative arrays */
      $ranks['raja'] = "First";
      $ranks['waseem'] = "Second";
      $ranks['rani'] = "Third";
      
      echo "Rank of raja is ". $ranks['raja'] . "<br/>";
      echo "Rank of waseem is ".  $ranks['waseem']. "<br/>";
      echo "Rank of rani is ".  $ranks['rani']. "<br/>";   
   ?>     
   </body>

Multidimensional Array

Multidimensional Array एक Array में जिसमें प्रत्येक Element भी Array होते है . और इसी तरह Multidimensional Array की Value को एक से अधिक Index मे उपयोग किया जाता है .

<!DOCTYPE html>
<html>   
   <head>
      <title>Multidimensional Array Example</title>
   </head>   
   <body>     
      <?php
      // Multidimensional Arrays Example //
      
      $marks = array( 
         "raja" => array
         (
            "physics" => 95,
            "maths" => 86,	
            "chemistry" => 75
         ),
         
         "waseem" => array
         (
            "physics" => 80,
            "maths" => 72,
            "chemistry" => 79
         ),
         
         "rani" => array
         (
            "physics" => 71,
            "maths" => 97,
            "chemistry" => 84
         )
      );
         
        
      echo "Marks for raja in physics : " ;
      echo $marks['raja']['physics'] . "<br/>"; 
      
      echo "Marks for waseem in maths : ";
      echo $marks['waseem']['maths'] . "<br/>"; 
      
      echo "Marks for rani in chemistry : " ;
      echo $marks['rani']['chemistry'] . "<br/>";
   ?>
   </body>
</html>

PHP in Hindi Loop Types

Loop का उपयोग बार-बार Code के एक ही Block को Execute करने के लिए किया जाता है जब तक Condition पूरी न हो जाए. समय और प्रयास को बचाने के लिए एक Program के भीतर दोहराए जाने वाले कार्य को Automatic Repetitive करने के लिये हम Loop चलाते है.

PHP 4 Type के Loop को Support करती है .

  • While Loop

  • Do...while Loop

  • For Loop

  • Foreach Loop

While Loop

while loop code के एक block को executes करता रहता है जब तक कि Specified condition true नहीं हो जाती है.

Syntax

while (condition)
{
  code that will be executed;
}

<!DOCTYPE html>
<html>
   <head>
      <title>While Loop Example</title>
   </head>
   <body>
      <?php
         $i = 5;   
         while( $i < 20 )
         {
            echo $i."<br/>";
            $i++;
         }
         echo "After while loop i value is $i";         
      ?>
   </body>
</html>

Do...While Loop

do...while loop हमेशा एक बार code के block को execute करता है और तब यह condition को check करता है और condition true होने तक loop को दोहराता है.

Syntax

do
{
   code to be executed;
}
while (condition);

<!DOCTYPE html>
<html>   
   <head>
      <title>Do While Loop Example</title>
   </head>   
   <body>     
      <?php
         $i = 0;   
         do
         {
            echo $i."<br/>";
            $i++;
         }
         while($i < 10);
         echo "After loop i value is $i";           
      ?>  
   </body>
</html>

For Loop

For loop का उपयोग विशिष्ट समय के लिए code set करने के लिए किया जा सकता है. जब आप पहले से जानते हैं कि script कितनी बार चलनी चाहिए .

Syntax

for (init; condition; increment)
{
   code to be executed;
}

For Example

<!DOCTYPE html>
<html>   
   <head>
      <title>For Loop Example</title>
   </head>   
   <body>     
      <?php
         for( $i = 10; $i < 20; $i++ )
         {
            echo $i."<br/>";
         }
         echo "After for loop i value is $i"; 
      ?>
   </body>
</html>

Foreach Loop

Foreach loop केवल arrays पर काम करता है, और array में सब key-value समरूप के माध्यम से loop के लिए उपयोग किया जाता है .

Syntax

foreach ($array as $value)
{
   code to be executed;
}

<!DOCTYPE html>
<html>   
   <head>
      <title>Foreach Loop Example</title>
   </head>   
   <body>     
      <?php
         $array = array( 1, 2, 3, 4, 5);
         
         foreach( $array as $value ) {
            echo "Value is $value <br />";
         }
      ?>
   </body>
</html>

PHP in Hindi Decision Making

PHP मे Decision Making Statements विभिन्न कार्यों के आधार पर विभिन्न प्रकार के कार्यों को करने के लिए उपयोग किया जाता है. इसमे if, elseif ...else और switch statements का उपयोग अलग condition मे फेसला लेने के लिए किया जाता है . आप अपने decision लेने के लिए code में conditional statements का उपयोग कर सकते हैं .

PHP Three Decision Making Statements को Supports करता है .

  • if...else statement

  • elseif statement

  • switch statement

If...Else Statement

If...Else Statement कुछ code को executes करता है यदि कोई condition true है और दूसरे code मे अगर वह condition false है तो हम if...else statement का उपयोग करते है .

<!DOCTYPE html>
<html>
   <head>
      <title>If...Else Statement Example</title>
   </head>
   <body>
      <?php
         $d = date("D");
         
         if ($d == "Fri")
            echo "Have a nice weekend!"; 
         
         else
            echo "Have a nice day!";         
      ?>
   </body>
</html>

ElseIf Statement

अगर आप कुछ code को executes करना चाहते हैं और कई conditions में से एक सही है तो आप elseif statement का उपयोग कर सकते है .

<!DOCTYPE html>
<html>   
   <head>
      <title>ElseIf Statement Example</title>
   </head>   
   <body>     
      <?php
		
      // ElseIf Statement Example //
      
      $d = date("D");
         
         if ($d == "Fri")
            echo "Have a nice weekend!";
         
         elseif ($d == "Sun")
            echo "Have a nice Sunday!"; 
         
         else
            echo "Have a nice day!";           
   ?>  
   </body>
</html>

Switch Statement

Switch statement का उपयोग execute किये जाने वाले code के कई block को select करने के लिए किया जाता है .

<!DOCTYPE html>
<html>   
   <head>
      <title>Switch Statement Example</title>
   </head>   
   <body>     
      <?php
         $d = date("D");
         
         switch ($d){
            case "Mon":
               echo "Today is Monday";
               break;
            
            case "Tue":
               echo "Today is Tuesday";
               break;
            
            case "Wed":
               echo "Today is Wednesday";
               break;
            
            case "Thu":
               echo "Today is Thursday";
               break;
            
            case "Fri":
               echo "Today is Friday";
               break;
            
            case "Sat":
               echo "Today is Saturday";
               break;
            
            case "Sun":
               echo "Today is Sunday";
               break;
            
            default:
               echo "Wonder which day is this ?";
         } 
   ?>
   </body>
</html>

PHP in Hindi File Uploading

PHP script का प्रयोग एक HTML form के साथ किया जा सकता है जिससे उपयोगकर्ता को server पर file upload करने की अनुमति मिल सके. Initially files को temporary directory में upload किया जाता है और फिर एक PHP script द्वारा destination पर transfer किया जाता है .

<!DOCTYPE html>
<html lang="en">
<?php
   if(isset($_FILES['fileImg'])){
      $errors= array();
      $fname = $_FILES['fileImg']['name'];
      $fsize =$_FILES['fileImg']['size'];
      $tmp_file =$_FILES['fileImg']['tmp_name'];
      $file_type=$_FILES['fileImg']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['fileImg']['name'])));
      
      $expensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$expensions)=== false){
         $errors[]="Please select a JPEG or PNG file only!";
      }
      
      if($fsize > 2097152){
         $errors[]='Max file size allowed is 2 MB only!';
		}
      
      if(empty($errors)==true){
         move_uploaded_file($tmp_file,"images/".$fname);
         echo "Success";
      }
      else{
         print_r($errors);
      }
   }
?>
<html>
   <head>
      <title>PHP File Uploading Example</title>
   </head>
   <body>      
      <form action="" method="POST" enctype="multipart/form-data">
         <input type="file" name="fileImg" />
         <input type="submit" value="Upload" />
			
      </form>
      <?php
          if(isset($_FILES['fileImg']['name'])){
             echo "Upload File Details";
         ?>
         <ul>
            <li>Sent file: <?php echo $_FILES['fileImg']['name'];  ?>
            <li>File size: <?php echo $_FILES['fileImg']['size'];  ?>
            <li>File type: <?php echo $_FILES['fileImg']['type'] ?>
         </ul>
         <?php
          }
         ?>
   </body>
</html>

PHP in Hindi File I/O

इस chapter मे हम file Input Output के बारे मे चर्चा करेंगे कि केसे PHP मे file को open, read, write और close कर सकते है .

  • Opening a file

  • Reading a file

  • Writing a file

  • Closing a file

Opening a file

PHP मे file को open करने के लिये fopen() function का उपयोग करते है . इस function के लिए two arguments होते है जो पहले file का नाम बताते हैं फिर उस mode में opreate होते हैं .

Opening file मे यह six file modes : r , r+ , w , w+ , a , a+ होते है

Reading a file

एक बार File को fopen() function का उपयोग करके खोला जाता है इसे fread() नामक function से read किया जा सकता है. इस function मे दो arguments की आवश्यकता होती है और ये file pointer और bytes मे file expressed की length के होना चाहिए.

Files की length Files filesize() function का उपयोग करके पाई जा सकती है जो file name को अपनी तर्क के रूप में लेती है और bytes मे व्यक्त file का size देती है.

<!DOCTYPE html>
<html>
   <head>
      <title>Reading a file Example</title>
   </head>
   <body>
      <?php
         $filename = "temp_file.txt";
         $filepoint = fopen( $filename, "r" );
         
         if( $filepoint == false )
         {
            echo ( "Error in opening file" );
            exit();
         }
         
         $filesize = filesize( $filename );
         $filetext = fread( $filepoint, $filesize );
         fclose( $filepoint );
         
         echo ( " The following is the content of the file <br/> " );
         echo ( " <pre> $filetext </pre> " );         
      ?>
   </body>
</html>

Writing a file

PHP मे fwrite() function का उपयोग करके एक new file को write किया जा सकता है या किसी मौजूदा file मे text को जोड़ा जा सकता है. इस function को दो arguments को एक file pointer और data के string को specify करने की आवश्यकता होती है जिससे यह write किया जाता है. वैकल्पिक रूप से एक तीसरी integer argument को लिखने के लिए data की length को specify करने के लिए शामिल किया जा सकता है. यदि तीसरा argument शामिल है तो specify length तक पहुंचने के बाद writing बंद हो जायगी . File को close करने के बाद file_exist() function का उपयोग कर इसकी मौजूदगी की पुष्टि होती है जो argument के रूप मे file नाम लेती है.

<?php
   $filename = "newfile.txt";
   $filepoint = fopen( $filename, "w" );   
   if( $filepoint == false )
   {
      echo ( "Error in opening new file" );
      exit();
   }
   fwrite( $filepoint, "The new file create and write text.\n" );
   fclose( $filepoint );
?>
<html>   
   <head>
      <title>Writing a file Example</title>
   </head>   
   <body>      
      <?php
         $filename = "newfile.txt";
         $filepoint = fopen( $filename, "r" );         
         if( $filepoint == false )
         {
            echo ( "Error in opening file" );
            exit();
         }
         
         $filesize = filesize( $filename );
         $filetext = fread( $filepoint, $filesize );
         
         fclose( $filepoint);
         
         echo ( " Total File size : $filesize bytes <br/> " );
         echo ( " Text in the file:: <br/> " );
         echo ( "  $filetext " );         
      ?>      
   </body>
</html>

Closing a file

PHP मे file को close करने के लिये fclose() function का उपयोग करते है .

<!DOCTYPE html> 
<html>   
   <head>
      <title>Closing a file Example</title>
   </head>   
   <body>      
      <?php
         echo "<p>Opening the file <b>tutorialsroot.txt</b> .....</p>";
         $myfile = fopen("tutorialsroot.txt", "r");
         if($myfile == false)
         {
            echo "Error occurred...exiting...";
            exit();
         }
         echo "<p>File opened successfully.</p>";
         echo "<p>Closing the file....</p>";
         fclose($myfile);
         echo "<p>File closed successfully.</p>"         
      ?>      
   </body>
</html>