menu

PHP Basic Programs for Beginners


1.

What is the output of the following PHP code snippet?

<?php
$x = 5;
$y = 10;
function myFunction() {
global $x, $y;
$y = $x + $y;
}
myFunction();
echo $y;
?>

5

10

15

None of the above


2.

What is the output of the following PHP code snippet?

<?php
$myVar = true;
if ($myVar) {
echo "The variable is true";
} else {
echo "The variable is false";
}
?>

The variable is true

The variable is false

1

None of the above


3.

What is the output of the following PHP code snippet?

<?php
$num1 = 10;
$num2 = 5;
$result = $num1 + $num2;
echo "The result is " . $result;
?>

The result is 10

The result is 5

The result is 15

None of the above


4.

What is the output of the following code snippet?

<?php
$x = 5;
$y = 10;
$z = $x + $y;
echo "The sum is: $z";
?>

The sum is: 15

The sum is: $z

The sum is: 5 + 10

None of the above


5. Which of the following is the correct way to remove an element from the beginning of an array in PHP?

array_shift($myArray);

array_pop($myArray);

unset($myArray[0]);

All of the above


6. Which of the following is the correct way to loop through an associative array in PHP?

foreach ($myArray as $value) { ... }

foreach ($myArray as $key) { ... }

foreach ($myArray as $key => $value) { ... }

None of the above


7.

What is the output of the following code snippet?

<?php
$colors = array("red", "green", "blue");
echo $colors[1];
?>

red

green

blue

None of the above


8.

What is the output of the following PHP code snippet?

<?php
$myString = "Hello, world!";
echo substr($myString, 7);
?>

Hello

world!

Hello, w

None of the above


9.

What is the output of the following PHP code snippet?

<?php
$x = 5;
echo ++$x;
?>

5

6

5

None of the above


10.

What is the output of the following PHP code snippet?

<?php
$myArray = array("red", "green", "blue");
echo count($myArray);
?>

3

red, green, blue

Array

None of the above