What is the purpose of a function in PHP?
A
To store data B
To group reusable code blocks C
To handle loops D
To create arrays
Analysis & Theory
Functions are used to group code that can be reused multiple times.
Which keyword is used to define a function in PHP?
A
method B
define C
function D
fun
Analysis & Theory
The `function` keyword is used to define a function in PHP.
What is the output?
```
<?php
function greet() {
echo "Hello!";
}
greet();
?>
```
A
Nothing B
Hello! C
Error D
greet
Analysis & Theory
The function `greet()` is called, and it outputs `Hello!`.
Can a PHP function return a value?
A
No B
Yes, using the `return` statement C
Only using echo D
Only using print
Analysis & Theory
Functions can return values using the `return` statement.
What is the output?
```
<?php
function add($a, $b) {
return $a + $b;
}
echo add(3, 4);
?>
```
A
7 B
34 C
Error D
add
Analysis & Theory
The function `add()` returns the sum `3 + 4`, which is `7`.
How are parameters passed to functions in PHP by default?
A
By reference B
By value C
By copy D
By pointer
Analysis & Theory
By default, parameters in PHP are passed by value.
Which symbol is used to pass arguments by reference in PHP functions?
A
& B
* C
@ D
#
Analysis & Theory
The `&` symbol before a parameter passes it by reference, allowing the function to modify it.
What is the output?
```
<?php
function test() {
return "Test Successful";
}
$result = test();
echo $result;
?>
```
A
Nothing B
Test Successful C
Error D
result
Analysis & Theory
The function returns `Test Successful` which is stored in `$result` and printed.
Can functions in PHP have default parameter values?
A
No B
Yes C
Only in classes D
Only in loops
Analysis & Theory
Yes, PHP functions can have default parameter values like `function greet($name = 'Guest')`.
Is it possible to define a function inside another function in PHP?
A
Yes B
No C
Only in loops D
Only in classes
Analysis & Theory
Yes, PHP supports nested functions. Inner functions are not available until the outer function is called.