PHP Questions and Answers – Functions – Part 1

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

1. Why should we use functions?

A Easily maintained

B Reusability

C Easier error detection

D All of the above

Functions provide reusability of code, easier to detect error, and easier to maintain.



2. How many categories of functions are there in PHP?

A 1

B 2

C 3

D 4

PHP allows us using two major categories of functions: Built-in functions and User Defined Functions.



3. Which function is not a built-in function in PHP ?

A fopen()

B print()

C addNumber()

D gettype()

addNumber() is not a built-in function in PHP.



4. How to define a function in PHP?

A function {function body}

B datatype functionName(parameters) {function body}

C functionName(parameters) {function body}

D function functionName(parameters) {function body}

PHP provids us a way to create a user-defined functions. Any name ending with an open and closed parenthesis () is a function. The keyword function is always used to start a function.



5. Type Hinting was provided in which version of PHP?



C PHP 5.3


With type hinting, we can specify the expected data type of an argument in a function declaration and as well as the return type.



6. A function always starts with the keyword _______

A def

B function

C fun

D None of the above

A function always starts with the keyword function.



7. A function’s name cannot begin with a _______.

A number

B underscore

C alphabet

D Both A and B

A function name cannot begin with a number. It can start with underscore or an alphabet.



8. A function name is not case-sensitive?

A False

B True

C Only user-defined function is case-sensitive

D None of the above

A function’s name is not case-sensitive.



9. What is the result of the following PHP code?

    function calculate($cost, $tax="")
        $total = $cost + ($cost * $tax);
        echo "$total"; 



A 0

B 5

C 25

D Error

You can specify some arguments as optional by puting them at the end of the list and assigning them a default value of nothing.



10. What is the type of function call used in line 10 in the following code?
    function add($n1, $n2)	
        $sum = $n1 + $n2;

    $n1 = 2;
    $n2 = 4;

    add($n1, $n2);	

A Call By Reference

B Call By Value

C Call By Address

D Default Argument Value

If we call a function by value, we actually transfer the values of the arguments which are kept or copied into the formal parameters of the function. As a result, the original values are untouched only the parameters inside the function changes.


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 *