Implode error for PHP

I have a form in which I have three checkboxes:

    <td>Wireless <input type="checkbox" name="services[]" value="wireless" /></td>
      </tr>
  <tr>
    <td>Cellular <input type="checkbox" name="services[]" value="cellular" /></td>
  </tr>
  <tr>
    <td>Security <input type="checkbox" name="services[]" value="Security" /></td>
<input type="submit" name="submit">

      

and then i fetch ($ _ POST) and have this code

$comServices = implode(",", $services);

      

but I am getting the error:

Warning: implode () [function.implode]: Invalid arguments passed ..

Does anyone know why I am getting this error?

+2


a source to share


3 answers


If none of your checkboxes are selected, $ services will be undefined and not an empty array.



You can do $comServices = implode(",", (array)$services);

to prevent it.

+13


a source


$services

will be empty if the checkbox is not checked (empty as in null

, not as in "empty array").

You will need to check if $ services is indeed an array:



if (is_array($services))
 $comServices = implode(",", $services)

      

+2


a source


This usually means that your variable is not an array ... You can check it with the is_array () function ...

0


a source







All Articles