0

In PHP7 env, I have the follow code:

try {
    echo strlen([123]);
} catch (Exception $e) {
    echo 'error...';
}

Because [123] is not string, I want it to output error..., but it outputs:

Warning: strlen() expects parameter 1 to be string, array given

Why?

thinkerou
  • 1,781
  • 5
  • 17
  • 28
  • 2
    Because a warning is not an exception, much like how car wheel isn't Jupiter and similar. – Mjh Apr 20 '16 at 12:54
  • 1
    You need to add `declare(strict_types=1);` to the top of your file in order to make this an Error. (You'll have to catch TypeError or Error or Throwable instead of Exception,) – NikiC Apr 20 '16 at 14:22
  • OK,Thanks Mjh and NikiC. `Can I try/catch a warning? ` have had answers. – thinkerou Apr 21 '16 at 03:31

0 Answers0