Declaring & Calling Functions
Part of the Fundamentals section of Coddy's PHP journey — lesson 61 of 71.
So far, you've been writing code that runs from top to bottom. But as your programs grow, you'll find yourself repeating the same code in multiple places. Functions solve this problem by letting you group code into reusable blocks.
A function is a named block of code that performs a specific task. You define it once, then call it whenever you need that task done. In PHP, you declare a function using the function keyword:
<?php
function sayHello() {
echo "Hello, World!\n";
}
?>This creates a function named sayHello. The code inside the curly braces is the function body—it defines what happens when the function runs.
However, declaring a function doesn't execute it. To run the code inside, you need to call the function by using its name followed by parentheses:
<?php
function sayHello() {
echo "Hello, World!\n";
}
sayHello();
sayHello();
?>This outputs:
Hello, World!
Hello, World!Each time you call sayHello(), PHP executes the code inside the function. You can call a function as many times as you need, which is what makes functions so powerful for avoiding repetition.
Challenge
EasyRead a single line of input containing a number that indicates how many times to display a greeting (e.g., 3).
Declare a function called printWelcome that prints Welcome to PHP! (with a newline at the end).
Then call the function the specified number of times using a loop.
Example 1:
If the input is 3, the output should be:
Welcome to PHP!
Welcome to PHP!
Welcome to PHP!Example 2:
If the input is 1, the output should be:
Welcome to PHP!Example 3:
If the input is 5, the output should be:
Welcome to PHP!
Welcome to PHP!
Welcome to PHP!
Welcome to PHP!
Welcome to PHP!Cheat sheet
A function is a named block of code that performs a specific task. You define it once, then call it whenever needed.
Declare a function using the function keyword:
<?php
function sayHello() {
echo "Hello, World!\n";
}
?>Call a function by using its name followed by parentheses:
<?php
function sayHello() {
echo "Hello, World!\n";
}
sayHello(); // Outputs: Hello, World!
sayHello(); // Outputs: Hello, World!
?>Functions can be called multiple times to avoid code repetition.
Try it yourself
<?php
// Read the number of times to display the greeting
$count = intval(fgets(STDIN));
// TODO: Declare a function called printWelcome that prints "Welcome to PHP!" with a newline
// TODO: Call the function the specified number of times using a loop
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic7Arrays Part 2 - Associative
Intro to Associative ArraysAccessing Values by KeyModifying Values by KeyAdding New Key-Value PairsCheck if Key ExistsRecap - Key-Value Data Store10Functions
Declaring & Calling FunctionsFunction ParametersReturning ValuesDefault Parameter ValuesType Declarations for ParamsReturn Type DeclarationsVariable ScopeRecap - Creating Reusable Code2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators