0% complete

Functions · Lesson 1 of 9

Introduction to Functions

Package reusable behavior behind a named function.

Lesson

Functions give a behavior a name, accept inputs, and can return a result. They reduce duplication and provide a useful boundary for testing. Choose a name that describes the result or action, keep each function focused, and avoid hidden dependencies on mutable global state. This small function can be called from any part of the same PHP file after it is defined. Further guidance Treat a function as a small contract: name its inputs, state what it returns, and avoid hidden dependencies. Test a representative value and an edge case. Keep a function focused; callers should decide how to display a returned result or handle a reported failure. Practice task: Create a fresh example for Introduction to Functions. Predict the result before running it, then change one input and explain why the result changes. Test a normal case and one boundary or invalid case where it applies. Compare your output with the example and note one rule you would use in a real project.

Code examples

Introduction to Functions
PHP
function greeting(): string
{
    return 'Welcome to PHP';
}
echo greeting();
Expected output
Welcome to PHP

Key points

  • Package reusable behavior behind a named function.
  • Keep inputs, return values, and side effects explicit.
  • Prefer focused functions that are easy to test independently.

Your learning progress

Finish this lesson

Optional practice

PHP playground

This site does not execute learner-provided PHP code. Connect a secured PHP sandbox service before enabling code execution.

Execution is disabled. No code is sent to a server.