How to solve PHP error 'Note: array for converting strings to ...'

I have a PHP file that tries to execute the $_POST echo code and I get an error message, 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 for the POST echo message.

 if(!empty($_POST['G'])){ echo $_POST['C']; } 

But when the code starts, I get an error, for example:

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

What does this error mean and how to fix it?

+46
html php
Nov 16 '13 at 10:38
source share
3 answers

When you have many HTML inputs named C[] , what you get in the POST array at the other end is an array of these values ​​in $_POST['C'] . Therefore, when you echo , you try to print an array, so all it does is print Array and a notification.

To print an array correctly, you either skip it or echo each element, or you can use print_r .

Alternatively, if you don’t know if it is an array or string or something else, you can use var_dump($var) , which will tell you what type it is and what it represents. Use this for debugging purposes only.

+54
Nov 16 '13 at 10:43
source share

How to reproduce the above Notice:

A message appears when you send an empty array to a function like: echo or print :

 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, echo and print will simply print Array to stdout and then write the notification to stderr.

In more detail you can do this in a php script:

Create a PHP array and try printing an empty array in stdout:

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

Correction 1: use the built-in php function print_r or var_dump:

http://php.net/manual/en/function.print-r.php

http://php.net/manual/en/function.var-dump.php

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

Print

 Array ( [0] => 1 [1] => 2 [2] => 3 ) array(3) { [0]=> int(3) [1]=> int(4) [2]=> int(5) } 

Correction 2: use json_encode to collapse the array into a json string:

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

Correction 3: Merging all the cells in the array:

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

Correction 4: notification suppression:

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

Why is this happening?

If you study the php documentation for print and echo,

http://php.net/manual/en/function.print.php

http://php.net/manual/en/function.echo.php

You will see that both take strings, not arrays. You pass the array as a string, in general, when you make such errors, PHP will not do anything or will do something very unexpected.

+15
Oct 14 '16 at 17:54
source share

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

You are using echo $_POST['C']; for the echo of this array - this will not work, but instead will publish this notification and the word "Array".

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

+5
Nov 16 '13 at 10:44
source share



All Articles