简体   繁体   English

PHP 变量在定义之前不会打印

[英]PHP variable won't print before defining it

I'm using a portable WAMP package "USB web server".我正在使用便携式 WAMP package“USB web 服务器”。 It is giving error if i try to echo some variable directly, for example例如,如果我尝试直接回显某些变量,则会出错

<?php
echo $abc;  
?>

The error i get is: "Notice: Undefined variable: abc "我得到的错误是:“注意:未定义的变量:abc”

This will work fine:这将正常工作:

<?php
$abc = "foo";
echo $abc;  
?>

Is it possible to fix this error?是否可以修复此错误? I am bound to use portable WAMP package as I do no thave administrator privileges, so i can not install any other package.我必须使用便携式 WAMP package,因为我没有管理员权限,所以我不能安装任何其他 package。

Well, apart from why I don't know why you would want to echo something that is not defined, you're not getting an error, you're getting a notice.好吧,除了为什么我不知道为什么要回显未定义的内容之外,您没有收到错误,而是收到了通知。

If you want to do this, you've got to ignore notices in your production environment, like you can read here works like this:如果你想这样做,你必须忽略生产环境中的通知,就像你可以在这里阅读的那样:

// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting(E_ALL ^ E_NOTICE);

What should it print if it is not defined?如果没有定义它应该打印什么?

You can check if it is defined before you try to print it您可以在尝试打印之前检查它是否已定义

if(isset($abc))
{
    echo $abc;
}

You can also suppress the error by appending an @ sign in front of the line您还可以通过在行前附加 @ 符号来抑制错误

@echo $abc;

But that is not recommended and can be slower.但不建议这样做,可能会更慢。

to remove notice and warning write删除通知和警告写

error_reporting(E_ALL ^ E_NOTICE); at the top of the page在页面顶部

The variable is indeed not defined.该变量确实没有定义。 What exactly would you like to happen?你到底想发生什么?

If you don't want to see the error, then define it.如果您不想看到错误,请定义它。

<?php
$abc = '';
echo $abc;
?>

Well, the answer is to fix the code so you aren't accessing variables that have not been defined.好吧,答案是修复代码,这样您就不会访问尚未定义的变量。 If you need to prevent it from printing errors which is not the correct solution (*), then you can switch off notices in your error reporting:如果您需要防止它打印不是正确解决方案的错误 (*),那么您可以关闭错误报告中的通知:

error_reporting(E_ALL ^ E_NOTICE);

The correct solution is to fix the code.正确的解决方案是修复代码。

Check if it exists before echoing it在回显之前检查它是否存在

if( isset($abc) ) { echo $abc } if( isset($abc) ) { 回声 $abc }

you could of course also just check if the variable exists.你当然也可以检查变量是否存在。

<?php if (isset($myvar)) echo $myvar; ?>

cu Roman古罗马

You could also suppress the warning on the echo command.您还可以抑制 echo 命令上的警告。 Disabling warnings in general is dangerous.通常禁用警告是危险的。

<?php
    @echo $abc;
?>

One of the earliest principle you learn in programming is to declare your variables .您在编程中学习的最早原则之一是声明变量 Therefore, to fix this error , declare your variables .因此,要修复此错误请声明您的变量

However, it may happen in multiple cases that you don't know if the variable has been defined or not, but still want to avoid the error.但是,在多种情况下可能会发生您不知道变量是否已定义,但仍想避免错误的情况。

In those cases, check for its existence first:在这些情况下,首先检查它的存在:

// print $abc if set, or print an empty string/nothing
echo isset($abc) ? $abc : '';
isset($abc) && print $abc;
if (isset($abc)) echo $abc;

The three lines above will give the exact same result one another.上面的三行将给出完全相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM