PHP Functions

The real power of PHP lies in its functions, which simplify repetitive tasks and enhance program organization. PHP provides more than 1000 built-in functions, and you can also create your own.

Tutorials dojo strip

PHP Built-in Functions

PHP includes a vast library of built-in functions that can be directly invoked from a script to perform specific tasks. Check out a PHP function reference guide for a detailed list of these tools.

PHP User-Defined Functions

In addition to the built-in functions, PHP allows you to create custom functions tailored to your needs. Here’s what you need to know about user-defined functions:

  • A function is a reusable block of code that can be invoked multiple times within a program.
  • Functions do not execute automatically when a page loads. Instead, they must be explicitly called.
  • To execute a function, you simply invoke it using its name.

Creating a Function

To declare a user-defined function, use the function keyword followed by its name.

Example: Display a Motorcycle Brand

PHP

Explanation:

  • The function showBrand() outputs “Welcome to Yamaha!”.
  • Function names must start with a letter or an underscore and are not case-sensitive.
  • Always assign meaningful names to your functions, reflecting their purpose.

Calling a Function

To execute a function, call its name followed by parentheses.

Example: Call the Function

PHP

PHP Function Arguments

Functions can accept data in the form of arguments, which operate like variables. Arguments are specified in the parentheses following the function name.

Example: Display Motorcycle Owners

PHP

Explanation:

  • The function motorcycleOwner($owner) uses an argument $owner to personalize the output.

Example: Multiple Arguments

PHP

Explanation:

  • This function accepts two arguments: $brand (e.g., “BMW”) and $year (e.g., “1916”), and outputs their combination.

PHP Default Argument Values

You can define default values for function arguments. If no value is provided during the function call, the default value is used.

Example: Setting a Default Height

PHP

Explanation:

  • When no argument is passed, $height defaults to 30.

PHP Functions – Returning Values

Functions can return values using the return statement.

Example: Calculate Total Mileage

PHP

Explanation:

  • The function calculates the total mileage by adding two numbers and returning the result.

Passing Arguments by Reference

By default, arguments are passed by value in PHP. Use the & symbol to pass arguments by reference, allowing modifications to the original variable.

Example: Adjust Price of a Motorcycle

PHP

Explanation:

  • The increasePrice() function updates the original $price variable directly.

Variable Number of Arguments

You can use the ... operator to allow functions to accept an unknown number of arguments, creating a variadic function.

Example: Calculate Total Sales

PHP

Explanation:

  • The ...$sales gathers all passed arguments into an array for easy processing.

PHP Strict Typing

PHP 7 introduced strict typing, enabling you to enforce data types for function arguments and return values. Add declare(strict_types=1); at the start of the file to enable strict mode.

Example: Enforcing Integer Arguments

PHP

Explanation:

  • The function enforces integer arguments, ensuring no unexpected data types are used.

Tutorials dojo strip