Is it acceptable practice to use PHP's ternary operator to check for successful INSERT in database (or more generally, the return value of any function)? The PHP manual seems to be cautious on stacked ternary operators, and I have some embedded in my writeData
function.
#pseudocode variables to give context
$conn = new PDO("mysql:host=localhost;dbname=blah", $user, $pass);
$form = sanitize($_POST);
#writeData returns true on success, false if PDO exception
#do nothing on success, warn if otherwise
writeData($conn, $form) ? : exit("Problem writing data to database.");
Edit:
I actually try
& catch
in the writeData
function. I'm not yet outputting error pages. Assuming I write error page code and output it next in the catch
block of writeData
, is it best to do:
if(!writeData($conn, $form)) die();
(or alternatively the ternary version, which I kinda agree is hard to read especially in this case)