Error using function htmlspecialchars()

Hello,
I’m studying the book PHP & MySQL: Novice To Ninja, 5th Edition.
I am supposed to use this two functions to format output data :

function html($text)
{
  return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}

function htmlout($text)
{
  echo html($text);
}

I am using them this way in the template file:

<?php foreach ($authors as $author): ?>
 <li>
 <form action="" method="post">
 <div>
  <?php htmlout($author['name']); ?>
  <input type="hidden" name="id" value="<?php echo $author['id']; ?>">
  <input type="submit" name="action" value="Modify">
  <input type="submit" name="action" value="Delete">
 </div>
</form>
</li>
<?php endforeach; ?>

And this is the container file:

foreach ($result as $row)
{
    $authors[] = array('id' => $row['id'], 'name' => $row=['name']);
}

All this doesn’t work. /var/log/apache2/error.log throws this error:

PHP Warning: htmlspecialchars() expects parameter 1 to be string, array given in /var/www/html/includes/helpers.inc.php on line 4
the file menthioned is the first code I posted.

Thanks for your reply.

Is that code correct from the container file? Specifically the last part of this line:

$authors[] = array('id' => $row['id'], 'name' => $row=['name']);

There’s an equal-sign after the last $row and before the open-square-bracket.

it’s certainly not intended, but as of PHP 5.4 valid syntax (['name'] equals array('name'))

Ops!
Thank you, I looked over the code 3 times and I wasn’t able to see it !
It works correctly eliminating that equal-sign.

Thanks and bye

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.