Home »
PHP »
PHP find output programs
PHP find output programs (Arrays) | set 1
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.
Submitted by Nidhi, on January 22, 2021
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));
?>
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.
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));
?>
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.
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;
}
?>
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.
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;
}
?>
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.
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;
}
?>
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.