How to check if a function returns error or not

Permalink
Say you pass the wrong data type to a function such as DateTime::createFromFormat,
You will get an error that stops the script.
Is there a way to instead check if the function returns an error or not then do something based on that?
Kind of how bash "if command" works.

ob7dev
 
mlocati replied on at Permalink Best Answer Reply
mlocati
DateTime::createFromFormat returns false if it fails, it doesn't throw any exception: see for instancehttp://3v4l.org/dUoem

Some function behaves differently, throwing an exception when they received invalid arguments. In this case you can use a try/catch block: see for instancehttp://3v4l.org/vWmCp

In any case, this kind of questions are not related to concrete5, so you may have better support in that by asking some generic PHP discussion group
ob7dev replied on at Permalink Reply
ob7dev
Oh, roger that. My mistake, I was trying to pass format immediately to DateTime whether it failed or not:
return DateTime::createFromFormat($startFormat[$type], $dateString)->format($newFormat[$type]);