Functions in PHP
Functions in PHP are blocks of code that can be reused. You define a function and then call it whenever you need it.
Defining a Function
To define a function in PHP, use the function keyword:
<?php
function greet($name) {
echo "Hello, $name!";
}
greet("John");
?>
Activity
Try It Yourself!
Write a function that takes two numbers as parameters and returns their sum.
Quiz
Quick Quiz
- What keyword is used to define a function in PHP?
- How do you call a function in PHP?
Answers: The function keyword; Call a function by writing its name followed by parentheses (e.g., greet()).