Functions in PHP

Built-in function (strlen): The length of the string "Hello, World!" is 13.

User-defined function (greet): Hello, Alice!

Explanation

Functions in PHP are blocks of code that can be repeatedly called to perform a specific task. PHP has built-in functions like strlen() for string length, and you can also define your own functions using the function keyword. User-defined functions can take parameters and return values, enhancing code reusability and organization.

Source Code

<?php
// Example of a built-in function
$string = "Hello, World!";
$string_length = strlen($string);
echo "<p>Built-in function (strlen): The length of the string \"$string\" is $string_length.</p>";

// Example of a user-defined function
function greet($name) {
    return "Hello, $name!";
}
$greeting = greet("Alice");
echo "<p>User-defined function (greet): $greeting</p>";
?>