Life of a web developer

February 2, 2011

External: 27 ways to be a better PHP developer

Filed under: External, PHP — Tags: , , , — Ole Aass @ 12:17 pm

LornaJane posted on her blog today a great 46 page slide covering 27 essential ways on how to be a better php developer. It’s not a “best practice” coding style article, but it has focus on the higher aspects on how to be better.

Some points covered in the slide is:

  • Participate in communities
  • Focus on relationships
  • Keep learning
  • Challenge yourself by leaving your comfort zone

Even though the title of the article is “27 ways to be a better PHP developer”, it is absolutely an article that refers to any developer, no matter what language.

Source: http://www.lornajane.net/posts/2011/27-Ways-To-Be-A-Better-PHP-Developer

November 23, 2010

Tutorial: randomImage() – A “new” way to select random images directly from the directory

Filed under: PHP, Tutorials — Tags: , , , — Ole Aass @ 8:55 pm

So, I stumbled across a very handy php function today called glob().

The glob() function returns an array of filenames or directories matching a specified pattern.
This function returns an array of files/directories, or FALSE on failure.

So I figured this would be great for showing stuff like random ads etc, and had to give it a closer look. The following function is what I used to try it out

/**
 * Select a random image from a directory
 * 
 * @param string $path The full directory path to where the images are located
 * @param bool $removePath If this is set to true, only the filename will be returned. (Default: false)
 * @param string $pattern The pattern it will use to filter out what files to grab. (Default: * (will select all filename))
 * @param string $validExtension All valid extensions separated by comma (Default: gif,jpg,png)
 */
function randomImage($path, $removePath = false, $pattern = '*', $validExtensions = 'gif,jpg,png') {

	// Place all image files in an array
	$images = glob($path.$pattern.'.{'.$validExtensions.'}', GLOB_BRACE);
	
	// Check if the result turned up empty
	if (!$images) {
		return false;
	}
	
	// Pick a random image from the array
	return ($removePath) ? str_replace($path, '', $images[array_rand($images)]) : $images[array_rand($images)];
}

Usage:

print_r(randomImage('images/', false));

Tutorial: getExt() – Get the extension of a file

Filed under: PHP, Tutorials — Tags: , , — Ole Aass @ 6:56 pm

This is probably the shortest function you can make that’s still very usefull

function getExt($filename) {
    return end(explode('.', $filename));
}

A more advanced version of this function will handle an array of filenames

function getExt($filenames) {

    // Make sure the variable is an array
    if (!is_array($filenames)) {
        $filenames = (array) $filenames;
    }

    // Loop through the array and assign the full filename as
    // index, and the value will be the file extension
    foreach ($filenames as $key => $value) {
        $output[$value] = end(explode('.', $value));
    }

    // Return an array holding the file extensions
    return $output
    
}

November 22, 2010

Tutorial: Handy mysql database object

Filed under: Database, PHP, Tutorials — Tags: , , , , — Ole Aass @ 10:45 pm

I’m tired of typing the same stuff over and over. So I spend alot of time writing reusable code.

No matter if it’s just simple methods or abit more complex objects. Here’s the database object I just wrote. It’s versatile and it really doesnt restrict you to follow any special rules except what objects already restricts you to. You can decide for yourself if you want to complete the query statement or make prepared statement in a valid vsprintf() format.

The following code is examples of usage:

$db = new Class_Database;
$db->connect();

$sql = "SELECT * FROM users WHERE username='%s' AND password='%s'";
$inputs = array('username', 'password');

$result = $db->execute($sql, $inputs);
if (!$result)
{
	return false;
}
else
{
	return true;
}

or you can use:

$db = new Class_Database;
$db->connect();

$username = $db->secureInputs('username');
$password = $db->secureInputs('password');
$sql = "SELECT * FROM users WHERE username='".$username."' AND password='".$password."'";

$result = $db->execute($sql);
if (!$result)
{
	return false;
}
else
{
	return true;
}

(more…)

Tutorial: emptyStr() the end of long unnecessary if statements

Filed under: PHP, Tutorials — Tags: , , — Ole Aass @ 10:33 pm

This is absolutely not a newsflash for anyone that’s been working a bit with PHP. We always come across situations where we need to make sure strings is or isn’t empty, and what’s more boring than having to write the same stuff for each variable? So i thought, why not make it like the isset() that handles an endless number of arguments? So this is what I came up with:

// Initialize the function
function emptyStr() {

	// Collect all the arguments passed to the method
	// and add them to an array
	$args = funct_get_args();

	// Loop through the arguments
	foreach ($args as $key => $value) {

		// If the argument is not an empty string, return false
		if ($value == "") {
			return true;
		}
	}

	// If none of the arguments are empty strings,
	// return true
	return false;
}

Usage:

$var1 = "Hello";
$var2 = "world!";

if (!emptyStr($var1, $var2)) {
	echo $var1.' '.$var2;
} else {
	echo "Failed";
}

The above code will output: Hello world!

Blog at WordPress.com.