Functions
A function is a reusable block of code, defined with function name(parameters) { ... }. It can accept input through parameters and send a result back with return. Once defined, it can be called by name as many times as needed.
Why this matters
- ·Avoids copy-pasting the same logic across a script
- ·Makes code easier to test and fix in one place
Common mistakes
- ·Forgetting return means the function gives back nothing (null), even if it echoes something
- ·Two functions with the same name in the same file cause a fatal error
Hello, Ayesha!
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Frequently asked questions
Use the code editor above edit the example freely and check the expected output against your own reasoning.
Practice questions
Question 1
Write a function called square($n) that returns the square of a number, then call it with 5.
PHP runs on a server, not in the browser this shows the expected output instead of a live one.
Quick check
Just for this topic answer honestly, then check yourself.
1 / 1
