首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PHP注意未定义变量

使用PHP注意未定义变量
EN

Stack Overflow用户
提问于 2015-07-07 12:27:34
回答 2查看 1.7K关注 0票数 3

我尝试了一段时间来修复这个问题,但我做不到。我找到了一些教程,但我无法修复它。

我和我的朋友在同一版本上工作,它在他的电脑上工作,没有任何问题--但对我来说不会。我们使用的是相同的文件,我是从他那里抄来的。

这是怎么回事,为什么我的电脑不能工作?

这是我的index.php

代码语言:javascript
复制
<?php
/* @var $this SystemManagementController */
/* @var $dataProvider CActiveDataProvider */
?>
<?php
$this->breadcrumbs = array(
    Yii::t('mainmenu', 'System Management'),
);

$contentTabUsers = $this->renderPartial('_tab_users', array(
    'model'              => $userModel,
    'columns'            => $userColumns,
    ), $return               = true);
$contentTabStates    = $this->renderPartial('_tab_states', array('model'                     => $stateModel), $return                    = true);
$contentTabPriorities    = $this->renderPartial('_tab_priorities', null, $return                     = true);
$contentTabProperties    = $this->renderPartial('_tab_properties', null, $return                     = true);

$upgradeLog = 'tbd'; //new UpgradeLog();

$systemInfo = new SystemInfo();
try
{
    $systemInfoData = array(
        'System Info'    => $systemInfo->getServerInfo(),
        'Apache'         => $systemInfo->getApacheInfo(),
        'MySQL Info'     => $systemInfo->getMysqlInfo(),
    );
}
catch (Exception $ex)
{
    Yii::log('Could not retrieve system info, exception thrown with message: ' . $ex->getMessage(), CLogger::LEVEL_ERROR);
    $systemInfoData = array();
}
$contentTabSysinfo = $this->renderPartial('_tab_sysinfo', array(
//  'model'                  => $upgradeLog,
//  'upgradeLogDataProvider' => $this->getUpgradeLogDataProvider(),
//  'upgradeScripts'         => $this->getAllInfoUpgradeScripts(),
    'systemInfo' => $systemInfoData,
    'phpinfo'    => $this->getBasicPhpInfo(),
    ), $return       = true
);




// get the filter value to show max lines
$showMaxLines    = (int) $this->getAppRequest()->getParam('log_show_max_lines', 50);
$contentTabLog   = $this->renderPartial('_tab_log', array(
    'applicationLog' => $this->getLog($showMaxLines),
    'showMaxLines'   => $showMaxLines,
    // 'log_show_max_lines' is a placeholder for the js value in the template
    'filterUrl'      => $this->getYiiApp()->createUrl('systemManagement/index', array('log_show_max_lines' => null)),
    ), $return               = true
);
Yii::app()->user->setState('activeSystemmanagementTab', 'system_info');

$tabs = array();
if (Yii::app()->user->checkAccess('Systemmanagement.users'))
{
    $tabs[Yii::t('systemmanagement', 'Users')] = array('content'     => $contentTabUsers, 'id'       => 'users');
}
if (Yii::app()->user->checkAccess('Systemmanagement.states'))
{
    $tabs[Yii::t('systemmanagement', 'States')] = array('content'    => $contentTabStates, 'id'      => 'states');
}
if (Yii::app()->user->checkAccess('Systemmanagement.priorities'))
{
    $tabs[Yii::t('systemmanagement', 'Priorities')] = array('content'    => $contentTabPriorities, 'id'      => 'priorities');
}
if (Yii::app()->user->checkAccess('Systemmanagement.properties'))
{
    $tabs[Yii::t('systemmanagement', 'Properties')] = array('content'    => $contentTabProperties, 'id'      => 'properties');
}
if (Yii::app()->user->checkAccess('Systemmanagement.sysinfo'))
{
    $tabs[Yii::t('systemmanagement', 'System Info')] = array('content'   => $contentTabSysinfo, 'id'         => 'system_info');
}
if (Yii::app()->user->checkAccess('Systemmanagement.log'))
{
    $tabs[Yii::t('systemmanagement', 'Log')] = array('content'   => $contentTabLog, 'id'         => 'log');
}

