PHP sizeof() alias of count() that returns the number of elements in an array or Countable object. The examples below show how the call behaves, where it is useful, and which limitations matter.
What Is sizeof() in PHP?
Alias of count() that returns the number of elements in an array or Countable object.
Its call structure is:
sizeof($value, [$mode])
The arguments identify the values or resources passed to sizeof(). Read the examples with attention to the type of each supplied argument and to whether the function modifies its input or returns a separate result.
Example 1: Basic Usage
This example introduces sizeof with a direct call and a small, readable input.
<?php
echo sizeof(["a", "b", "c"]);
Output:
3
Alias of count() that returns the number of elements in an array or Countable object. A string is not countable in PHP 8; sizeof() is not a string-length operation.
Example 2: Another Behavior or Input
Compare this input with the previous example: changing the data or option can affect how sizeof behaves.
<?php
$items = []; echo sizeof($items);
Output:
0
The changed input illustrates why output may differ with the chosen values or environment. A string is not countable in PHP 8; sizeof() is not a string-length operation.
Practical Example: Using the Feature
The following example incorporates sizeof into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.
<?php
$rows = [["id" => 1], ["id" => 2]]; echo "Rows: " . sizeof($rows);
Output:
Rows: 2
In a real application, check each operation result, validate values before use and clean up acquired resources. A string is not countable in PHP 8; sizeof() is not a string-length operation.
Important Behavior and Edge Cases
- Purpose: Alias of count() that returns the number of elements in an array or Countable object.
- Key limitation: A string is not countable in PHP 8; sizeof() is not a string-length operation.
- Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.
Comparison With Related Functions
| Function or Approach | Best Fit |
|---|---|
sizeof() | Alias of count() that returns the number of elements in an array or Countable object. |
count() | Canonical function for measuring arrays and Countable objects; sizeof() is an alias. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| A string is not countable in PHP 8; sizeof() is not a string-length operation. | The correct approach depends on this behavior: A string is not countable in PHP 8; sizeof() is not a string-length operation. |
| Ignoring errors or cleanup after the operation. | Validate inputs and check each result; close files, restore settings or release resources when the operation requires it. |
Key Takeaways
- Alias of count() that returns the number of elements in an array or Countable object.
- A string is not countable in PHP 8; sizeof() is not a string-length operation.
- Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.
sizeof is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.