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
$myString = "Hello, world!";
echo str_replace("Hello", "Hi", $myString);
?>

Hi, world!

Hello, world!

Hi

None of the above


3.

What is the output of the following PHP code snippet?

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

5

6

5

None of the above


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

function myFunction() {}

def myFunction() {}

func myFunction() {}

None of the above


5. Which of the following is the correct way to sort an array in ascending order in PHP?

sort($myArray, "ASC");

asort($myArray);

ksort($myArray);

None of the above


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


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


8. Which of the following is a valid PHP variable name?

$my_var

1var

my-var

my var


9.

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


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