PHP Fundamentals

Learn PHP programming from scratch, including variables, functions, arrays, and object-oriented programming.

beginner Backend Development 6 hours

Chapter 7: Functions

Chapter 7 of 15

Chapter 7: Functions

7.1 Defining Functions

Functions encapsulate reusable code.

function greet($name) {
    return "Hello, " . $name;
}

echo greet("John");  // Output: Hello, John

7.2 Function Parameters

function add($a, $b) {
    return $a + $b;
}

function greet($name = "Guest") {
    return "Hello, " . $name;
}

7.3 Built-in Functions

PHP provides many built-in functions.

  • String functions: strlen(), substr(), str_replace()
  • Array functions: count(), array_push(), array_pop()
  • Math functions: abs(), round(), rand()