PHP Functions
Use this lesson when you want to understand the key concepts behind PHP Functions.
The real power of PHP comes from its functions.
PHP has more than 1000 built-in functions, and in addition, you can create your own custom functions to make your code modular and reusable.
A user-defined function declaration starts with the word function.
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // Call the function
?>
Information can be passed to functions through arguments. An argument is just like a variable.
If you want a function to return a computed value back to you instead of printing it directly, use the return statement.
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}
echo "5 + 10 = " . sum(5, 10);
?>