【问题标题】:Codeigniter 4.1.1 shows undefined variable even after using isset functionCodeigniter 4.1.1 即使在使用 isset 函数后也显示未定义的变量
【发布时间】:2021-02-09 10:25:18
【问题描述】:

我在名为 Body 的主视图中使用了多个视图。我没有声明$sub_page,它显示undefined variable。我尝试将其声明为null,但仍然执行了 if 块。

错误

功能

public function index()
    {
        $Main = model('App\Models\mainModel');
        $fields = ["id","product_name","product_uid","product_price","instock"];
        $data = [
            'site' => "Billing",
            'page' => "Products",
            'table' => "products",
            'data' => $Main->get_data("products",$fields),
            'is_verify' => false,
            'is_action' => true,
            'fields' => $fields
        ];
        return view('Body',$data);
    }

面包屑视图

<div class="content-header">
    <div class="d-flex align-items-center">
        <div class="mr-auto">
            <h3 class="page-title"><?=$page?></h3>
            <div class="d-inline-block align-items-center">
                <nav>
                    <ol class="breadcrumb">
                        <li class="breadcrumb-item"><a href="<?=base_url()?>"><i class="mdi mdi-home-outline"></i></a></li>
                        <li class="breadcrumb-item" aria-current="page"><?=$page?></li>
                        <?if(isset($sub_page)):?>
                        <li class="breadcrumb-item active" aria-current="page"><?=$sub_page?></li>
                        <?endif;?>
                    </ol>
                </nav>
            </div>
        </div>

    </div>
</div>

身体视图

   <div class="wrapper">
        <?=view("Header")?>
        <?=view("Navigation")?>
        <!-- Content Wrapper. Contains page content -->
        <div class="content-wrapper">
            <div class="container-full">
                <!-- Content Header (Page header) -->
                <?=view("Breadcrumb")?>
                <section class="content">
                    <div class="row">
                        <?if(isset($views)):?>
                        <?foreach ($views as $view):?>
                        <?=view($view)?>
                        <?endforeach;?>
                        <?endif;?>
                        <?if(isset($table)):?>
                        <?=view("Datatable")?>
                        <?endif;?>
                    </div>
                </section>
            </div>
        </div>
        <?=view("Footer")?>
    </div>

为什么会出现这个错误,如何解决它

【问题讨论】:

  • 试试var_dump($sub_page)你的变量,看看它是否不为空。处理它。
  • 我刚刚发现页面源代码显示 PHP 标记 &lt;??&gt;,但其他 PHP 文件工作正常,它只显示在这个项目文件夹中

标签: php codeigniter-4


【解决方案1】:

问题是在.htaccess 文件中添加php_flag short_open_tag on 后的short_open_tag 解决了我的问题。 php.ini 有short_open_tag=On 但它仍然显示错误但在.htaccess 中添加上面的标签解决了问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2016-02-17
    • 2022-08-18
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多