Hi all haven’t posted in a while.
Been doing a fair bit of PHP recently and have come across some useful PHP snippets.
I was looking to perform multiple MySQL queries but the plain old mysql_query doesn’t allow it unfortunately. After doing a little bit of digging around I found that you need to use mysqli_multi_query to perform multi-queries. Of course you need to connect to your MySQL database using mysqli_connect(and I believe you need to have enabled a seperate mysqli module for php).
I’ve also been doing quite a bit of string manipulation with php. I was looking for a similar function to Coldfusion’s ListContainsNoCase whereby I needed to search a comma seperated list for a specific value. Didn’t find anything especially useful so learnt that using the explode function converts a string separated by a specified delimiter into an array which can then be searched using the in_array function. The strict flag is especially useful if you require an element of the array to match exactly the original term you’re looking for in the list.
String comparison was another sticking point last week. I had a string containing a single character, “*” and attempted compare this string variable using the double equality operator(i.e. $myVar == “*”) but was getting a false match every time. After printing out the variable many times and ensuring that it did indeed have the right value I began to think something else was wrong and had a look at the php documentation. After trying out the Strcasecmp function I began getting the matches I expected. strcasecmp is a binary safe case-insensitive string comparison. A binary-safe function is essentially one that treats its input as a raw stream of data without any specific format. It should thus work with all 256 possible values that a character can take (assuming 8-bit characters). Most functions are not binary safe when using any special or markup characters, such as escape codes or those that expect null-terminated strings. A possible exception would be a function whose explicit purpose is to search for a certain character in a binary string.(thank you Wikipedia). As I was working with a special character my string comparison was crapping out, now I know why.
Creating a singleton class in PHP is something that is a very common operation and after looking at a handy tutorial I came up with a handy singleton class which is used to obtain and instantiate singleton objects. Further work needs to be done on this to allow for multiple objects to be instantiated but it works pretty well for me.(sorry about the indentation, WordPress screwed it up on pasting it in)
class Singleton
// ensure that only a single instance exists for each class.
{
function getDBConnection()
{ static $db = null;
global $dbLocation;
global $dbusername;
global $dbpassword;
global $cstdatabase;
if($db == null)
{ $db = mysql_connect($dbLocation,$dbusername,$dbpassword);
mysql_select_db($cstdatabase) or die( "Unable to select database");
}
return $db;
}
//TODO: Make this more flexible for parameters into inited functions, possibly an array
public function &getInstance ($class, $classPath=null,$arg1=null)
// implements the 'singleton' design pattern.
{ $lowerClassName = strtolower($class);
static $instances = array(); // array of instance names
if (array_key_exists($lowerClassName, $instances)) {
// instance exists in array, so use it
$instance =& $instances[$lowerClassName];
} else {
// load the class file (if not already loaded)
if (!class_exists($lowerClassName)) {
if($classPath)
{
require_once "$classPath/$class".".php";
}
else
{
switch ($lowerClassName) {
case 'object1':
require_once 'object1/Object1.php';
break;
case 'object2':
require_once 'object2/Object2.php';
break;
default:
require_once "$class".".php";
break;
} // switch
}
} // if
// instance does not exist, so create it
$newClass = new $class();
$instances[$lowerClassName] = $newClass;
if(method_exists($newClass,"init"))
{ if($arg1 != null)
{ $newClass->init($arg1);
}
else
{
$newClass->init();
}
}
$instance =& $newClass;
} // if
return $instance;
} // getInstance
} // singleton