Jump to content

checking if an argument in a function is not called


cball306

Recommended Posts

Hey guys,

im creating a function that detects if the user is coming from an Iphone, Ipad or internet explorer. If i call the function browsercheck(iphone); i want it to specifically check if it is an iphone. And the same goes with Ipad and MSIE. That part of the script I have got to work. But what i want is that if the function is called just like browsercheck();  without an argument is automatically detects what the user agent is and then acts accordingly.

 

The problem I have is writing the part of the code that looks if the variable $agent which is the argument is empty, and if so act upon that, and if its not empty, carry on with the rest of the function.

 

 

<?php

/* This function is used for checking the user is coming from the correct platform, and if not it will redirect them */
function browsercheck ($agent) {
$useragent = getenv("HTTP_USER_AGENT");

if (isset($agent)){
	echo "choose";
} else {




switch ($agent) {

	/* This identifies if the user is using an iphone and if not redirect */
	case "iPhone":
	if (preg_match("/iPhone/", "$useragent")) {
	echo "your using iphone";
	echo "$useragent";
}
else { 
echo "not iphone"; }
break;

/* This identifies if the user is using Ipad and if not redirects */
case "iPad":
if (preg_match("/Safari/", "$useragent")) {
	echo "your using iPad";
	echo "$useragent";
}
else { 
echo "not iPad"; }
break;

/* This identifies if the user is using safari and if not redirects */
case "MSIE":
if (preg_match("/MSIE/", "$useragent")) {
	echo "your using Internet Explorer";
	echo "$useragent";
}
else { 
echo "not IE"; }
break;

}
}
}

return $agent;

browsercheck("");

?>

thanks for the help, the script still returns a blank screen when it should be echoing "choose". I made the change you said from (isset) to (!isset) but still getting a blank screen. can you elaborate a bit on the second change you suggested as i don't quite understand.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.