Last active
May 23, 2019 20:26
-
-
Save SrChach/8dfd0a8e7de5c38fe22260faf13cbaa0 to your computer and use it in GitHub Desktop.
Manage errors and success messages in php, check if values exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// print_if_not_failed sólo afecta si la operación fué exitosa | |
function json_check_print($mensaje_posible_error = 'error', $variable = null, $print_if_not_failed = false, $json = false){ | |
$print = [ | |
'error' => null | |
]; | |
if($json) | |
header('Content-Type: application/json'); | |
$failed = $variable === null || $variable === false; | |
if($failed) | |
$print['error'] = $mensaje_posible_error; | |
else | |
$print['content'] = $variable; | |
if( (!$failed && $print_if_not_failed) || $failed ){ | |
echo json_encode($print); | |
die(); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment