【问题标题】:working with PHP notice Undefined variable使用 PHP 注意未定义变量
【发布时间】:2015-07-07 12:27:34
【问题描述】:

我正在尝试解决此问题,但我不能。我找到了几个教程,但我无法修复它。

我的朋友和我正在开发相同的版本,并且它可以在他的 PC 上正常运行 - 但对我来说不会。我们使用相同的文件,我从他那里复制了我的。

这是怎么回事?为什么这不能在我的电脑上运行?

这是我的 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 通知未定义变量: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 ) )

【问题讨论】:

标签: php mysql database yii vagrant


【解决方案1】:

这个问题是由于变量$tabs没有被定义造成的。

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

我。 (首选)

在使用它之前定义你的变量。

二。 (不推荐)

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

为了隐藏 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

【讨论】:

  • 当我添加这个没有任何变化时,
  • 这不是答案。
  • 我认为由于未定义变量而显示错误很明显。我的回答解决了最初问题中提到的环境之间的差异。
  • 这确实很明显,但显然不适用于似乎没有 PHP/编程经验的 OP。
  • 未预定义的变量是一个问题,但不是他的主要问题。他有 yii 身份验证的问题。除非他拥有适当的访问权限,否则他甚至不应该看到该页面。
【解决方案2】:

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

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

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

【讨论】:

  • 我正要说同样的话。你的问题在这里: Yii::app()->user->checkAccess('Systemmanagement.users') 你没有通过身份验证。
  • 什么意思??你没有通过身份验证??
  • 检查用户是否拥有'Systemmanagement.users'等权限。如果他们没有任何权限,则永远不会设置 $tabs。
  • 当我这样做时:$tabs = false;我收到此错误:Invalid argument supplied for foreach(), /vagrant/vendor/yiisoft/yii/framework/zii/widgets/jui/CJuiTabs.php(111)
  • @Alex,那是因为您正在尝试迭代 boolean 值。 foreach() 期待诸如数组之类的东西,例如 $tabs[] = false
猜你喜欢
  • 2021-03-07
相关资源
最近更新 更多