Wednesday, December 12, 2012

PHP configuration files by environment

Ever want to define a configuration file for your code that is different on each of your servers automatically?

At bare minimum you may want to create a configuration file for your local development environment and your live server.

If you aren't using a framework like symfony or codeigniter and you'd like an easy way to make your configurations solid, then here is the trick for you.

It all comes down to defining a custom variable in the php ini file on your local environment. Simply open up your php.ini file, toss in the below code in there just below the [PHP] tag

server_environment = "local"

Then it's up to your programming to read this variable to decide what configuration to include. I use a general file named conf.php that gets included in my base controller class.

conf.php defines some pretty standard things like database connection strings, a site notification email address, and sometimes path information.


$server_environment = get_cfg_var('server_environment');
if ( $server_environment && file_exists(dirname(__FILE__). '/conf.'. $server_environment .'.php') ) {
include dirname(__FILE__). '/conf.'. $server_environment .'.php';
} else {
define('BASE_WEB', '/');

$db_config = array(
'username' => 'codebase',
'password' => 'zigzag',
'database' => 'codebase',
'host' => 'localhost',
'debug' => false,

Then you can create a file in the same folder as your conf.php file, name it conf.local.php and then you can define separate configuration parameters in there:

define('BASE_WEB', '/development/website/');

$db_config = array(
'username' => 'codebase',
'password' => '',
'database' => 'codebase',
'host' => 'localhost',
'debug' => true,

It's as easy as that, now you have a configuration file that will automatically be included for your local development environment with different parameters from your production configuration file.

If you need global configuration that applies to both your production and local environments, then just put the configuration parameters outside of your conditional statement in conf.php ( just like the cookie salt ).

Thursday, April 19, 2012

PHP Ternary Operator

Ever want a short way to quickly evaluate a conditional and set a variable based on it's result? You could do it the long way and do something like:

if ( $var == 1 ) $moo = 2; else $moo = 3;

Or you could use a ternary operator, for example

$moo = ( $var == 1 ? 2 : 3 );

It evaluates the conditional before the question mark, if it's true it gives you the first item before the colon, otherwise it gives you the item after the colon. This is useful for many situations such as showing a quick representation of a boolean:

echo ( $var ? 'Yes' : 'No' );

It makes booleans more digestible to those users who may not know that a 1 is "Yes", in situations like pulling a boolean value from a table and displaying it to an admin.

Monday, April 2, 2012

windows 7 windows is unable to install to the selected location 0x80300024

Recently my desktop computer had an SSD drive go out. The said SSD had the installation of windows 7 on it. Out of no where the drive would not boot. I tried a few times to get it to boot, eventually it got through and gave me the classic blue screen of death, referencing some problem about disk reads.

After a few more tries I was able to get it to boot, however the system had many corrupted files. This SSD is not even a year old yet. I don't know if I will have much trust for SSD's for a while; on the other hand I have had regular disks go out on me in the same time frame.

At any rate, I had a backup of my files, and as the system had a couple of 2 TB drives I had a place to install win7 to get back up and running. I removed the SSD drive, popped in the Windows 7 install disk and rebooted. I was greeted by the installation screen and the options for choosing a drive. Beforehand I had moved all my files over to one of the drives, and cleared one off in prep to install win 7.

I selected my unallocated drive and hit go, expecting that the install would soon be under way. I was greeted by an error message "windows 7 windows is unable to install to the selected location 0x80300024". Microsoft sure does love their cryptic error messages. After a bit of googling I decided that I'd give a try at loading some drivers from the motherboard driver disk, in a ditch effort that it might be a missing driver. After no luck with the driver and win 7 installer still throwing some 0x80300024 at me.

In another ditch effort, I figure well perhaps the installer is easily confused by me having multiple hard drives. I shut down the system, unplugged the drive that had data leaving only the unallocated drive plugged in. I fired up the windows installer again, clicked the disk, low and behold my installation is underway.

This goes to show you, the windows 7 installer is easily confused. If you are having problems try unplugging all extra disks except the one you are installing to.