181

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]' " . 
         "style='background-color:#D0A9F5;'></input>";

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

Here is the code to echo the POST.

if(!empty($_POST['G'])){
    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?

5

7 Answers 7

163

The error means you are trying to use an array as though it's a string.

For example,

$var = [42];
echo "The Answer to the Ultimate Question of Life is $var"; 

will cause this error. To avoid that, you have to address a specific array item, for example

echo "The Answer to the Ultimate Question of Life is $var[0]"; 

The same goes for nested arrays. 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.

0
108

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
Array

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:

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

Correction 1: use foreach loop to access array elements

http://php.net/foreach

$stuff = array(1,2,3);
foreach ($stuff as $value) {
    echo $value, "\n";
}

Prints:

1
2
3

Or along with array keys

$stuff = array('name' => 'Joe', 'email' => '[email protected]');
foreach ($stuff as $key => $value) {
    echo "$key: $value\n";
}

Prints:

name: Joe
email: [email protected]

Note that array elements could be arrays as well. In this case either use foreach again or access this inner array elements using array syntax, e.g. $row['name']

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

In case it's just a plain 1-demensional array, you can simply join all the cells into a string using a delimiter:

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

Correction 3: Stringify an array with complex structure:

In case your array has a complex structure but you need to convert it to a string anyway, then use http://php.net/json_encode

$stuff = array('name' => 'Joe', 'email' => '[email protected]');
print json_encode($stuff);

Prints

{"name":"Joe","email":"[email protected]"}

A quick peek into array structure: use the builtin php functions

If you want just to inspect the array contents for the debugging purpose, use one of the following functions. Keep in mind that var_dump is most informative of them and thus usually being preferred for the purpose

examples

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

Prints:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
array(3) {
  [0]=>
  int(3)
  [1]=>
  int(4)
  [2]=>
  int(5)
}
2
  • 2
    This should be the accepted answer. Helped me immensely. I also had to use json_encode with this answer. print_r ( json_encode ( $AuthData ) ); Result similar to this: [{"system_id":"61a694d0-3605-4502-952b-38d87b451a56","system_auth_id":"caa5906f-d9ae-4297-8e9f-5ea8d9ed8b51","system_lastauth_id":"ace681bb-48f5-4831-a23d-6608c696f264","system_rundate":"2019-04-27T22:46:07.090Z"}]
    – Andrew
    Commented May 31, 2019 at 18:13
  • 1
    Given this question is used as a canonical duplicate, I took the liberty to add some practical examples, as simply dumping the array contents is seldom being a goal. Commented Nov 20, 2020 at 12:34
7

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];

0
6

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
    
    if(!empty($_POST['C'])){
        echo $_POST['C'];
    }

  }catch(Exception $e){

    //handle the error message $e->getMessage();
  }
1
  • 3
    That would be a very bad idea. there is nothing to "handle" in this error. It must be fixed right away. Adding a try catch makes absolutely no sense here. Commented Nov 20, 2020 at 6:17
4
<?php
ob_start();
var_dump($_POST['C']);
$result = ob_get_clean();
?>

if you want to capture the result in a variable

1

this error happened to me in the following context: I was trying to do a mass allocation registration

Product::create($request->all());

I got the error right there, what was happening is that my model had the variable $table misspelled:

protected $table = ['product'];

As it should be:

protected $table = 'product';

I know it's something very specific, but I hope it helps someone.

0

You can also try this:

if(isset($_POST['G']) && isset($_POST['c']){
        for($i=0; $i< count($_POST['C']); $i++){
            echo $_POST['C'][$i];
        }
}
1
  • isset($_POST['G']) doesn't appear to be relevant in this context. Maybe !empty() would be a logical choice. This answer is missing its educational explantion. Commented Apr 20, 2023 at 22:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.