A function in PHP is a reusable block of code that performs a specific task. You define a function once, and you can use (or "call") it multiple times throughout your program.
<?php function greet() { echo "Hello, World!"; } greet(); // Calling the function ?>
You can pass values (parameters) to a function for it to use.
<?php function greetUser($name) { echo "Hello, $name!"; } greetUser("Alice"); // Output: Hello, Alice! ?>
Functions can return values instead of printing them directly.
<?php function add($a, $b) { return $a + $b; } $sum = add(5, 3); echo $sum; // Output: 8 ?>
You can set default values for parameters if not provided during function call.
<?php function greetUser($name = "Guest") { echo "Hello, $name!"; } greetUser(); // Output: Hello, Guest! ?>
function
keywordstrlen()
, strtolower()
, array_push()
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!