How to solve PHP error 'Notice: Array to string conversion in...'


I have a PHP file that tries to echo a $_POST and I get an error, here is the code:

echo "<html>";
echo "<body>";
for($i=0; $i<5;$i++){
    echo "<input name='C[]' value='$Texting[$i]' " . 

echo "</body>";
echo "</html>";
echo '<input type="submit" value="Save The Table" name="G"></input>'

Here is the code to echo the POST.

    echo $_POST['C'];

But when the code runs I get an error like:

Notice: Array to string conversion in 
C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php on line 8

What does this error mean and how do I fix it?

10/12/2017 3:11:52 PM

Accepted Answer

When you have many HTML inputs named C[] what you get in the POST array on the other end is an array of these values in $_POST['C']. So when you echo that, you are trying to print an array, so all it does is print Array and a notice.

To print properly an array, you either loop through it and echo each element, or you can use print_r.

Alternatively, if you don't know if it's an array or a string or whatever, you can use var_dump($var) which will tell you what type it is and what it's content is. Use that for debugging purposes only.

11/16/2013 10:43:40 AM

What the PHP Notice means and how to reproduce it:

If you send a PHP array into a function that expects a string like: echo or print, then the PHP interpreter will convert your array to the literal string Array, throw this Notice and keep going. For example:

php> print(array(1,2,3))

PHP Notice:  Array to string conversion in 
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(591) :
eval()'d code on line 1

In this case, the function print dumps the literal string: Array to stdout and then logs the Notice to stderr and keeps going.

Another example in a PHP script:

    $stuff = array(1,2,3);
    print $stuff;  //PHP Notice:  Array to string conversion in yourfile on line 3

You have 2 options, either cast your PHP array to String using an array to string converter or suppress the PHP Notice.

Correction 1: use the builtin php function print_r or var_dump: or

$stuff = array(1,2,3);
$stuff = array(3,4,5);


    [0] => 1
    [1] => 2
    [2] => 3
array(3) {

Correction 2: Use json_encode to collapse the array to json string:

$stuff = array(1,2,3);
print json_encode($stuff);   //Prints [1,2,3]

Correction 3: Joining all the cells in the array together:

    $stuff = array(1,2,3);
    print implode(", ", $stuff);    //prints 1, 2, 3
    print join(',', $stuff);        //prints 1, 2, 3

Correction 4: suppress the Notices:

print(array(1,2,3));    //Prints 'Array' without a Notice.

You are using <input name='C[]' in your HTML. This creates an array in PHP when the form is sent.

You are using echo $_POST['C']; to echo that array - this will not work, but instead emit that notice and the word "Array".

Depending on what you did with the rest of the code, you should probably use echo $_POST['C'][0];


Array to string conversion in latest versions of php 7.x is error, rather than notice, and prevents further code execution.

Using print, echo on array is not an option anymore.

Suppressing errors and notices is not a good practice, especially when in development environment and still debugging code.

Use var_dump,print_r, iterate through input value using foreach or for to output input data for names that are declared as input arrays ('name[]')

Most common practice to catch errors is using try/catch blocks, that helps us prevent interruption of code execution that might cause possible errors wrapped within try block.

  try{  //wrap around possible cause of error or notice

        echo $_POST['C'];

  }catch(Exception $e){

    //handle the error message $e->getMessage();

$result = ob_get_clean();

if you want to capture the result in a variable