Web & Software Developer

Archive for “PHP Code Snippets”

PHP code snippets and tips for customizing your own website, and for generating dynamic content with PHP.

Print or var_dump To Screen, But Only On My IP Address

This lets you print PHP variable information, or any debugging code, to the screen, but only on your IP address. This is useful when you have to test on a live site, and for whatever reason you want to print to the screen rather than to error logs, but you don’t want visitors to see … Read more →

Return Human-Readable debug_backtrace With Line Number

Get a human-readable debug backtrace including the the file, the function which calls it, and its line number. Usage: The following will print it directly to your WordPress error log (/wp-content/debug.log).

Hide Content From One IP Address on WordPress Site

This gets the visitor’s IP address, and hides the content from the IP address that you specify. This is for use on WordPress sites. One way to use it is in your single.php template file (or content-single.php). Replace this: … with this: Be sure to change the ‘11.111.111.11’ on line 21 above to the IP … Read more →

Insert Data Array Into MySQL Table Using PHP

Updated to use prepared SQL statments and bound parameters. This lets you insert data from a PHP array variable into your MySQL database table. Before inserting, you must prepare your data into an array consisting of records that you want to insert into the database. This should be a two-dimensional array, in which each element … Read more →

Get Current Page URL Using PHP

This is a PHP function to get the current page URL with only PHP. This works within WordPress, as well. Example Usage One use for this is if you’re using ajax to process a form and return results to the page, and you’d like a link below the results to allow the user to “Return … Read more →

Convert Time From 24-hour Format to 12-hour Format

This is a PHP function to convert time from 24-hour format (HH:MM) into 12-hour format, with am or pm added. It takes two parameters: the hour the minute (with a leading zero for 0 through 9) It returns the time in 12-format like this: HH:MM am/pm

Get Local Time in PHP with localtime

This will get the local time from the visitor’s browser. Then it converts PHP’s localtime() function into a readable display format, with leading zeros before the single-digit minutes. It also converts time into 12-hour format and appends the meridian ( am/pm ) to it. Then it displays the final time, formatted nicely, like See below … Read more →