Implode(): invalid arguments passed

posted 5 min read

Today, we're going to discuss a common error in php, "Implode(): invalid arguments passed". It occurs whenever we pass in invalid arguments to the implode() function. In the next few sections, we'll explore what is "Implode(): invalid arguments passed", why it occurs and how to resolve it.

What is "Implode(): invalid arguments passed" #

First, let's understand what's implode() function. It requires an input array and an optional string separator to work. It will return a string which concatenate all the elemenets in the array orderly with the separator between each element. Let's see:


<?php
$elements = ["Today", "is", "Friday"];
$txt = implode(" ", $elements);
var_dump($txt);
?>
  

This is the result:


string(15) "Today is Friday"
  

Look, it is a string text of length 15 and each element in the array is separated by a space.

Tip If you didn't provide a separator, the default separator is empty string ("").

Let's see another example:


<?php
$elements = "Today is Friday";
$txt = implode(" ", $elements);
var_dump($txt);
?>
  

This is the result:


NULL

PHP Warning:  implode(): Invalid arguments passed in HelloWorld.php on line 3
  

You may notice that the result is NULL because there's error during runtime and hence the variable, $txt is assigned a NULL value. But why PHP Warning: implode(): Invalid arguments passed happens? Let's move to the next section.

Causes #

The reason behind this error is we didn't pass in an input array as argument to implode() function. This function will only accept an input array to work. Let's see another example:


<?php
$elements = 3;
$txt = implode(" ", $elements);
var_dump($txt);
?>
  

This is the result:


NULL

PHP Warning:  implode(): Invalid arguments passed in HelloWorld.php on line 3
  

Even you pass in an integer value, the result is still the same, because it's not an array. So, how to resolve it? Let's see the next section.

Solution #

You may guess the solution easily. Yes, it's passing in an array argument to implode() function.


<?php
$elements = array("Hello","World");
$txt = implode(" ", $elements);
var_dump($txt);
?>
  

This is the result:


string(11) "Hello World"
  
Note array() function can generate an array from its several number of arguments. For deep understanding on how to manipulate an array using array() function, you may visit https://www.php.net/manual/en/function.array.php.

You may even pass in an empty array:


<?php
$elements = [];
$txt = implode(" ", $elements);
var_dump($txt);
?>
  

This is the result:


string(0) ""
  

You'll notice that this error will never occur as long as you're passing an input array. Pretty simple right?

The Conclusion #

In this study blog, we've understood what's "Implode(): invalid arguments passed", why it occurs and how to simply resolve it. Remember, always pass in array argument to implode() function to omit this error. Hope it helps you and have a nice day! :)

The Reference #

For further information, please visit:

  1. implode(): https://www.php.net/manual/en/function.implode.php
  2. array(): https://www.php.net/manual/en/function.array.php

More Posts

Understanding Basic Data Structures for Web Development

MasterCraft - Feb 16

Index was outside the bounds of the array

VGR - May 21

Actual and formal argument lists differ in length

juhist - May 14

How I Built My Own Laravel Analytics Package (and Almost Didn't Crash Production)

oleant - Jun 3

phpser: a fast, secure binary serializer for PHP cache workloads

ilia - Jun 2
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!