Skip to content

Instantly share code, notes, and snippets.

@SrChach
Last active May 23, 2019 20:26
Show Gist options
  • Save SrChach/8dfd0a8e7de5c38fe22260faf13cbaa0 to your computer and use it in GitHub Desktop.
Save SrChach/8dfd0a8e7de5c38fe22260faf13cbaa0 to your computer and use it in GitHub Desktop.
Manage errors and success messages in php, check if values exists
<?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