Tag: PHP
-
How to Connect to a Database with PHP Data Objects (PDO)
PHP Data Objects (PDO) provide a consistent interface to different databases including MySQL, PostgreSQL, SQLite, SQL Server and a handful of other systems as well. Aside from database-specific SQL, the only other thing that isn’t consistent is how to connect to a database. Each database type requires a string to…
-
Split a String by Letters with PHP
Every once in a blue moon you find yourself needing to take a string and converting it to an array of characters in the string. Maybe it’s because you want to count how often certain letters exist in the string or want to convert the letters to something else. To…
-
Passing Arguments to Command-Line PHP
In a previous post we discussed how to tell whether or not you are on the command-line from within your PHP script. Today we’re going to talk about passing command-line arguments to your script. When on the command-line two variables are available to you, $argc and $argv. They are the…
-
Calculating page load time / execution time with PHP
So who remembers back in the day when one of the coolest things you could have on your website’s footer was the page load time? As passé as that may seem in this day in age, it’s still something that can be useful when attempting to profile a site or…
-
MySQL and Redis Command Equivalents
Score another one for HitTail as this particular topic was their suggestion. MySQL (PostgreSQL, SQL Server, SQLite or any other RDBMS I didn’t list) and Redis are completely different beasts in regard to syntax and especially schema design (or lack there of). The following are just some common SQL statements…
-
Check if PHP is running from the command-line
When running PHP from the command-line interface, the availability of some variables and functionality may or may not be available. Case in point, when running command-line PHP sessions are not available and $_SESSION is undefined. When you’re on the command-line there’s also the possibility that you want to use the…
-
Check if a PHP class exists
We’ve discussed how to check if a class has a function but what if the class doesn’t exist at all? To check if a class exists you can use, use guessed it, class_exists(). if (class_exists(‘MyClass’)) { $object = new MyClass(); } else { throw new Exception(‘The class MyClass does not…
-
Check if a PHP class has a certain function
We’ve previously discussed how to check if a function exists but that only works on standalone functions like the ones built into PHP as well as any user defined functions. Luckily there’s a function for that called method_exists(). When speaking in the scope of a class, the functions are referred…
-
Check if a PHP function exists
Let’s use json_encode as an example here. Prior to PHP 5.2.0 JSON support was only available via a PECL extension and since 5.2.0 it’s been available as part of the PHP core. You could check that the server is running PHP 5.2.0 or above, but that could result in a…
-
Detect Required PHP Version
If you write and/or use open source software, you know that running an up to date version of PHP is a must. This can be a problem sometimes, like if you are running on a host that doesn’t run the most up to date version of PHP. Another scenario would…