MCQ

PHP MCQ – Multiple Choice Questions and Answers – Basics – Part 3

This collection of PHP Multiple Choice Questions and Answers (MCQs): Quizzes & Practice Tests with Answer focuses on “PHP basic”.
 

1. Which of the following statement will output $a on the screen?

A echo "\$a";

B echo "$$a";

C echo "/$a";

D echo "$a;";

A
A backslash ‘\’ is used in order to make the dollar sign as a normal string character rather than prompt PHP to treat $a as a variable. The backslash ‘\’ used in this way is known as escape character.
How to display PHP variable values with echo, print_r, and var_dumpHow to display PHP variable values with echo, print_r, and var_dumpIn this tutorial, we’re going to see some examples of how to display PHP variable values with echo(), print_r(), and var_dump().   Output the content…Read More
 

2. What is the result of the following PHP code?
<?php
    $total = "10 persons";
    $plus = 5;
    $total = $total + $plus;
    echo "$total";
?>

A 15

B Error

C 15 persons

D 10 persons

A
The integer value at the beginning of “10 persons” is used in the calculation. Despite that if it begins with anything but a numerical value, the value will be 0.

 

 

3. If $x = 10 what will be returned when ($x == 10) ? 2 : 4 is executed?

A 10

B 2

C 4

D Error

B
?: is known as ternary operator. If condition is true then the section just after the ? is executed else the section after : .

 

 

4. What is the result of the following PHP code?
<?php
    $name = "Emily";
    echo 'What is her name? \n She is $name';
?>

A

What is her name? \n She is $name

B

What is her name?
She is $name

C

What is her name? She is Emily

D

What is her name? 
She is Emily
A
When a string is surrounded within single quotes both variables and escape sequences will not be interpreted when the string is parsed.

 

 

5. What is the result of the following PHP code?
<?php

    $name = "Alex";

    switch ($name)
    {
        case "Emily":
            echo "Hello Emily. Welcome to StackHowTo!";

        case "Alex":
            echo "Hello Alex. Welcome to StackHowTo!";

        case "Bob":
            echo "Hello Bob. Welcome to StackHowTo!";
    }

?>

A Hello Bob. Welcome to StackHowTo!

B Hello Alex. Welcome to StackHowTo!

C Hello Alex. Welcome to StackHowTo!Hello Bob. Welcome to StackHowTo!

D Error

C
If a break declaration isn’t present, all next case blocks will execute until a break declaration is found.

 

 

6. Which statements will output “Welcome to StackHowTo!” on the screen?

A echo(“Welcome to StackHowTo!”);

B print(“Welcome to StackHowTo!”);

C printf(“Welcome to StackHowTo!”);

D sprintf(“Welcome to StackHowTo!”);

A, B, C
echo(), print() and printf() all can be applied to display a statement over the screen. The sprintf() statement is functionally same to printf() except that the result is assigned to a string instead of rendered to the browser.
How to display PHP variable values with echo, print_r, and var_dumpHow to display PHP variable values with echo, print_r, and var_dumpIn this tutorial, we’re going to see some examples of how to display PHP variable values with echo(), print_r(), and var_dump().   Output the content…Read More
 

7. What is the result of the following PHP code?
<?php
    $n = 1234;
    $arr = (array) $n;
    echo $arr[0];
?>

A 2

B 1

C Error

D 1234

D
The cast operator (array) is used to convert values from other data types to array.
Type casting in PHPType casting in PHPType Cast is the conversion of a variable of data type A into another data type B. For example, converting an integer variable into a…Read More
 

8. What is the result of the following PHP code?
<?php 
    $tmp = 'Alex';              
    $name = &$tmp;              
    $name = "My name is $name";  
    echo $name;
    echo $tmp;
?>

A ERROR

B My name is Alex Alex

C My name is AlexMy name is Alex

D My name is Alex

C
The $name = &$tmp; line will reference $tmp via $name.
PHP References When to Use Them, and How They WorkPHP References: When to Use Them, and How They WorkIf you pass a variable of a primitive data type (Boolean, Integer, Float, String, Array) to a function or assign it to another variable, then…Read More
 

9. What is the result of the following PHP code?
<?php
    $str = "Stack";
    $str .= "How";
    $str .= "To";
    
    echo "$str";
?>

A Stack

B How

C To

D StackHowTo

D
‘.’ is a concatenation operator. $str .= “How” is same as $str = $str.”How” where $str is having value of “Stack” in the previous statement and “To” in the next statement. So the result will be “StackHowTo”.

 

 

10. What is the result of the following PHP code?
<?php

    function countDown() 
    {
        static $count = 10;
        $count--;
        echo $count;
    }

    countDown();
    countDown();
    countDown();
?>

A 1098

B 987

C 789

D 101010

B
Because $count is static, it keeps its previous value each time the function is called.
PHP - Static VariablesPHP – Static VariablesIn this tutorial, we’re going to see how to use the static variables in functions in PHP. If the scope of a function is abandoned,…Read More
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *