我有一个小问题,我用一些代码来检查浏览器是IE6还是IE7,我相信问题是处理$browserCheck
变量的简单问题。我的意图是$browserCheck
变量增量,但出于测试目的,我也有变量echo。我认为问题可能是脚本无法识别我将变量设置为整数的位置,因此使用http://php.net/manual/en/language.types.type-juggling.php来更改类型。这个网站是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
}
}
?>
答案 0 :(得分:3)
您的问题与$browserCheck
的类型无关,而是与其范围有关。在你的功能中不知道。您必须在函数内部使用global
关键字或$GLOBALS['browserCheck']
,或者更好的是,将其作为参数传递给函数。
但是,如果代码中的其他任何地方都没有使用$browserCheck
,则应在函数内部而不是在全局范围内定义。{/ p>
// 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...
}
function browseRight($t)
{
// pull browserCheck from the $GLOBALS array
if (($t == 'css') && ($GLOBALS['browserCheck'] > 0))
{
// etc...
}
最后,您可以在函数顶部使用global $browserCheck
,但不建议这样做。我更喜欢$GLOBALS
因为它在您的代码中明确表示您正在访问全局变量。
答案 1 :(得分:2)
$ browserCheck变量是全局的
如果你想从函数中访问它,你需要声明
global $browserCheck
在你的职能开始时
答案 2 :(得分:1)
最简单的方法是添加GLOBAL $ browsercheck;像这样
function browseRight($t)
{
GLOBAL $browserCheck;
声明函数后立即添加它。但这很快又很脏。你应该将$ browserCheck传递给函数,而不是使用全局。
答案 3 :(得分:0)
这将解决它:
<?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
}
}
?>
答案 4 :(得分:0)
你需要移动浏览器 - 检查决定是否应该显示CSS或HTML是基于函数,在它之外它是无用的。这也将确保它不会被无用地执行,你可以适当地关注变量范围。
使用静态变量可确保调用之间的值不会丢失。默认情况下,第一个函数调用的静态值为NULL(除非您初始化它们):
<?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
}
}