menu

PHP Basic Programs for Beginners


1.

What is the output of the following PHP code snippet?

<?php
$i = 1;
do {
echo $i;
$i++;
} while ($i <= 5);
?>

12345

54321

1234

None of the above


2.

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


3.

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


4.

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


5. Which of the following is the correct way to include a PHP file in another PHP file?

include("myfile.php");

require("myfile.php");

include_once("myfile.php");

All of the above


6. 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


7.

What is the output of the following PHP code snippet?

<?php
$num1 = 10;
$num2 = 5;
if ($num1 > $num2) {
echo "$num1 is greater than $num2";
} else {
echo "$num2 is greater than $num1";
}
?>

10 is greater than 5

5 is greater than 10

10

None of the above


8. Which of the following is the correct way to check if a variable is not null in PHP?

if ($myVar == null) { ... }

if ($myVar === null) { ... }

if ($myVar != null) { ... }

None of the above


9.

What is the output of the following PHP code snippet?

<?php
$num = 7;
if ($num % 2 == 0) {
echo "Even";
} else {
echo "Odd";
}
?>

Even

Odd

7

None of the above


10. Which of the following is the correct way to declare a constant in PHP?

define("PI", 3.14);

const PI = 3.14;

PI = 3.14;

None of the above