Php 变量增量,但当我在函数中使用它时,它不会';正如我所期望的那样

Php 变量增量,但当我在函数中使用它时,它不会';正如我所期望的那样,php,Php,我用一些代码来检查浏览器是IE6还是IE7有一个小问题,我相信这个问题是处理$browserCheck变量的简单问题。我的意图是使用$browserCheck变量增量,但出于测试目的,我还使用了变量echo。我想问题可能是脚本没有识别出我在哪里将变量设置为整数,因此用于更改类型。这个网站是 #browseRight { 最小宽度:1000px; 填充:0px 100px 0px 100px; 高度:440px; 背景:url(browseRight/bg.jpg)重复; 文本对齐:居中; }

我用一些代码来检查浏览器是IE6还是IE7有一个小问题,我相信这个问题是处理
$browserCheck
变量的简单问题。我的意图是使用
$browserCheck
变量增量,但出于测试目的,我还使用了变量echo。我想问题可能是脚本没有识别出我在哪里将变量设置为整数,因此用于更改类型。这个网站是


#browseRight
{
最小宽度:1000px;
填充:0px 100px 0px 100px;
高度:440px;
背景:url(browseRight/bg.jpg)重复;
文本对齐:居中;
}
#布朗斯莱特保险公司
{
宽度:1000px;
}
#布朗斯里特酒店
{
浮动:左;
列表样式类型:无;
高度:310px;
宽度:180px;
填充:10px 10px 10px 10px;
}
#BrowserRight ul li:悬停
{
背景:eee7d5;
}
#布朗塞莱特酒店
{
高度:130像素;
明确:两者皆有;
}
#棕黄色h2
{
字体系列:“Oswald”,无衬线;
宽度:100%;
明确:两者皆有;
字体大小:20px;
填充:20px 0px 0px 0px;
颜色:#333333;
}
#布朗斯莱特酒店
{
字体系列:“Oswald”,无衬线;
宽度:100%;
明确:两者皆有;
字体大小:16px;
颜色:#333333;
}
#棕黄色
{
字体系列:“Ubuntu”,无衬线;
字体大小:12px;
颜色:#333333;
文本对齐:居中;
溢出:隐藏;
}
#布朗斯莱特酒店
{
高度:80px;
}
您的浏览器已过时。
下面的网站看起来不好吗?如果是这样,那是因为你的浏览器已经过时了。幸运的是,您可以解决这个问题,它与更新web浏览器一样简单。下面是最流行的网络浏览器,请访问他们的网站并更新到新的浏览器


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

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

最佳方法:作为参数传递 备选方案:通过$GLOBALS访问 最后,您可以在函数顶部使用
global$browserCheck
,但不建议这样做。我更喜欢
$GLOBALS
,因为它在代码中明确表示您正在访问一个全局变量


建议阅读:

$browserCheck变量是全局变量

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

global $browserCheck 

在函数开始时,这将使其得到修复:

<?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
        }
    }
?>

#browseRight
{
最小宽度:1000px;
填充:0px 100px 0px 100px;
高度:440px;
背景:url(browseRight/bg.jpg)重复;
文本对齐:居中;
}
#布朗斯莱特保险公司
{
宽度:1000px;
}
#布朗斯里特酒店
{
浮动:左;
列表样式类型:无;
高度:310px;
宽度:180px;
填充:10px 10px 10px 10px;
}
#BrowserRight ul li:悬停
{
背景:eee7d5;
}
#布朗塞莱特酒店
{
高度:130像素;
明确:两者皆有;
}
#棕黄色h2
{
字体系列:“Oswald”,无衬线;
宽度:100%;
明确:两者皆有;
字体大小:20px;
填充:20px 0px 0px 0px;
颜色:#333333;
}
#布朗斯莱特酒店
{
字体系列:“Oswald”,无衬线;
宽度:100%;
明确:两者皆有;
字体大小:16px;
颜色:#333333;
}
#棕黄色
{
字体系列:“Ubuntu”,无衬线;
字体大小:12px;
颜色:#333333;
文本对齐:居中;
溢出:隐藏;
}
#布朗斯莱特酒店
{
高度:80px;
}
您的浏览器已过时。
下面的网站看起来不好吗?如果是这样,那是因为你的浏览器已经过时了。幸运的是,您可以解决这个问题,它与更新web浏览器一样简单。下面是最流行的网络浏览器,请访问他们的网站并更新到新的浏览器

    function browseRight($t) { // pull browserCheck from the $GLOBALS array if (($t == 'css') && ($GLOBALS['browserCheck'] > 0)) { // etc... }
global $browserCheck 
<?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
        }
    }
?>
function browseRight($t)
{
    GLOBAL $browserCheck;
<?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
    }
}