简体   繁体   中英

Can I shorten isset() and == in PHP?

I was wondering if there is a shorter way to perform this if condition:

if(isset($somVar) && $someVar == 'some value')

I tried:

if($someVar === 'some value')

If you can ensure that $someVar is always existing, you can skip the isset() part - otherwise you will get a PHP notice if the variable does not exist.

Also note that there is a substantial difference between == and === .

So I conclude that you cannot shorten that expression.

As far as I know there's not really another 'clean' way, however...

  1. You could turn off error reporting

  2. Alternatively, you could declare your variable in advance

     $somVar = null; // do what you must if ($somVar == 'some value') ... 
  3. Thirdly, you could manually suppress error messages (works in php 5.5)

     if (@$someVar == 'some value') ... 

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