"TypeError: implode(): Argument #2 ($array) must be of type ?array, string given" in PHP 8

"TypeError: implode(): Argument #2 ($array) must be of type ?array, string given" in PHP 8

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

Stefan Bogdanescu

Founder & Senior Architect

TypeError: implode(): Argument #2 ($array) must be of type ?array, string given in PHP 8

When upgrading a PHP project from version 7 to 8, you will frequently encounter strict type enforcement that previously went unnoticed. A common offender is the implode() function, which now requires its second argument to be explicitly an array (or null). If you pass a string instead, PHP throws a TypeError. This often stems from legacy code where developers assumed the input was always an array, relying on loose typing or fallbacks that no longer exist.

Consider the scenario where you are processing form submissions involving multiple selections. You have a checkbox field named vpn_network[] populated dynamically from a database query. The logic to handle this submission often looks like this:

$vpn1 = implode(',', $_POST['vpn_network'] ?? '');

At first glance, the null coalescing operator (??) seems robust enough. If $_POST['vpn_network'] does not exist, it defaults to an empty string ''. However, this is exactly what triggers the crash in PHP 8. The implode() function receives an empty string rather than an empty array, causing the fatal error: Argument #2 ($array) must be of type ?array, string given.

The core issue lies in the interaction between the null coalescing operator and the expected data type. When the key is missing from $_POST, the expression $_POST['vpn_network'] ?? '' evaluates to a string. While PHP 7 would silently convert this string into an array (or treat it incorrectly depending on context), PHP 8 rejects the mismatch outright.

To resolve this, you must ensure that the value passed to implode() is always an array before the function is called. There are several patterns to achieve this without introducing fragile logic.

Explicit Array Casting

The most direct fix is to cast the variable to an array using (array). This operation safely converts a string into an array containing a single element, or leaves an existing array unchanged. However, in your specific case where the fallback is an empty string, casting that string results in [''] (an array with one empty string).

$vpn_network = $_POST['vpn_network'] ?? [];
$vpn1 = implode(',', $vpn_network);

Here, we replace the empty string fallback with an empty array literal []. This guarantees that if the key is missing, implode() receives an empty list of values. The result of implode(',', []) is an empty string, which is the expected behavior for an unchecked form field.

Handling Mixed Types

If your codebase relies on logic where a single value should be treated as an array of one item (e.g., if you intended to support both single and multiple select fields under the same key), casting (array) alone might not be sufficient if the default fallback was meant to represent "no data." You must distinguish between "data provided" and "no data provided."

If you use $_POST['vpn_network'] ?? '', and the user submits the form without selecting any VPN, the variable becomes a string. Casting this specific empty string does not yield an empty array; it yields an array containing an empty string. To prevent populating your list with unwanted empty entries, the default fallback must be [] directly, not ''.

// Incorrect in PHP 8 context for implode
$vpn1 = implode(',', $_POST['vpn_network'] ?? ''); 

// Correct approach
$vpn1 = implode(',', $_POST['vpn_network'] ?? []);

Debugging the Source

Sometimes the error originates from how the data is being gathered before it reaches implode(). In your provided HTML snippet, the checkboxes use value="" . $row4['id']. If $row4['id'] contains unexpected data or if the loop logic fails to populate the array correctly in certain edge cases (such as an empty result set from myQuery), the resulting $_POST structure might be malformed.

Ensure that your database query returns results and that the loop iterates correctly. If SELECT * FROM vpn_networks returns no rows, the form submits without the key entirely. This is a common edge case where server-side validation is crucial. You might want to validate the presence of data before processing:

if (!empty($_POST['vpn_network'])) {
    $vpn1 = implode(',', $_POST['vpn_network']);
} else {
    $vpn1 = '';
}

While this adds a conditional check, it is often clearer and safer than relying on implicit type coercion. It makes the intent explicit: if no networks are selected, $vpn1 remains empty; otherwise, it contains the comma-separated list.

Conclusion on Type Safety

PHP 8's strict typing forces developers to be more deliberate about data structures. The pattern of defaulting missing keys to an empty string is a relic from looser PHP versions that no longer applies to functions expecting arrays. By adopting ?? [] for array contexts and validating inputs explicitly, you prevent runtime crashes and make your codebase more resilient across version upgrades.

Our network