cball306 Posted July 3, 2010 Share Posted July 3, 2010 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(""); ?> Link to comment https://forums.phpfreaks.com/topic/206627-checking-if-an-argument-in-a-function-is-not-called/ Share on other sites More sharing options...
Mchl Posted July 3, 2010 Share Posted July 3, 2010 You want to change this if (isset($agent)){ to this: if (!isset($agent)){ Also you should change function signature to allow for ommiting argiment function browsercheck ($agent = null) { Link to comment https://forums.phpfreaks.com/topic/206627-checking-if-an-argument-in-a-function-is-not-called/#findComment-1080669 Share on other sites More sharing options...
cball306 Posted July 3, 2010 Author Share Posted July 3, 2010 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. Link to comment https://forums.phpfreaks.com/topic/206627-checking-if-an-argument-in-a-function-is-not-called/#findComment-1080675 Share on other sites More sharing options...
Mchl Posted July 3, 2010 Share Posted July 3, 2010 For empty argument you should call this function like this: browsercheck(); See here: http://www.php.net/manual/en/functions.arguments.php Link to comment https://forums.phpfreaks.com/topic/206627-checking-if-an-argument-in-a-function-is-not-called/#findComment-1080677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.