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.

MYSQLI DELETE Prepared Statement

This is how to make a PHP mysqli DELETE query with a prepared statement. This example includes error checking at every step. Error checking will help you debug your prepared mysqli query when it’s failing and you don’t know why. You can use this example to help you debug any mysqli prepared statement, not only … Read more →

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 ‘’ on line 21 above to the IP … Read more →

Insert Data Array Into MySQL Table Using PHP

Updated to use MySQL transactions, prepared SQL statments, and bound parameters. This lets you insert data from a PHP array into your MySQL database table. It will insert multiple rows into the table, where each row will take values from one array element. Before inserting, you must prepare your data into an array consisting of … 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