I have this code in my file:
if (is_array($_REQUEST))
$cepl=implode(' ', $_REQUEST);
every couple of days I get this warning in php log: PHP Warning: Array to string conversion in /file.php on line 76
line 76 is: $cepl=implode(' ', $_REQUEST);
I can't find out what is causing this warning?!
The definition of the
implodefunction is very roughly equivalent to this (this is just an illustration, not tested code):The key point is the line
$pieceAsString = (string)$piece;- in order to combine the elements of the array,implodehas to convert each of them in turn to strings.Now consider what happens if
$pieceslooks like this:At some point in our loop, we're going to have
$piece = ['two-a', 'two-b'], and try to convert it to a string - whoops!So, the warning comes about because inside your
$_REQUESTarray, there are other arrays. There's a couple of ways this can happen:$_REQUESTcan be written to directly. For instance, someone can write$_REQUEST['example'] = ['a', 'b'];/your-page.php?example[]=a&example[]=band$_REQUESTwill automatically be populated with['a', 'b'].This leads to a very important reminder: Never trust user input! Making any assumptions about what's in
$_REQUESTis very dangerous, because that input is under the user's control, and the user might not be your friend.