Current Location: Home> Latest Articles> Comprehensive Guide to PHP Function Parameter Data Types with Examples

Comprehensive Guide to PHP Function Parameter Data Types with Examples

M66 2025-07-28

Categories of Data Types Accepted by PHP Function Parameters

In PHP, function parameters can accept multiple data types. The most common include scalar types, composite types, and some special types. Understanding these types is essential for writing flexible and efficient functions.

Scalar Types

  • Integer (int): Numbers without decimal points, such as 123
  • Float (float): Numbers with decimal points, such as 3.14
  • String (string): A sequence of characters, such as "Hello"
  • Boolean (bool): Represents true or false, with values TRUE or FALSE

Composite Types

  • Array (array): A collection of elements, where elements can be of any type
  • Object (object): An instance of a class, containing properties and methods

Other Types

  • NULL: Indicates a variable has no value
  • Resource (resource): A reference to an external resource like a file handle or database connection

Practical Example: Using Multiple Data Types in PHP Function Parameters

function myFunction($name, $age, $hobby) {
  echo "Name: $name <br>";
  echo "Age: $age <br>";
  echo "Hobby: $hobby <br>";
}

$name = "John Doe";
$age = 30;
$hobby = "Programming";

myFunction($name, $age, $hobby);

Example Output

Name: John Doe
Age: 30
Hobby: Programming

This function demonstrates how to define a simple PHP function that accepts string and integer parameters and outputs the values passed to them. Mastering multiple data types for function parameters helps improve code flexibility and maintainability.