如何解决PHP错误'注意:数组到字符串转换...' [英] How to solve PHP error 'Notice: Array to string conversion in...'
问题描述
我有一个试图回显 $ _ POST
的PHP文件,并且出现错误,下面是代码:
echo< html>;
echo< body>;
for($ i = 0; $ i <5; $ i ++){
echo< input name ='C []'value ='$ Texting [$ i]'。
style ='background-color:#D0A9F5;'>< / input>;
}
echo< / body>;
回显< / html>;
echo'< input type =submitvalue =Save The Tablename =G>< / input>'
这里是回显POST的代码。
if(!empty ($ _POST ['G'])){
echo $ _POST ['C'];
}
但是当代码运行时,我得到一个错误:
注意:数组到字符串转换在
C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php on line 8
这个错误是什么意思,我该如何解决它?
$ _ POST ['C']
>中的数组。所以,当你 echo
时,你正试图打印一个数组,所以它所做的只是打印 Array
和一个通知。 要正确打印一个数组,您可以循环遍历它并为每个元素
echo
,或者您可以使用 print_r
。 另外,如果你不知道它是一个数组还是一个字符串, var_dump($ var)
它会告诉你它是什么类型以及它的内容是什么。仅用于调试目的。
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?
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.
这篇关于如何解决PHP错误'注意:数组到字符串转换...'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!