menu

PHP Basic Programs for Beginners


1. Which of the following is the correct way to check if a string contains a specific substring in PHP?

if (strpos($myString, "world") !== false) { ... }

if (substr($myString, "world") !== false) { ... }

if ($myString contains "world") { ... }

None of the above


2.

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


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


4.

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


5.

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


6. Which of the following is the correct syntax to create a function in PHP?

function myFunction() {}

def myFunction() {}

func myFunction() {}

None of the above


7.

What is the output of the following PHP code snippet?

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

Hi, world!

Hello, world!

Hi

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 remove an element from the beginning of an array in PHP?

array_shift($myArray);

array_pop($myArray);

unset($myArray[0]);

All of the above


10.

What is the output of the following code snippet?

<?php
$a = "Hello";
$b = "World";
echo $a . " " . $b;
?>

HelloWorld

Hello World

Hello "World"

None of the above