I have a PHP file and I am trying to output a $_POST
, but I get an error, here is the code:
echo ""; echo ""; for($i=0; $i<5;$i ){ echo ""; } echo ""; echo ""; echo ''
Here is the code to output POST.
if(!empty($_POST['G'])){ echo $_POST['C']; }
But when the code runs, I get an error like this:
Notice: Array to string conversion in C:xampphtdocsPHISFinalSubmissionOfTheFormPHP.php on line 8
What does this error mean and how do I fix it?
The meaning of PHP Notice and how to reproduce it:
If you pass a PHP array to a function that expects a string, such as
echo
orprint
, then the PHP interpreter will convert your array to a literal stringArray
, throw this Notice and continue execution. For example:In this case, the function
print
outputs the literal stringArray
to stdout, then logs the Notice to stderr and continues execution.Another PHP script example:
Correction method 1: Use foreach loop to access array elements
http://php.net/foreach
Output:
Or contain array key name:
Output:
Note that array elements can also be arrays. In this case, you can use
foreach
again or use array syntax to access the inner array elements, like$row['name']
Correction method 2: Connect all cells in the array together:
If it is just an ordinary one-dimensional array, you can use the delimiter to concatenate all cells into a string:
Example:
Output:
When you have a lot of HTML input named
C[]
, what you get on the other end of the POST array is an array of those values$_POST['C']
. So when youecho
it, you are trying to print an array, so it will just printArray
and a hint.To properly print an array, you can loop through it and
echo
each element, or you can useprint_r
.Also, if you don't know if it's an array or a string or whatever, you can use
var_dump($var)
and it will tell you what type it is and what its contents are . For debugging purposes only.