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.

PHP mktime() Changes Your Time, Here’s the Fix

If you’re using PHP mktime() to make a timestamp for a specific moment in time that should not change, then be sure to first set the default timezone to UTC. Otherwise, the dynamic behavior of mktime() is to modify or change your time, based on the timezone settings on your server. This may be desirable … Read more →

PHP strftime() is Faster Than date() in PHP 7

After running several benchmark tests, I find that the PHP strftime() is faster than date(). I tested each one creating the same datetime string in this format: ‘YYYY-MM-DD HH:MM’. The test iterates 100000 times over each function. I repeated this test several times. Each time, I alternated which function ran first. Here are the benchmark … Read more →

PHP – Check if INDEX or KEY Exists in MySQL Table

Here are some PHP functions to check if an INDEX exists in a MySQL table. Here is also a function that adds a new INDEX to a table. The first function lets you check for a specific INDEX (or KEY). The second function lists all indexes of a table, with their Key_name and Column_name. The … Read more →

PHP Function to Count Lines in a Data Text File

This is a PHP function that will return the number lines in a text file. Use it to count lines in a data file. See a usage example below. The function takes one parameter, $file, the path to the data text file. It will return 0 if the file is not found. Usage Example This … Read more →

PHP – Count Rows in MySQL Database Table

This is a quick PHP function to count rows in a MySQL database table. It takes 2 parameters, which are: $link This is the MySQL connection object which is made, for example, by using mysqli_connect() or mysqli_init(). $table The name of the table that you want to count rows for. The function will a return … Read more →

Error Codes For system(“unzip archive.zip”) in PHP

If you’re using system(“unzip archive.zip”) to unzip an archive file in PHP, and you want to catch errors, you’ll notice that it only returns a number for the error code. Here are the error codes (exit status codes) and meanings for the unzip command. This PHP array matches the unzip error code with its meaning. … Read more →

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 →