donglu5000 2011-09-28 22:47
浏览 56

是否可以在不获取“未定义变量”消息的情况下调用未定义的变量?

I'm trying to make a simple little templating system where I don't want to define every variable that's available in the template. For example.

Every page would have

<?php displayHTML($variable); ?> 

but only some pages would actually have $variable defined.

$variable = "something";

In included functions.php I have tried

function displayHTML($variable){
if(isset($variable)){
echo $variable;
}else{
echo '';
        }
}

But the error seems to be coming from the fact that $variable isn't defined rather than not being set with a value. This error only shows with E_ALL enabled and I know I can shut it with @ or simply defining $variable across the board. Any function or best practice to avoid having to do this?

I know this seems weird but I would like to option to not define every variable if its not gonna be used on a page.

  • 写回答

4条回答 默认 最新

  • dowy77780 2011-09-28 22:51
    关注

    Define each variable before you use it.

    If your templates are trying to use variables that don't exist, then that is a logical fault with your design, and you should fix that.

    Give them some "default" or empty value if that's the visualisation that you wish to assign to those values.

    Leaving them undefined is simply error-prone and, forgive me for saying so, lazy.


    Some people would tell you to use the @ error-suppressant. This is a fast, cheap, hacky workaround to an underlying fault... and my advice would be to steer well clear of it.

    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等