0
<?php if(count($errors) > 0) : ?> <div> <?php foreach ($errors as $error) : ?> <p><?php echo $error ?></p> <?php endforeach ?> </div> <?php endif ?>

Am new to PHP, trying to make a website that let you register and when I want to display that user did not fill up forms this error appear, need a solution I know that count doesn't work for this from version 7.2 of PHP but i can't figure out how to get this to work

  • Welcome to Stack Overflow! Your question is unclear, without a clear and well thought out question you're unlikely to get a clear and well thought out answer. Please review the SO [guide on asking good questions](https://stackoverflow.com/help/how-to-ask), and have a quick read of [this blog post](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/) which contains a checklist of actions to take before asking a question. – Joundill May 12 '20 at 00:58
  • You can make $error with empty array if there are no errors. – Naveen K May 12 '20 at 01:46

1 Answers1

1

In PHP 7.2 you can do a check on your value before doing the count with is_iterable() function. Later in PHP 7.3 you also have the is_countable() function.

Try type casting your object to an array.

 if (count((array) $errors) > 0)
kopz
  • 738
  • 11
  • 20