Q:

PHP find output programs (Arrays) | set 1

0

Find the output of PHP programs | Arrays | Set 1: Enhance the knowledge of PHP Arrays concepts by solving and finding the output of some PHP programs.

Question 1:

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

Question 2:

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

Question 3:

<?php
    $arr = array(
        10,
        5,
        23,
        19,
        25,
        64
    );
    
    echo arrayLen($arr);
    function arrayLen($arr)
    {
        $i = 0;
        while (1)
        {
            $A = is_int($arr[$i++]);
            if ($A == 0) break;
        }
        return $i;
    }
?>

Question 4:

<?php
    $arr = array(
        10,
        5,
        23,
        19,
        25,
        64
    );
    
    echo arrayLen();
    
    function arrayLen()
    {
        $i = 0;
        while (1)
        {
            $A = is_int($arr[$i++]);
            if ($A == 0) break;
        }
        return $i;
    }
?>

Question 5:

<?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 = isint($arr[$i++]);
            if ($A == 0) break;
        }
        return $i;
    }
?>

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Answer1:-

Output:

PHP Fatal error:  Uncaught Error: Call to undefined function length() 
in /home/main.php:12
Stack trace:
#0 {main}
  thrown in /home/main.php on line 12 

Explanation:

The above program will generate a compilation error because length() is not a built-in function. We need to use count() function to get the length of the specified array.

Answer 2:

Output:

10 64

Explanation:

In the above program, we created an array of integer elements. 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 count() 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.

Answer 3:

Output:

7

Explanation:

The above program will print 7 on the webpage. 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. Here we use a post-increment operator with $i, then it will reach till 7.

Answer 4:

Output:

1

Explanation:

The above program will print 1 on the webpage. 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, but $arr inside the function arrayLen() is different from the above-defined array. That's why it arrayLen() will return 1.

Answet 5:

Output:

PHP Fatal error:  Uncaught Error: Call to undefined function isint() 
in /home/main.php:26
Stack trace:
#0 /home/main.php(13): arrayLen(Array)
#1 {main}
  thrown in /home/main.php on line 26

Explanation:

The above program will generate syntax error because isint() is not a built-in function. is_int() function is used to check the specified variable is an integer number or not.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now