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

Issue

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?

Solution

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.

Answered By – jadkik94

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published