How to add a string value into a PHP array

How to add a string value into a PHP array

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Why array_push() Returns a Number and How to Fix It

When you encounter unexpected numeric output while manipulating PHP arrays, the culprit is almost always the return value of array_push(). The behavior you described—where inserting "one" into $array2 results in the integer 3 instead of modifying the array silently—is not a bug; it is a specific feature of the function designed for efficiency.

In your example, $push holds the new total count of elements in $array2, not the value you just pushed. Before the operation, $array2 contains two items ("test" and "test2"). When you execute array_push($array2, $value), PHP appends "one" to the end, making the collection size three. The function immediately returns this new length index (0-based, so the next insert would be at index 3). If your goal is simply to mutate the array and move on, capturing that return value is unnecessary noise.

The Correct Approach: Direct Mutation

To achieve the desired state where $array2 becomes ["test", "test2", "one"], you should ignore the return value of array_push(). In PHP, functions that modify arrays in place often return nothing (void) or specifically return the new count if you ask for it. Since you don't need the count here, just call the function directly:

$array = ["one", "two", "three"];
$array2 = ["test", "test2"];

foreach ($array as $value) {
    if ($value === 'one') {
        array_push($array2, $value);
        // Do not assign the return value to a variable
    }
}

// $array2 is now ['test', 'test2', 'one']
print_r($array2); 

By removing $push = ..., the script stops treating the array as a data source for metrics and treats it purely as a mutable container. This is the standard pattern when you only care about the side effect of adding an element.

Alternative Methods: [] Syntax and array_splice()

While array_push() works perfectly, relying on its return value can lead to confusion if you are not explicitly checking for it. PHP 5.4 introduced the shorthand array syntax using square brackets [], which behaves similarly but is often more readable when appending a single element:

if ($value === 'one') {
    $array2[] = $value;
}

This achieves the exact same result without the function call overhead. Some developers prefer this because it visually indicates "append" rather than "push to stack," even though PHP treats arrays as ordered maps regardless of their internal implementation details.

If you need to insert a value at a specific index rather than the end, array_push() is insufficient. In that case, array_splice() becomes necessary. Unlike array_push, which always appends, array_splice allows you to specify an offset:

// Inserts 'one' at index 10 if it doesn't exist, or shifts existing elements
array_splice($array2, 10, 0, ['one']);

This method is more verbose but essential when the target position isn't strictly the end of the array. Note that array_splice returns an array of the removed elements (empty in this case) and does not return a count, so confusion about its return value is less likely than with array_push.

Performance Considerations

For large-scale applications, the choice between these methods matters slightly. array_push() is implemented efficiently at the C level and is generally faster than using the [] syntax in tight loops because it avoids repeated parsing of the short-hand array expression. However, for a single conditional insertion like your example, the performance difference is negligible.

The real trade-off lies in code readability versus explicitness. If you are building a complex data processing pipeline where tracking the number of elements added is part of the logic (e.g., filling up a fixed-size buffer), capturing the return value of array_push() provides that information without needing a separate count() call later. But if you are just aggregating data, treating it as void is cleaner and reduces cognitive load.

Conclusion

The issue in your original snippet stemmed from misunderstanding what array_push() returns. It returns the new length of the array, not the value added. By simply omitting the assignment to $push, you get the exact behavior you intended: mutating $array2 to include "one" at the end. Whether you use array_push(), the [] shorthand, or array_splice(), the decision should depend on whether you need the element appended to the end, inserted elsewhere, or if you need to track the resulting array size.

Our network