Creating a PHP Function

Associate
Joined
3 Oct 2006
Posts
2,304
Location
London
Hi,

When creating a php function, how do you define an optional argument for it? I'm wanting to do this

PHP:
	function dbgetviewcomputerhardware($optional)
	{
		global $db;
		$query = "SELECT * FROM ";
		$query .= $db->V_COMPUTER_HARDWARE;
		if (isset ($optional)){
			$query .= " WHERE CompUse = ";
			$query .= $optional;
		}
		$result = mssql_query($query);
		return $result;
	}
 
Also, use less verbose string concatenation IMO:

Code:
function dbgetviewcomputerhardware( $optional = null) {
    global $db;
    $query = "SELECT * FROM $db->V_COMPUTER_HARDWARE";
    if ( $optional ){
        $query .= " WHERE CompUse = $optional "
    }
    $result = mssql_query($query);
    return $result;
}
 
Thanks,

Have knocked the string back down to two lines now. I split it up because I was having some problems with my script so eliminating things that could have possibly caused them.

Cheers all!
 
Back
Top Bottom