ExamVeda
Login
Home
1
Which one of the following is the right way of defining a function in PHP?
Discuss
Answer & Solution
Answer: Option D
No explanation is given for this question. Let's Discuss on Board
2
Type Hinting was introduced in which version of PHP?
Discuss
Answer & Solution
Answer: Option B
Solution:
Type hinting gives you the ability to force parameters to be objects of certain class or to be arrays. PHP 5 introduced this feature.
3
What will happen in this function call?
<?php
function calc($price, $tax)	
{
    $total = $price + $tax;
}
$pricetag = 15;
$taxtag = 3;
calc($pricetag, $taxtag);	
?>
Discuss
Answer & Solution
Answer: Option A
Solution:
When you pass an argument in the above manner or say we pass 15 and 3 directly, it is called passing by value or call by value.
4
What will be the output of the following PHP code?
<?php
function calc($price, $tax="")
{
    $total = $price + ($price * $tax);
    echo "$total"; 
}
calc(42);	
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
You can designate certain arguments as optional by placing them at the end of the list and assigning them a default value of nothing.
5
Which of the following are valid function names? 1. function() 2. €() 3. .function() 4. $function()
Discuss
Answer & Solution
Answer: Option A
Solution:
Except a) others are invalid names. According to the specified regular expression ([a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*), a function name like this one is valid.
6
What will be the output of the following PHP code?
<?php
function a()
{
    function b()
    {
        echo 'I am b';
    }
    echo 'I am a';
}
a();
a();
?>
Discuss
Answer & Solution
Answer: Option D
Solution:
This will be the output- I am a Fatal error: Cannot redeclare b()
7
What will be the output of the following PHP code?
<?php
function a()  
{
    function b()
    {
       echo 'I am b';
    }
    echo 'I am a';
}
b();
a();
?>
Discuss
Answer & Solution
Answer: Option C
Solution:
This will be the output- Fatal error: Call to undefined function b(). You cannot call a function which is inside a function without calling the outside function.
8
What will be the output of the following PHP code?
<?php
$op2 = "blabla";
function foo($op1)
    {
        echo $op1;
        echo $op2;
    }
foo("hello");
?>
Discuss
Answer & Solution
Answer: Option B
Solution:
The correct answer is Option B: Error

Here's why:

In PHP, variables defined outside of a function are not automatically accessible inside the function.

Scope is a crucial concept in programming.

The variable `$op2` is defined outside the function `foo()`.

Inside `foo()`, you are trying to use `$op2` without it being defined within the function's scope.

Therefore, PHP will throw an error (specifically, a notice about an undefined variable) when trying to access `$op2` within the function.

Although the `echo $op1` will work fine , but after that when trying to access the `$op2` that is not defined inside the function scope it will throw error .

To make `$op2` accessible inside the function, you would need to either:
1. Pass it as an argument to the function.
2. Use the `global` keyword to bring it into the function's scope.

Since neither of these is done in the given code, an error occurs.
9
A function in PHP which starts with ______ (double underscore) is know as.
Discuss
Answer & Solution
Answer: Option A
Solution:
Option A: Magic Function
In PHP, a function that starts with double underscores (__) is known as a magic function.
These functions are predefined in PHP and are automatically called when certain events or operations occur.
Examples include __construct, __destruct, __get, __set, and many others, which are automatically invoked when specific conditions are met in the code.

Option B: Inbuilt Function
Inbuilt functions in PHP are built into the language and are not necessarily prefixed with double underscores.
They are regular functions like echo(), print(), and other standard PHP functions, but do not follow the magic function naming convention.

Option C: Default Function
There is no specific category called "default function" in PHP.
This term is not commonly used in the context of PHP function naming or functionality.

Option D: User Defined Function
User-defined functions are those created by the developer, and they do not follow the double underscore convention.
These functions are typically defined with a name chosen by the user and are not prefixed with any special characters like the magic functions.

Conclusion:
The correct answer is Option A: Magic Function.
Magic functions in PHP start with double underscores and are automatically invoked by PHP under certain conditions.
10
What will be the output of the following PHP code?
<?php
function foo($msg)
{
    echo "$msg";
}
$var1 = "foo";
$var1("will this work");
?>
Discuss
Answer & Solution
Answer: Option D
Solution:
It is possible to call a function using a variable which stores the function name.