Skip to content

Instantly share code, notes, and snippets.

@codeasashu
Created November 17, 2016 19:11
Show Gist options
  • Save codeasashu/08b37f082ca8ced4e2e0ec45a34b1e92 to your computer and use it in GitHub Desktop.
Save codeasashu/08b37f082ca8ced4e2e0ec45a34b1e92 to your computer and use it in GitHub Desktop.
PHP debug checklist
<?php
/**
* File to remind myself what things may come handy
* while I get stuck somewhere and don't have idea
* why things aren't working. These tricks may help you
* know the cause and solve the issue
**/
//Checklist 1: Sometimes, your json_encode may return null or false.
//So to know if there is some issue in json_encode itself, use this switch below
$a = json_encode($b); //or $a = json_encode($b, JSON_UNESCAPED_UNICOD);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
/**
* Remember when you want to output something on page, it must be properly encode and escaped.
* Sometimes, when your output contains unicode characters and if you haven't encoded it properly,
* it will not output anything, leaving you all confused and less hairs on your head.
**/
$html_content = htmlentities(stripslashes(utf8_encode($html_content)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment