PHP shuffle() randomly rearranges the values of an array in place and reindexes numeric keys. The examples below show how the call behaves, where it is useful, and which limitations matter.
What Is shuffle() in PHP?
Randomly rearranges the values of an array in place and reindexes numeric keys.
Its call structure is:
shuffle(&$array)
The argument is an array passed by reference, and shuffle() returns a success boolean while modifying that same array.
Example 1: Basic Usage
This example introduces shuffle with a direct call and a small, readable input.
<?php
$cards = ["A", "B", "C"]; shuffle($cards); print_r($cards);
Randomly rearranges the values of an array in place and reindexes numeric keys. Do not rely on preserved keys or use shuffle() for cryptographic selection.
Example 2: Another Behavior or Input
Compare this input with the previous example: changing the data or option can affect how shuffle behaves.
<?php
$numbers = [3, 1, 2]; shuffle($numbers); var_dump(array_keys($numbers));
The changed input illustrates why output may differ with the chosen values or environment. Do not rely on preserved keys or use shuffle() for cryptographic selection.
Practical Example: Using the Feature
The following example incorporates shuffle into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.
<?php
$quizOptions = ["blue", "red", "green"]; shuffle($quizOptions); foreach ($quizOptions as $option) { echo $option . "\n"; }
In a real application, check each operation result, validate values before use and clean up acquired resources. Do not rely on preserved keys or use shuffle() for cryptographic selection.
Important Behavior and Edge Cases
- Purpose: Randomly rearranges the values of an array in place and reindexes numeric keys.
- Key limitation: Do not rely on preserved keys or use shuffle() for cryptographic selection.
- 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 |
|---|---|
shuffle() | Randomly rearranges the values of an array in place and reindexes numeric keys. |
Random\Randomizer::shuffleArray() | Returns a shuffled array through a Randomizer instance; shuffle() changes its input array in place. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Do not rely on preserved keys or use shuffle() for cryptographic selection. | The correct approach depends on this behavior: Do not rely on preserved keys or use shuffle() for cryptographic selection. |
| 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
- Randomly rearranges the values of an array in place and reindexes numeric keys.
- Do not rely on preserved keys or use shuffle() for cryptographic selection.
- Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.
shuffle is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.