Simple question! Anyway to combine multiple "if echo" statements?

Anyway to combine multiple “if echo” statements? Basically, anyway to shorten this? Thanks!


<body <?php if ($msie6) echo 'class="ie6"'; if ($msie7) echo 'class="ie7"'; if ($msie8) echo 'class="ie8"'; ?>>

I wonder why do you use separate variables, not one with different values.
So, it can be ether

<body class="<?php echo $classes[$browser] ?>">; 

or even just

<body class="<?php echo $browser ?>">; 

Thanks! Although I am php-tarded, so could you use small words please. That sounds good, but I don’t understand what you mean? Here is what I have worked out. So you can see what I’m trying to do here. It works as is, I just wanted to know if there was a better way to write it. Thanks!


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>{ visibility: inherit; } Target IE Method #6</title>
<?php
$msie6 = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 6.0') ? true : false;
$msie7 = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7.0') ? true : false;
$msie8 = strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 8.0') ? true : false;
?>
<style type="text/css">
.ie6 p {color:red;}
.ie7 p {color:green;}
.ie8 p {color:blue;}
</style>
</head>
<body <?php if ($msie6) echo 'class="ie6"'; if ($msie7) echo 'class="ie7"'; if ($msie8) echo 'class="ie8"'; ?>>

<p>Some Text</p>

</body>
</html>


I’d use regexp to get only number and then use that number in the class name.

mind showing me how?

Although the way you are looking for probably is

<?
$msie=0;
if(strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 6.0') $msie = 6;
if(strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 7.0') $msie = 7;
if(strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE 8.0') $msie = 8;
?>

But I wonder what if it has minor version, like 6.1

I’d recommend against using PHP to simply change a style depending on IE versions.

Simply use HTML conditional statements. Different Internet Explorer versions can be differentiated and you simply add a different additional stylesheet for each one.

Yes I am aware of IE CC’s and all different ways to target IE. I am working on adding to my tut seen here. I am simply asking if this can be trimmed at all. Thanks!

and then in the body tag I’d say what?

No, it cannot be trimmed at all.
But you can use programming to do it another way.
But if you have no programming skills, it is all right too, no changes needed.

Maybe something along the lines of…

$MSIE = preg_match('/MSIE (\\d+)/', $_SERVER['HTTP_USER_AGENT'], $matches) ? $matches[1] : false;

?

Thanks guys. I worked it out here.

<?php $browser = ‘’; for($i = 4; $i<9; $i++ ) { if (strpos($_SERVER[“HTTP_USER_AGENT”], “MSIE $i.0”)) $browser = ‘class="ie’.$i.‘"’;} ?>

unbelievable

Whilst I agree that it’s dodgy, shrapnel, maybe giving advise would help him?

There’s a fine line between criticism and constructive criticism.

So the problems I can see with the code Shrapnel highlighted:

  • It requires 5 separate strpos statements, meaning more time is taken. As far as I’m aware, a single preg_match is quicker.
  • It becomes outdated as soon as I.E. 10 comes out.
  • It only includes versions n.0. There may be other subversions.

Heres another way;


$is_ie = explode( "MSIE", $_SERVER['HTTP_USER_AGENT'] ) ;
if( isset( $is_ie[1] ) ) echo (int)$is_ie[1] ;

Beware though if your IE8 is in Compatability mode, and reports itself being IE7, it could confuse a dim person (well, me anyway).

If youre serious about browser sniffing though you should read up on browscap abilities, take all the pain out of this, and I daresay tee you up nicely for Mobile too.

[fphp]get_browser[/fphp]

Cups, using this code, then what would the php in the body tag look like? This code would require no update as soon as a new IE came out right? Thanks!


$is_ie = explode( "MSIE", $_SERVER['HTTP_USER_AGENT'] ) ;
if( isset( $is_ie[1] ) ) echo 'class="ie'. (int)$is_ie[1] .'"';

Going back to your OP, to achieve the same effect you’d echo the "class = " depending on MSIE being found in the string. When a new browser turns up you might want to test it.

I’d take a good look through some old log files and see how many different “MSIE compatable” browser strings there are (I dont have a clue), and run this test on as many as you can find before telling everyone this is bullet-proof.

I can only reiterate - you should be using get_browser for this kind of thing, or as has been stated, abandon the idea of dealing with different browsers.