Is there a way instead of putting OS && Browser as 'If commands', making it so that it would work as an array? If not is there a way of cleaning this up so that instead of doing a lot of "if commands" making it so it goes through a list and says you are using x OS and y browser? Or is this something that I should be using with PHP/SQL to make it easier? <?php //Operating Systems function xp(){return(eregi("Windows NT 5.1", $_SERVER['HTTP_USER_AGENT']));} function vista(){return(eregi("Windows NT 6.0", $_SERVER['HTTP_USER_AGENT']));} function win7(){return(eregi("Windows NT 6.1", $_SERVER['HTTP_USER_AGENT']));} //Web Browsers function chrome(){ return(eregi("chrome", $_SERVER['HTTP_USER_AGENT']));} function ie9(){ return(eregi("MSIE 9.0", $_SERVER['HTTP_USER_AGENT']));} function ie8(){ return(eregi("MSIE 8.0", $_SERVER['HTTP_USER_AGENT']));} if(xp() && chrome()) { // do something if XP and Chrome echo 'You are using Windows XP with a Chrome Web Browser'; } if(xp() && ie8()) { // do something if XP and Chrome echo 'You are using Windows XP with a Internet Explorer 8 Web Browser'; } ?> Code (markup):
Are you only wanting to output a statement that says you are using X operating system and Y web browser? Or do you want to perform another function depending on what they are using?
Well ultimately I will want to want to output a line of code... which I think I know how to do, but what I am looking to do is make it say you are using x OS and y browser... but what I want to do is make it so that it does it in one line instead of having a lot of if statements all over the place. The amount of variables really is too extensive to be worth typing. Basically what I am doing is writing a if x OS and y browser, use z CSS script. But I also want to use it to make a script in where I can use it for other functions as well.
Just set the common stuff (like variables, business logic etc) outside of your if statements, then anything specific which uses them variables inside. I can't see how you'll have too many items of code (other than css) which are written for a specific browser.
You should be using the preg* set of functions (preg_match ?) instead as ereg* is deprecated. Also don't bother with what you are doing - no need to reinvent the wheel....I've seen tons of classes and functions lying around code databases online - google...
You can alternately use the switch statement to display the operating system first and then use switch statement again to display the browser. http://php.net/manual/en/control-structures.switch.php
The function below will help you to get the OS the user is using. http://www.php.net/manual/en/function.get-browser.php Using the $_SERVER['HTTP_USER_AGENT'] - you can get the User agent i.e. the browswer the user is using. Combing these together - you should be able to construct any line of code you want to output. I think this could be done within 5 lines of code or fewer.