Member Avatar for ovidiu_b13

Hello I have this code to detect the user browser, using if and else:

<?php

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) 
		echo 'Internet explorer';

elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE) 
		echo 'Mozilla Firefox';
		
		elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE) 
			echo 'Google Chrome';
			
			else 
				echo 'Something else';
?>

Is there a way to do this using the switch statement?

I tried this, and it does not work, it just retuns default:

<?php

switch(strpos($_SERVER['HTTP_USER_AGENT'], $browser) !== FALSE) {

case $browser='MSIE':
	echo 'Internet explorer...';
		
case $browser='Firefox':
	echo 'Mozilla Firefox...';
	
case $browser='Chrome';
	echo 'Google Chrome...';	
	
default:
	echo 'Something else...';
}
?>
Member Avatar for P0lT10n

Do this:

YOUR CODE

<?php

switch(strpos($_SERVER['HTTP_USER_AGENT'], $browser) !== FALSE) {

case $browser='MSIE':
	echo 'Internet explorer...';
		
case $browser='Firefox':
	echo 'Mozilla Firefox...';
	
case $browser='Chrome';
	echo 'Google Chrome...';	
	
default:
	echo 'Something else...';
}
?>

NEW CODE

<?php

switch($_SERVER['HTTP_USER_AGENT']) {
	case 'MSIE':
		echo 'Internet explorer...';
	break;
	case 'Firefox':
		echo 'Mozilla Firefox...';
	break;
	case 'Chrome';
		echo 'Google Chrome...';	
	break;
	default:
		echo 'Something else...';
}
?>
Member Avatar for ovidiu_b13

It doesn't work. This goes to default to.

Hello $_SERVER don’t bring just “Firefox” but something like “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7) Gecko/20040803 Firefox/0.9.3” depending on the system – browser user use. Instead of making your own parsing method PHP has the get_browser function witch returns an array of informations (if available) of visitors browser. Use the “browser” key and you will get “Firefox”.

Take a look at
http://php.net/manual/en/function.get-browser.php

(And a personal coding suggestion, use , if , else if , else instead of switch statement , maybe it is more verbal but made your code a lot cleaner, in my point of view… )

I agree with jkon, the only caveat is needing a browscap.ini which isn't packaged with php by default. This can only be set in the php.ini or httpd.conf files.

Member Avatar for ovidiu_b13

I want to use switch because 1. to practice coding, 2. Because Switch is faster than if else. I don't understand the get_browser() yet. Still studying. I've studied from w3schools.com, but the tutorial was not good enough, so now I'm going to the main source: php.net (manual). Went over the

if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE) 
		echo 'Internet explorer';

and wanted to practice a little.

Switch is not faster than if else … (I can give code about it)… maybe it could be in 1000 switches for 0.01 second … but … this is not programming …

Member Avatar for ovidiu_b13

This is what I've learned from my C++ teacher, and I thought this applies to every language. I see that you are a php wizz, do you think learning from php.net manual will get me anywhere? My current level is Beginner.

Member Avatar for P0lT10n

This is what I've learned from my C++ teacher, and I thought this applies to every language. I see that you are a php wizz, do you think learning from php.net manual will get me anywhere? My current level is Beginner.

Yes !

Since you mentioned that I think that you should now a lot of deferent languages and master some of them, Cobol , basic , java , php. And not only know them, work with them as an employ first , that is how you learn the job … even your degrees … The basic think for me (maybe I am wrong) is the algorithm and object oriented way of thinking …

Member Avatar for ovidiu_b13

Unfortunately I am learning on my own. The C++ teacher I mentioned was only temporary (3-4 months), but that helped me get started. I got hired by my dean to make the school website, and that is how I've learned HTML, CSS and Joomla. Now I want to get more advanced. By the time I finish this so called College, I want to be able to get hired as a Web Developer (Not WebDesign!). I want to make web applications not websites.

I know a bit of C++, C#, Java yet still I don't exactly understand the notion of objects.

Master Java and you will know ... learn a framework then like JSF and learn PHP ... don't think PHP as an inferior language but as superior giving you the opportunity to write OOP and think as you like ... Coding strict .. Choose your editor (eclipse) ... I said much ... that is my opinion ... programming best techniques is not an algorithm but has to do with peoples involving it. ...

A friend using google chrome is being told I am using Safari, heres my code:

$user_agent = $_SERVER['HTTP_USER_AGENT']; 

if (preg_match('/MSIE/i', $user_agent)) { $browser = "Internet Explorer";} 
elseif (preg_match('/Firefox/i', $user_agent)){$browser = "Mozilla Firefox";} 
elseif (preg_match('/Chrome/i', $user_agent)){$browser = "Google Chrome";} 
elseif (preg_match('/Safari/i', $user_agent)){$browser = "Safari";} 
elseif (preg_match('/Opera/i', $user_agent)){$browser = "Opera";}
else {$browser = "Other";}
Member Avatar for diafol

Chrome gives this on my machine:

Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36

And outputs...

Google Chrome

Create a dummy page like below and send him/her the url and ask them to tell you what they get.

$user_agent = $_SERVER['HTTP_USER_AGENT']; 
echo $user_agent;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.