PHP Debug In Browser Console

When debugging in PHP there are a few techniques you can use, you could use something likeXdebug which will allow you to step through your code at run time and you can see exactly what the code is doing. Xdebug will allow you to step into function and make sure the variables are being set as the should be.
Your other option is to output the code in the browser and exit the script so it just displays what you want to debug and nothing else, something similar to this.
<?php
echo '<pre>';
print_r($debug_array);
echo '</pre>';
exit;
?>
This will allow you to see exactly what is in this variable at a certain time of running your code.
But what if you want to view the rest of the page and debug at the sametime, you can simply print the variable without the exit in your code. But then you get the problem of the print being on the page which could either break your design or the debug will be displayed in the design.
Your other option is to write debug into a log file, this can be done with a debug class which writes to a debug log file, allowing you to view all your variables and not breaking your design.
If you don't want to send your data to a log file you can also try this neat little trick and output debug data into the browser debug console.
console
Here is a snippet of a function you can use to output PHP data to the browser console.
<?php
/**
 * Send debug code to the Javascript console
 */ 
function debug_to_console($data) {
    if(is_array($data) || is_object($data))
 {
  echo("<script>console.log('PHP: ".json_encode($data)."');</script>");
 } else {
  echo("<script>console.log('PHP: ".$data."');</script>");
 }
}
?>

Access Database Outside Of WordPress

Sometimes I've needed to build a new page outside of WordPress but still have the ability to access the WordPress database from this new page. This would normally use custom tables you've created.
To access the WordPress database normally you would just need to use the global variable $wpdb. This object connects to your database using the credentials found in the wp-config.php in the constant variables DB_NAME, DB_HOST, DB_USER, DB_PASSWORD.
function access_db()
{
   global $wpdb;
}
The $wpdb object has a number of methods that you can use to interact with the database, you are able to use the following:
  • query() - To run a custom SQL query on the database.
  • get_var() - Get a single value from the database and add as a variable.
  • get_row() - Get the single row from the database.
  • get_col() - Get the column information from the database.
  • get_results() - Get all the results from SQL query.
  • insert() - Insert a row into the database
  • replace() - Replace the data in the row.
  • update() - Allows you to update a row in the database.
  • delete() - Allows you to delete a row from the database.
  • prepare() - Used for SQL escaping when processing the SQL query.
If you're using custom database tables in your application then you will be able to access them by using the $wpdb object and writing your own custom queries.
For more information on creating custom tables in WordPress use this tutorial.
If you're creating a page outside of the WordPress application you won't have access to the $wpdb object, so you will need to instantiate the WordPress application before you can access it. To do this in WordPress it's very easy. If you look at the .htaccess file you will see some code like this.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This will send all requests on the domain to go through the index.php file, then if you look inside the index.php file you will see how WordPress will be loaded, simply by loading the file wp-blog-header.php.
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
When this is loaded it will create the WordPress application and instantiate all the variables that you will need. You will have access to the $wpdb object which will automatically connect to the datbase in the wp-config.php file.
Now you will be able to use this variable in your new page outside of the WordPress application.
Create a new file, this example is a new file at the root level of your WordPress files. First require the wp-blog-header.php file and this will instantiate WordPress outside of the application.
<?php
// Get access to WordPress
require( dirname( __FILE__ ) . '/wp-blog-header.php' );

// Get all posts
$posts = $wpdb->get_results('SELECT * FROM '. $wpdb->prefix.'posts');

File_Exists For Remote URL

If you want to make sure that a file exists in PHP you can use the function file_exists(), which takes one parameter of the filename.
// Returns true if the file exists
file_exists( $filename );
This function will not only work for files but will also work for directories, you can pass in a filename of the directory and if this directory exists the file_exists() function will return true.
<?php
$filename = '/path/to/foo.txt';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>
The problem I've seen with this function is that people have tried to use it when they want to see if a remote file exists using a URL. But if you try to search for a file exists using this function with a URL it will not work correctly, the function will always return false.
If you want to check if the file_exists() with a remote URL you need to make a HTTP request for this file and check what the HTTP header status are when the request returns.

Get Headers Of A URL

To get the headers of a remote file then you can use the PHP function of get_headers(). This takes a parameter of the URL you want to request and it will return an array of the headers returned. The first key of the array is the value we are interested in, this will return the header status of the HTTP. If the file exists the status will return a 200 code, if the remote file doesn't exist then the status will return a 404 error.
This means we can use function to check if the remote file exists.
$file_headers = @get_headers($url);
if($file_headers[0] == 'HTTP/1.0 404 Not Found')
{
   $file_exists = false;
} else {
   $file_exists = true;
}