2

我用一些代码来检查浏览器是 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
        }
    }
?>
4

5 回答 5

3

您的问题与 ' 类型无关$browserCheck,而与它的范围有关。它在你的函数内部是未知的。您必须使用global关键字或$GLOBALS['browserCheck']在函数内部,或者更好的是,将其作为参数传递给函数。

但是,如果$browserCheck未在代码中的其他任何地方使用,则应在函数内部而不是在全局范围内定义它。

最佳方法:作为参数传递

// 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...
}

替代方案:通过 $GLOBALS 访问

function browseRight($t)
{
    // pull browserCheck from the $GLOBALS array
    if (($t == 'css') && ($GLOBALS['browserCheck'] > 0))
    {
    // etc...
}

最后,您可以global $browserCheck在函数顶部使用,但不建议这样做。我更喜欢$GLOBALS它,因为它在您的代码中明确表明您正在访问全局变量。

推荐阅读:PHP 的变量范围手册。

于 2012-05-03T13:39:35.417 回答
2

$browserCheck 变量是全局的

如果你想从函数中访问它,你需要声明

global $browserCheck 

在您的功能开始时

于 2012-05-03T13:39:37.583 回答
1

最简单的方法是添加 GLOBAL $browsercheck; 像这样

function browseRight($t)
{
    GLOBAL $browserCheck;

声明函数后在此处添加它。不过,这又快又脏。您应该真正将 $browserCheck 传递给函数,而不是使用全局变量。

于 2012-05-03T13:45:51.293 回答
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
    }
}
于 2012-05-03T13:47:40.187 回答
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
        }
    }
?>
于 2012-05-03T13:41:32.140 回答