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. Which of the following is the correct way to access a specific character in a string in PHP?

$myString[3]

$myString{3}

Both A and B

None of the above


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


4. Which of the following is the correct way to create a multidimensional array in PHP?

$myArray = array("red", "green", "blue");

$myArray = array(array(1, 2), array(3, 4));

$myArray = array(1, 2, 3 => array("a", "b", "c"));

None of the above


5. Which of the following is the correct way to create a multidimensional array in PHP?

$myArray = array("red", array("green", "blue"), "yellow");

$myArray = array("red", "green", "blue", "yellow");

$myArray = array(array("red", "green"), array("blue", "yellow"));

None of the above


6.

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


7.

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


8.

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


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


10. Which of the following is the correct way to add an element to the end of an array in PHP?

array_push($myArray, "newElement");

$myArray[count($myArray)] = "newElement";

$myArray[] = "newElement";

All of the above