Skip to content

Instantly share code, notes, and snippets.

@crazyyy
Last active June 26, 2021 20:21
Show Gist options
  • Save crazyyy/e84e5c0b1327a90695b3e13fff289aab to your computer and use it in GitHub Desktop.
Save crazyyy/e84e5c0b1327a90695b3e13fff289aab to your computer and use it in GitHub Desktop.
🤸 #php Favorite Snippets

PHP Snippets

PHP Debug

write variable to JSON file

$serialized = json_encode($data, JSON_PRETTY_PRINT);
// Save the serialized array to a text file.
file_put_contents( WP_CONTENT_DIR . '/logs/data.json', $serialized);

write debug to console.log()

function php_logger( $data ) {
    $output = $data;
    if ( is_array( $output ) )
        $output = implode( ',', $output );

    // print the result into the JavaScript console
    echo "<script>console.log( 'PHP LOG: " . $output . "' );</script>";
}

PHP info and INI

var_dump(php_ini_loaded_file(), php_ini_scanned_files());
php_info();

Debug

echo '<table><tr><td><pre>';
  var_dump($the_query->posts);
echo '</pre></td><td><pre>';
	print_r($the_query);
echo '</pre></td></tr></table>';

Write Dump to File

  ob_start();
  var_dump($form);
  $content = ob_get_clean();
  file_put_contents(__FILE__.spl_object_hash($this), $content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment