PHP find output programs (Arrays) | set 2

Find the output of PHP programs | Arrays | Set 2: Enhance the knowledge of PHP Arrays concepts by solving and finding the output of some PHP programs.
Submitted by Nidhi, on January 22, 2021

Question 1:

<?php
    $arr = array(
        10,
        5,
        23,
        19,
        25,
        64
    );
    
    do
    {
        for ($i = 0;$i < arrayLen($arr);$i++)
        {
            if ($arr[$i] % 2 == 0) echo $arr[$i] . ' ';
        }
        break;
    }
    while (pow(2, NULL));
    
    function arrayLen($arr)
    {
        $i = 0;
        while (1)
        {
            $A = is_int($arr[$i++]);
            if ($A == 0) break;
        }
    
        return ($i - 1);
    }
?>

Output:

10 64

Explanation:

In the above program, we created an array of integer elements, and we defined a function arrayLen(), which is used to count the total number of elements in the array. In the arrayLen() we check every element of the array, and checked it is an integer number, is_int() function will return 0, if it is not an integer number.

Now look apart from arraylen() function, here we used do…while() loop which is useless because we used break statement at the end of the body of do…while() loop, then it will execute only once.

Now look to the inner loop, here counter variable $i will move from 0 to 5 because arrayLen() function will return 6. Inside the loop, we check every element of the array, which is an "even number or not. If the number is even then we print those numbers on the webpage.

Question 2:

<?php
    $student = array(
        "Rahul" => "101",
        "Rohit" => "102",
        "Virat" => "103"
    );
    echo $student[1];
?>

Output:

PHP Notice:  Undefined offset: 1 in /home/main.php on line 7

Explanation:

The above program will generate a PHP notice because we created an associative array that cannot be accessed using an index.

Question 3:

<?php
    $student = array(
        "Rahul" => "101",
        "Rohit" => "102",
        "Virat" => "103"
    );
    echo $student["101"];
?>

Output:

PHP Notice:  Undefined offset: 101 in /home/main.php on line 7

Explanation:

The above program will generate PHP notice because in the defined associative array "101" is the value, and "Rahul" is the key and we can access the value using the key. The below statement will print "101".

echo $student["Rahul"];

Question 4:

<?php
    $student = array(
        "Rahul" => "101",
        "Rohit" => "102",
        "Virat" => "103"
    );
    foreach ($student as $key => $value)
    {
        echo $key . "=>" . $value;
        echo "<br>";
    }
?>

Output:

Rahul=>101
Rohit=>102
Virat=>103

Explanation:

In the above program, we created an associative array that contains keys and associated values. Then we used a "foreach" loop to access keys and values and then print them on the webpage.

Question 5:

<?php
    $TwoDArr = array(
        array(
            12.24,
            15.43,
            17.56
        ) ,
        array(
            10,
            20,
            30
        ) ,
        array(
            "ABC",
            "PQR",
            "XYZ"
        )
    );
    for ($i = 0;$i < 3;$i++)
    {
        for ($j = 0;$j < 3;$j++) echo $TwoDArr[$i][$j] . ' ';
        echo '<br>';
    }
?>

Output:

12.24 15.43 17.56
10 20 30
ABC PQR XYZ

Explanation:

In the above program, we defined two-d array, here we defined three nested arrays, then we accessed array elements with the help of nested loop, we accessed elements row-wise and print them on the webpage.





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.