Bird
Raised Fist0
PHPprogramming~10 mins

Function declaration and calling in PHP - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Function declaration and calling
Start
↓
Declare function
↓
Call function
↓
Execute function body
↓
Return result (optional)
↓
Continue main program
↓
End
First, you write a function with a name and code inside. Then you call it to run that code. The program runs the function's code and returns a result if given.
Execution Sample
PHP
<?php
function greet() {
  echo "Hello!\n";
}
greet();
?>
This code defines a function greet that prints Hello! and then calls it to show the message.
Execution Table
StepActionEvaluationOutput
1Declare function greetFunction greet is stored
2Call function greet()Function greet runsHello!
3Function greet endsReturn to main program
4Program endsNo more code
💡 Program stops after calling greet and printing Hello!
Variable Tracker
VariableStartAfter CallFinal
greet (function)DeclaredRunningFinished
Key Moments - 2 Insights
Why doesn't the function run when it is declared?
Declaring a function only tells the program what it is. It runs only when you call it, as shown in step 2 of the execution table.
What happens if we call the function multiple times?
Each call runs the function code again. The execution table shows one call, but calling greet() again would print Hello! again.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when greet() is called?
Agreet
BNothing
CHello!
DError
💡 Hint
Check Step 2 in the execution table where the output is shown.
At which step does the function greet finish running?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the execution table row describing function end.
If we remove the call greet(), what happens?
ANothing is printed
BHello! is printed twice
CHello! is printed once
DSyntax error
💡 Hint
Refer to the variable_tracker and execution_table showing function runs only when called.
Concept Snapshot
PHP function declaration:
function name() { code }

Calling function:
name();

Function code runs only when called.
Use echo inside to print output.
No output if not called.
Full Transcript
In PHP, you first declare a function by writing function name() and putting code inside curly braces. This tells the program what the function does but does not run it yet. When you want to run the function, you call it by writing its name followed by parentheses, like greet(). The program then runs the code inside the function. In the example, greet prints Hello! when called. The execution table shows the function is declared first, then called, which prints Hello!, then the function ends and the program finishes. Variables track the function state from declared to running to finished. Beginners often wonder why the function does not run when declared; it only runs when called. Also, calling the function multiple times runs the code each time. If you remove the call, nothing prints because the function code never runs.

Practice

(1/5)
1. What is the correct way to declare a function named greet in PHP?
easy
A. function greet() { }
B. def greet() { }
C. func greet() { }
D. function: greet() { }

Solution

  1. Step 1: Recognize PHP function declaration syntax

    In PHP, functions are declared using the keyword function followed by the function name and parentheses.
  2. Step 2: Match the correct syntax

    function greet() { } uses function greet() { }, which is the correct PHP syntax for declaring a function named greet.
  3. Final Answer:

    function greet() { } -> Option A
  4. Quick Check:

    PHP function declaration = function greet() { } [OK]
Hint: Use 'function' keyword to declare functions in PHP [OK]
Common Mistakes:
  • Using 'def' instead of 'function'
  • Adding a colon after 'function'
  • Using 'func' keyword which is not valid in PHP
2. Which of the following is the correct way to call a function named sayHello in PHP?
easy
A. call sayHello();
B. sayHello();
C. sayHello;
D. callFunction(sayHello);

Solution

  1. Step 1: Understand PHP function call syntax

    In PHP, to call a function, you write the function name followed by parentheses, optionally with arguments inside.
  2. Step 2: Identify the correct option

    sayHello(); uses sayHello();, which is the correct way to call the function named sayHello.
  3. Final Answer:

    sayHello(); -> Option B
  4. Quick Check:

    Function call syntax = sayHello(); [OK]
Hint: Call functions by name with parentheses () [OK]
Common Mistakes:
  • Using 'call' keyword which does not exist in PHP
  • Omitting parentheses when calling functions
  • Using incorrect function call syntax like callFunction()
3. What will be the output of the following PHP code?
function add($a, $b) {
    return $a + $b;
}
echo add(3, 4);
medium
A. 7
B. 34
C. add(3, 4)
D. Error

Solution

  1. Step 1: Understand the function behavior

    The function add takes two parameters and returns their sum.
  2. Step 2: Calculate the return value

    Calling add(3, 4) returns 3 + 4 = 7, which is then printed by echo.
  3. Final Answer:

    7 -> Option A
  4. Quick Check:

    3 + 4 = 7 [OK]
Hint: Functions return values; echo prints the result [OK]
Common Mistakes:
  • Concatenating numbers as strings instead of adding
  • Expecting function name to print instead of return value
  • Syntax errors from missing semicolons
4. Identify the error in the following PHP code:
function greet() {
    echo "Hello!";
}
greet;
medium
A. Missing semicolon after function declaration
B. Function name is incorrect
C. Missing parentheses when calling the function
D. Echo statement syntax error

Solution

  1. Step 1: Check function call syntax

    The function greet is declared correctly, but when calling it, parentheses are missing.
  2. Step 2: Understand PHP function call requirements

    In PHP, functions must be called with parentheses, even if they take no arguments. So greet; is invalid; it should be greet();.
  3. Final Answer:

    Missing parentheses when calling the function -> Option C
  4. Quick Check:

    Function calls need () even if no arguments [OK]
Hint: Always use () to call functions, even if empty [OK]
Common Mistakes:
  • Calling functions without parentheses
  • Confusing function declaration with call
  • Missing semicolons elsewhere
5. Consider the following PHP code:
function multiply($x, $y = 2) {
    return $x * $y;
}
echo multiply(5);

What will be the output and why?
hard
A. 0 because default value is ignored
B. 5 because only one argument is passed
C. Error because both parameters are required
D. 10 because the second parameter uses default value 2

Solution

  1. Step 1: Understand default parameter usage

    The function multiply has two parameters, where $y has a default value of 2.
  2. Step 2: Analyze the function call

    Calling multiply(5) passes 5 as $x, and since $y is not provided, it uses the default 2.
  3. Step 3: Calculate the result

    The function returns 5 * 2 = 10, which is printed by echo.
  4. Final Answer:

    10 because the second parameter uses default value 2 -> Option D
  5. Quick Check:

    5 * 2 = 10 with default parameter [OK]
Hint: Default parameters fill missing arguments automatically [OK]
Common Mistakes:
  • Assuming all parameters must be passed
  • Ignoring default values
  • Expecting error when missing arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard