使用PHP注意未定义的变量

时间:2015-07-07 12:27:35

标签: php mysql database yii vagrant

我正试图解决这个问题,但我无法解决。我找到了几个教程,但我无法修复它。

我和我的朋友正在研究相同的版本,它在他的电脑上运行没有任何问题 - 但对我而言,它不会。我们使用相同的文件,我从他那里复制了我的文件。

这里有什么问题以及为什么在我的电脑上赢得这项工作?

这是我的index.php

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

当我去服务器时出现此错误:

  

PHP notice未定义变量:tabs /var/www/private/protected/views/systemmanagement/index.php(84)

那指的是:

'tabs'       => $tabs,

为了解决这个问题,我添加了以下文件:

$tabs = array();

现在,当我执行此操作时,它可以正常运行并且不会出现任何错误,但它只会转到页面,并且不会显示任何内容。请帮助我花太多时间在这上面。

如果我把它放在我的代码中:

  

的print_r($ systemInfoData);

我明白了:

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 ) )

2 个答案:

答案 0 :(得分:2)

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

您有两个选择,正如其他贡献者正确提到的那样:

<强>予。 (优选的)

在使用之前定义变量。

<强> II。 (不推荐)

由于他/她的环境中设置了error_reporting级别,因此您朋友的PC上未显示错误。修改error_reporting中定义的php.ini级别。

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

error_reporting = E_ALL & ~E_NOTICE;

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

// Report all errors except E_NOTICE
error_reporting(E_ALL & ~E_NOTICE);

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

答案 1 :(得分:1)

您收到Undefined variable错误,因为未定义$tabs变量。

您有可以定义的多个if语句,但如果它们都评估为false,则它将保持未定义。

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