Reputation:
I have a small issue with a bit of code I use to check if a browser is IE6 or IE7, I believe the problem is something simple dealing with the $browserCheck
variable. My intention is to have the $browserCheck
variable increment, but for testing purposes I have the variable echo as well. I thought the problem might be that the script didn't recognize where I set the variable as a integer so is used http://php.net/manual/en/language.types.type-juggling.php to change the type. The site this is on is http://www.asuperiorcallcenter.com/development/
<?php
$browserCheck = (int) 0;
if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6') !== false) { $browserCheck++; echo $browserCheck; }
if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7') !== false) { $browserCheck++; echo $browserCheck; }
function browseRight($t)
{
if (($t == 'css') && ($browserCheck > 0))
{
?>
<link href='http://fonts.googleapis.com/css?family=Ubuntu|Oswald' rel='stylesheet' type='text/css'>
<style type="text/css">
#browseRight
{
min-width: 1000px;
padding: 0px 100px 0px 100px;
height: 440px;
background: url(browseRight/bg.jpg) repeat;
text-align: center;
}
#browseRight ul
{
width: 1000px;
}
#browseRight ul li
{
float: left;
list-style-type: none;
height: 310px;
width: 180px;
padding: 10px 10px 10px 10px;
}
#browseRight ul li:hover
{
background: #eee7d5;
}
#browseRight ul li img
{
height: 130px;
clear: both;
}
#browseRight h2
{
font-family: 'Oswald', sans-serif;
width: 100%;
clear: both;
font-size: 20px;
padding: 20px 0px 0px 0px;
color: #333333;
}
#browseRight ul li h2
{
font-family: 'Oswald', sans-serif;
width: 100%;
clear: both;
font-size: 16px;
color: #333333;
}
#browseRight p
{
font-family: 'Ubuntu', sans-serif;
font-size: 12px;
color: #333333;
text-align: center;
overflow: hidden;
}
#browseRight ul li p
{
height: 80px;
}
</style>
<?php
}
if (($t == 'html') && ($browserCheck > 0))
{
?>
<div align="center"><div id="browseRight">
<h2>Your Browser is out of date.</h2>
<p>Does the website below look bad? if so it is because your browser is out of date. Fortunately you can fix this, it is as easy as updating your web browser. Below are the most popular web browsers around, visit thier websites and update to a new browser.</p>
<div align="center"><ul>
<a href="http://www.google.com/chrome/" target="_blank"><li><img src="browseRight/google-chrome-logo.png"><h2>Google Chrome</h2><p>Get a fast, free web browser</p></li></a>
<a href="http://www.mozilla.org/en-US/" target="_blank"><li><img src="browseRight/mozilla-firefox-logo.png"><h2>Mozilla Firefox</h2><p>We are mozilla. Non-profit, for the good of the Web</p></li></a>
<a href="http://www.apple.com/safari/" target="_blank"><li><img src="browseRight/safari-logo.png"><h2>Safari</h2><p>Opera Software has always strived to develop the fastest and technologically most advanced browsers.</p></li></a>
<a href="http://www.opera.com/" target="_blank"><li><img src="browseRight/opera-logo.png"><h2>Opera</h2><p>It’s a browser. It’s a platform. It’s an open invitation to innovate. Safari sets the standard for the way browsing should be.</p></li></a>
<a href="http://windows.microsoft.com/en-us/internet-explorer/products/ie/home" target="_blank"><li><img src="browseRight/internet-explorer-logo.png"><h2>Internet Explorer</h2><p>A more beautiful web</p></li></a>
</ul></div>
</div></div>
<?php
}
}
?>
Upvotes: 2
Views: 109
Reputation: 270617
Your problem isn't to do with $browserCheck
's type, but rather its scope. It is not known inside your function. You must either use the global
keyword or $GLOBALS['browserCheck']
inside the function, or better still, pass it as a parameter to the function.
However, if $browserCheck
is not used anywhere else in your code, it should be defined inside the function rather than at global scope.
###Best method: pass as a parameter
// Declared at global scope, unknown to the function.
$browserCheck = 0;
if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6') !== false) { $browserCheck++; echo $browserCheck; }
if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7') !== false) { $browserCheck++; echo $browserCheck; }
// Pass as a parameter to the function.
function browseRight($t, $browserCheck)
{
// etc...
}
###Alternative: access via $GLOBALS
function browseRight($t)
{
// pull browserCheck from the $GLOBALS array
if (($t == 'css') && ($GLOBALS['browserCheck'] > 0))
{
// etc...
}
Finally, you can use global $browserCheck
at the top of the function, but that isn't recommended. I prefer $GLOBALS
because it makes explicit in your code the fact that you're accessing a global variable.
Suggested reading: PHP's manual on variable scope.
Upvotes: 2
Reputation: 197767
You need to move the browser-check the decision if the CSS or HTML should be displayed is based on into the function, outside of it it is useless. That will also ensure it won't be uselessly executed and you can properly care about variable scope.
Using a static variable ensures, the value won't get lost between calls. By default, static values are NULL on the first function call (unless you initialize them):
<?php
function browseRight($t)
{
static $browserCheck;
if (NULL === $browserCheck)
{
$browserCheck = 0;
if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6') !== false) { $browserCheck++; echo $browserCheck; }
if (strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7') !== false) { $browserCheck++; echo $browserCheck; }
}
if ($browserCheck === 0) return;
if ($t == 'css')
{
?>
<link href='http://fonts.googleapis.com/css?family=Ubuntu|Oswald' rel='stylesheet' type='text/css'>
<style type="text/css">
#browseRight
{
min-width: 1000px;
padding: 0px 100px 0px 100px;
height: 440px;
background: url(browseRight/bg.jpg) repeat;
text-align: center;
}
#browseRight ul
{
width: 1000px;
}
#browseRight ul li
{
float: left;
list-style-type: none;
height: 310px;
width: 180px;
padding: 10px 10px 10px 10px;
}
#browseRight ul li:hover
{
background: #eee7d5;
}
#browseRight ul li img
{
height: 130px;
clear: both;
}
#browseRight h2
{
font-family: 'Oswald', sans-serif;
width: 100%;
clear: both;
font-size: 20px;
padding: 20px 0px 0px 0px;
color: #333333;
}
#browseRight ul li h2
{
font-family: 'Oswald', sans-serif;
width: 100%;
clear: both;
font-size: 16px;
color: #333333;
}
#browseRight p
{
font-family: 'Ubuntu', sans-serif;
font-size: 12px;
color: #333333;
text-align: center;
overflow: hidden;
}
#browseRight ul li p
{
height: 80px;
}
</style>
<?php
}
if ($t == 'html')
{
?>
<div align="center"><div id="browseRight">
<h2>Your Browser is out of date.</h2>
<p>Does the website below look bad? if so it is because your browser is out of date. Fortunately you can fix this, it is as easy as updating your web browser. Below are the most popular web browsers around, visit thier websites and update to a new browser.</p>
<div align="center"><ul>
<a href="http://www.google.com/chrome/" target="_blank"><li><img src="browseRight/google-chrome-logo.png"><h2>Google Chrome</h2><p>Get a fast, free web browser</p></li></a>
<a href="http://www.mozilla.org/en-US/" target="_blank"><li><img src="browseRight/mozilla-firefox-logo.png"><h2>Mozilla Firefox</h2><p>We are mozilla. Non-profit, for the good of the Web</p></li></a>
<a href="http://www.apple.com/safari/" target="_blank"><li><img src="browseRight/safari-logo.png"><h2>Safari</h2><p>Opera Software has always strived to develop the fastest and technologically most advanced browsers.</p></li></a>
<a href="http://www.opera.com/" target="_blank"><li><img src="browseRight/opera-logo.png"><h2>Opera</h2><p>It’s a browser. It’s a platform. It’s an open invitation to innovate. Safari sets the standard for the way browsing should be.</p></li></a>
<a href="http://windows.microsoft.com/en-us/internet-explorer/products/ie/home" target="_blank"><li><img src="browseRight/internet-explorer-logo.png"><h2>Internet Explorer</h2><p>A more beautiful web</p></li></a>
</ul></div>
</div></div>
<?php
}
}
Upvotes: 0
Reputation: 3217
The simplest way would be to add GLOBAL $browsercheck; like so
function browseRight($t)
{
GLOBAL $browserCheck;
Add it right there after you declare the function. This is quick and dirty though. You should really pass $browserCheck to the function, rather than using a global.
Upvotes: 0
Reputation: 2023
This will get it fixed:
<?php
$browserCheck = false;
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 6.') !== FALSE) { $browserCheck = true; echo $browserCheck; }
if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE 7.') !== FALSE) { $browserCheck = true; echo $browserCheck; }
function browseRight($t)
{
if (($t == 'css') && $browserCheck)
{
?>
<link href='http://fonts.googleapis.com/css?family=Ubuntu|Oswald' rel='stylesheet' type='text/css'>
<style type="text/css">
#browseRight
{
min-width: 1000px;
padding: 0px 100px 0px 100px;
height: 440px;
background: url(browseRight/bg.jpg) repeat;
text-align: center;
}
#browseRight ul
{
width: 1000px;
}
#browseRight ul li
{
float: left;
list-style-type: none;
height: 310px;
width: 180px;
padding: 10px 10px 10px 10px;
}
#browseRight ul li:hover
{
background: #eee7d5;
}
#browseRight ul li img
{
height: 130px;
clear: both;
}
#browseRight h2
{
font-family: 'Oswald', sans-serif;
width: 100%;
clear: both;
font-size: 20px;
padding: 20px 0px 0px 0px;
color: #333333;
}
#browseRight ul li h2
{
font-family: 'Oswald', sans-serif;
width: 100%;
clear: both;
font-size: 16px;
color: #333333;
}
#browseRight p
{
font-family: 'Ubuntu', sans-serif;
font-size: 12px;
color: #333333;
text-align: center;
overflow: hidden;
}
#browseRight ul li p
{
height: 80px;
}
</style>
<?php
}
if (($t == 'html') && $browserCheck)
{
?>
<div align="center"><div id="browseRight">
<h2>Your Browser is out of date.</h2>
<p>Does the website below look bad? if so it is because your browser is out of date. Fortunately you can fix this, it is as easy as updating your web browser. Below are the most popular web browsers around, visit thier websites and update to a new browser.</p>
<div align="center"><ul>
<a href="http://www.google.com/chrome/" target="_blank"><li><img src="browseRight/google-chrome-logo.png"><h2>Google Chrome</h2><p>Get a fast, free web browser</p></li></a>
<a href="http://www.mozilla.org/en-US/" target="_blank"><li><img src="browseRight/mozilla-firefox-logo.png"><h2>Mozilla Firefox</h2><p>We are mozilla. Non-profit, for the good of the Web</p></li></a>
<a href="http://www.apple.com/safari/" target="_blank"><li><img src="browseRight/safari-logo.png"><h2>Safari</h2><p>Opera Software has always strived to develop the fastest and technologically most advanced browsers.</p></li></a>
<a href="http://www.opera.com/" target="_blank"><li><img src="browseRight/opera-logo.png"><h2>Opera</h2><p>It’s a browser. It’s a platform. It’s an open invitation to innovate. Safari sets the standard for the way browsing should be.</p></li></a>
<a href="http://windows.microsoft.com/en-us/internet-explorer/products/ie/home" target="_blank"><li><img src="browseRight/internet-explorer-logo.png"><h2>Internet Explorer</h2><p>A more beautiful web</p></li></a>
</ul></div>
</div></div>
<?php
}
}
?>
Upvotes: 0
Reputation: 3073
the $browserCheck variable is global
If you want to access it from within the function youll need to declare
global $browserCheck
at the start of your function
Upvotes: 1