$this->widget('zii.widgets.jui.CJuiTabs', array(
    'tabs'       => $tabs,
    // additional javascript options for the tabs plugin
    'options'    => array(
        'collapsible'    => true,
        'hide'           => 'fade',
        'activeTab'      => Yii::app()->user->getState('activeSystemmanagementTab'),
//      'show'           => 'highlight',
        //TODO @see http://www.bsourcecode.com/2012/11/how-to-handle-cjuitabs-in-yii/
        'selected'       => isset(Yii::app()->session['tabid']) ? Yii::app()->session['tabid'] : 0,
        'select'         => 'js:function(event, ui) {
            var index=ui.index;
            $.ajax({
                "url":"' . Yii::app()->createUrl('site/tabidsession') . '",
                "data":"tab="+index,
            });
    }',
    )
    )
);
?>
<script type="text/javascript">
    function changeIsactive(id)
    {
        $.ajax({
            type: 'post',
            url: "<?php echo Yii::app()->createUrl('usp/AjaxSetuspOnOff') ?>",
            datatype: 'json',
            data: "MeId=" + id,
            success: function (data) {
                // if page access denied show the error msg
                var hasError = $("<div></div>").append(data).find("#content div.error").length > 0;
                if (hasError)
                {
                    $("#flashmsg").show().addClass('flash-error').html('<?php echo Yii::t('systemwide', 'You Are Not Authorized to Turn On/Off this ELement'); ?>').animate({opacity: 0.9}, 3500).fadeOut("slow");
                    return false;
                } else {
                    if (data != 'error')
                    {
                        if (data)
                        {
                            $('#onOff_' + id).addClass(data);
                        }
                        else {
                            $('#onOff_' + id).removeClass('checked');
                        }
                    }
                    else
                    {
                        $("#flashmsg").show().addClass('flash-error').html('<?php echo Yii::t('systemwide', 'You Are Not Authorized to Turn On/Off this ELement'); ?>').animate({opacity: 0.9}, 3500).fadeOut("slow");
                    }
                    return false;
                }
            },
            error: function (jqXHR, exception) {
                $("#flashmsg").show().addClass('flash-error').html('<?php echo Yii::t('systemwide', 'You Are Not Authorized to Turn On/Off this ELement'); ?>').animate({opacity: 0.9}, 3500).fadeOut("slow");
            }
        });
    }
</script>

当我转到服务器时,我会得到以下错误:

注意未定义变量:选项卡/var/www/private/protected/views/systemmanagement/index.php(84)

这指的是:

代码语言:javascript
复制
'tabs'       => $tabs,

为了解决这个问题,我在我的文件上添加了以下内容:

代码语言:javascript
复制
$tabs = array();

现在,当我这样做,它工作,它没有任何错误,但它只是转到页面,它没有显示任何内容。请帮助我在这件事上花了太多的时间。

如果我把这个写进我的代码里:

print_r($systemInfoData);

我得到:

代码语言:javascript
复制
Array ( [System Info] => Array ( [OS] => Linux #36-Ubuntu SMP Tue Apr 10 20:39:51 UTC 2012 [Machine Type] => x86_64 [Server Name] => 192.168.33.10 [Server IP Address] => 192.168.33.10 ) [Apache] => Array ( [version] => Apache/2.4.12 (Ubuntu) [Loaded Modules] => core, mod_so, mod_watchdog, http_core, mod_log_config, mod_logio, mod_version, mod_unixd, mod_access_compat, mod_alias, mod_auth_basic, mod_authn_core, mod_authn_file, mod_authz_core, mod_authz_groupfile, mod_authz_host, mod_authz_user, mod_autoindex, mod_cgi, mod_deflate, mod_dir, mod_env, mod_expires, mod_filter, mod_headers, mod_include, mod_mime, prefork, mod_negotiation, mod_php5, mod_reqtimeout, mod_rewrite, mod_setenvif, mod_status ) [MySQL Info] => Array ( [Server version] => 5.5.43-0ubuntu0.12.04.1 [Meta information] => Uptime: 11334 Threads: 1 Questions: 11476 Slow queries: 0 Opens: 76 Flush tables: 1 Open tables: 54 Queries per second avg: 1.012 ) )
EN

回答 2

Stack Overflow用户

发布于 2015-07-07 12:43:46

该问题是由未定义变量$tabs引起的。

正如其他供款人所正确提及的,你有两种选择:

I. (可取)

在使用变量之前定义变量。

II. (不推荐)

由于在您朋友的环境中设置了error_reporting级别,所以该错误不会显示在他/她的PC上。编辑在您的error_reporting中定义的php.ini级别。

为了隐藏php通知,在php.ini中添加或编辑下面一行

代码语言:javascript
复制
error_reporting = E_ALL & ~E_NOTICE;

或者,您可以直接从脚本中设置错误报告级别,如下所示:

代码语言:javascript
复制
// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

在php中阅读更多有关错误报告的信息:http://php.net/manual/en/function.error-reporting.php

票数 2
EN

Stack Overflow用户

发布于 2015-07-07 12:43:50

您正在获得Undefined variable错误,因为您的$tabs变量没有定义。

您有多个if语句可以定义它,但是如果它们都计算为false,那么它将仍然没有定义。

设置$tabs = array();定义变量,但它仍然没有内容。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31268536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档