PHP Foreach Loop

Foreach Loops in PHP in Hindi – PHP Foreach Loop

PHP

Foreach Loops in PHP in Hindi PHP Foreach Loop in Hindi:- PHP Foreach Kaam Karta Hain Arrays And Objects Ke Variable Par Or Agar Arrays And Objects Ke Alava Different Data Type Par uninitialized Variable Error Show Karta Hain |

PHP Loops 4 Type Ke Hote Hai |

  1. While loop
  2. Do-while loop
  3. For loop
  4. Foreach loop

Foreach Loops in PHP in Hindi

Foreach Loop Main Two Types Ke Syntax Hote Hain Jinhe Niche Dikhaya Gaya Hain |

Syntax:

foreach( $array as $element ) {
    // PHP Code to be executed
}

or

foreach( $array as $key => $element) {
    // PHP Code to be executed
}

Example:

<?php 

// Declare an array 
$arr = array("Apple", "Banana", "Gavava", "Graps"); 

// Loop through the array elements 
foreach ($arr as $element) { 
	echo "$element "; 
} 

?> 

Output:

Apple Banana Gavava Graps

Example 2:

Is Program Ham Array Ki Help Se Key And Value Print Karana Wale Hain Wo Bhi foreach Laga Kar |

<?php 
$emp = array( 
	"name" => "Danish", 
	"email" => "[email protected]", 
	"age" => 18, 
	"gender" => "male"

); 

// Loop through employee array 
foreach($emp as $key => $element) { 
	echo $key . ": " . $element . "<br>"; 
} 

?> 

Output:

name: Danish
email: [email protected]
age: 18
gender: male

Also Read

Leave a Reply

Your email address will not be published. Required fields are marked *