0% found this document useful (0 votes)
15 views

Array 3

The document contains PHP code examples demonstrating the use of arrays, loops, and functions. It shows numeric, associative, and multidimensional arrays output with foreach loops. It also contains examples of while and do-while loops and explains the difference between them. Finally, it defines a function to calculate division that checks for division by zero and outputs an example function call.

Uploaded by

milad.gulzad786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Array 3

The document contains PHP code examples demonstrating the use of arrays, loops, and functions. It shows numeric, associative, and multidimensional arrays output with foreach loops. It also contains examples of while and do-while loops and explains the difference between them. Finally, it defines a function to calculate division that checks for division by zero and outputs an example function call.

Uploaded by

milad.gulzad786
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

<?

php

$numericArray = [1, 2, 3, 4, 5];

?>

<ul>

<?php foreach ($numericArray as $value): ?>

<li><?php echo $value; ?></li>

<?php endforeach; ?>

</ul>

<?php

$associativeArray = ['Name' => 'John', 'Age' => 25, 'City' => 'New York'];

?>

<dl>

<?php foreach ($associativeArray as $key => $value): ?>

<dt><?php echo $key; ?></dt>

<dd><?php echo $value; ?></dd>

<?php endforeach; ?>

</dl>
<?php

$multidimensionalArray = [

[1, 2, 3],

[4, 5, 6],

[7, 8, 9]

];

?>

<ul>

<?php foreach ($multidimensionalArray as $subArray): ?>

<li>

<ul>

<?php foreach ($subArray as $value): ?>

<li><?php echo $value; ?></li>

<?php endforeach; ?>

</ul>

</li>

<?php endforeach; ?>

</ul>

// While loop example

int i = 0;

while (i < 5) {

System.out.println(i);

i++;

// Do-while loop example

int j = 0;
do {

System.out.println(j);

j++;

} while (j < 5);

$colors = ['red', 'green', 'blue'];

// Foreach loop example

foreach ($colors as $color) {

echo $color . "\n";

The main difference between a while loop and a do-while loop is in when the loop condition is
checked:while loop: In a while loop, the condition is checked before the loop body is executed. If the
condition evaluates to false initially, the loop body will not be executed at all.do-while loop: In a do-
while loop, the condition is checked after the loop body is executed. This means that the loop body is
guaranteed to be executed at least once, even if the condition evaluates to false on the first iteration

<?php

function calculateDivision($numerator, $denominator) {

if ($denominator != 0) {

return $numerator / $denominator;

} else {

return "Division by zero is undefined.";

}
// Calling the function

$numerator = 10;

$denominator = 2;

$result = calculateDivision($numerator, $denominator);

echo "The division of $numerator by $denominator is: $result";

?>

You might also like