简体   繁体   中英

PHP - if isset shorthand

Quick question. I have seen in tutorials that simple if statement is written like this:

if($vest) echo $vest->naslov

But when I write it like that, I get an error Undefined variable: vest and I have to write the code old fashion way:

if(isset($vest)) echo $vest->naslov

Are there any setting in the php.ini file that regulate this? At the moment I am using LAMP on Ubuntu (latest version).

You have E_STRICT on, which complains if you don't have variables declared before checking them. You could fix this by turning off E_STRICT in php.ini , but a better idea would be to simply declare your variables ahead of time - just do this, at the top of your function:

$vest = null;

before the rest of your logic. This is good practice, because it forces you to think about the way your program executes in a way that more closely matches reality.

You can lower error_reporting to E_ALL ^ E_NOTICE , but it's actually a bad practice which can lead to a lot of hard to track down bugs. if $vest is a typo, you won't know, and your conditions will never work.

Instead, try to structure your application so you always know which variables are defined and which aren't. Then, you can use the shorthand version without worry.

Undefined variable: vest error mean that variable $vest is not recognized. The reason isset($vest) does not generate error is because isset() check whether $vest is already set, or not. Therefore if(isset($vest)) will do a boolean check to the return value of isset($vest) .

If you just use if($vest) , php will assume $vest is already set (when in fact, $vest is still undefined) and it do a boolean check operation.

You can turn down the error reporting level and ignore the error, but that would be dangerous programming practice.

When you type this: if($vest) , do you really mean $vest==null or $vest=='' or $vest==='' or isempty($vest) or $vest===true or isset($vest)`?

There are a lot of uses for if statements; you should make it clear in your code what exactly you are doing. Is it really so hard to initialize the variable at the top of your script?

I would say it's better practice to declare your variables to the negative state, then - if they are not set, or they are set to false then - the shorthand if will act as you intend.

Ignoring errors is bad (they are there for a reason, and your code will be more readable, therefore maintainable).

if($foo) and if(isset($foo)) are not the same thing and shouldn't be used interchangeably.

if($foo) checks wheter or not $foo evaluates to true. Another way of writing that would be: if( (bool) $foo === true ) . Therefore, if $foo is undefined, a notice from PHP is expected.

Rather than turning off notices, like some people suggested, write your code the correct way. To check if $foo is defined, always use isset() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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