Is there a shortcut for the "isset construct"?
Asked Answered
S

9

11

I'm writing quite often this line of code:

$myParam = isset($params['myParam']) ? $params['myParam'] : 'defaultValue';

Typically, it makes the line very long for nested arrays.

Can I make it shorter?

Silken answered 21/11, 2011 at 18:25 Comment(0)
S
4

PHP 7 will contain ?? operator that does exactly that.

See https://wiki.php.net/rfc/isset_ternary, example:

// Fetches the request parameter user and results in 'nobody' if it doesn't exist
$username = $_GET['user'] ?? 'nobody';
// equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
Silken answered 28/9, 2014 at 6:15 Comment(0)
O
14
function getOr(&$var, $default) {
    if (isset($var)) {
        return $var;
    } else {
        return $default;
    }
}

$myParam = getOr($params['myParam'], 'defaultValue');

Be sure to pass the variable by reference though, otherwise the code will produce a E_NOTICE. Also the use of if/else instead of a ternary operator is intentional here, so the zval can be shared if you are using PHP < 5.4.0RC1.

Oxygen answered 21/11, 2011 at 18:31 Comment(3)
-1 since this leaves your tested variables floating around with NULL values if they didn't exist before the getOr() call.Loralorain
I don't really get the comment above. @NikiC: Thanks for pointing it out. This should've been a language operator or at least a built-in function.Undemonstrative
@Undemonstrative PHP 7 will have $params['myParam'] ?? 'defaultValue' for this ("null coalesce operator").Oxygen
S
4

PHP 7 will contain ?? operator that does exactly that.

See https://wiki.php.net/rfc/isset_ternary, example:

// Fetches the request parameter user and results in 'nobody' if it doesn't exist
$username = $_GET['user'] ?? 'nobody';
// equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
Silken answered 28/9, 2014 at 6:15 Comment(0)
C
3

Yes, by making a proxy function, but is it really worth it?

Also, isset is a language construct, so wrapping it in a proxy function will degrade performance, although the degradation will likely be less than trivial (not even really worth mentioning.)

Cirrostratus answered 21/11, 2011 at 18:29 Comment(2)
Function will generate a E_NOTICE warning, won't it? I can't accept an E_NOTICE warning (because of code standards).Silken
I was hoping in a some kind of PHP operator for this.Silken
A
1

This is what I use:

function getindex($arr, $index, $default = null) {
    return isset($arr[$index]) ? $arr[$index] : $default;
}
Aquatint answered 3/12, 2012 at 16:45 Comment(0)
S
0

As of PHP 5.3 you can use:

$myParam = $params['myParam'] ?: 'defaultValue';

Note, however, that $params['myParam'] and isset($params['myParam']) are not 100% the same.

Spyglass answered 21/11, 2011 at 18:32 Comment(4)
-1 Note the "isset()" in the OPs question. I believe that your example won't behave the same way.Embankment
I just edited it. For a lot of use cases it will result in the same behaviour.Spyglass
What if $params['myParam'] = false and I want $myParam to be false?Medford
@Medford As I said, they are not 100% equivalent.Spyglass
C
0

No. Unfortunately, you can't. Not in a decent way. You'll at least have to give in on performance.

Update: since PHP7, ?? will do just that. See https://wiki.php.net/rfc/isset_ternary

Continuate answered 21/11, 2011 at 18:40 Comment(0)
W
0

I'm using little this little magic class which works as variable

class Post() {
 private $post = Array();
 public function __construct() {
  $this->post = $_POST;
 }
 public function __get($name) {
  return @$this->post[$name];
 }
 public function __set($name, $value) {
  return $this->post[$name] = $value;
 }
 public function __call($function, $params) {
  if(isset($this->post[$function])) {
   return $this->post[$function];
  } else {
   $this->post[$function] = $params[0];
   return $params[0];
  }
 }
}
$post = new Post();

then in document you can use it easily as any other variable so for example $post->name $post->somelist[2] or with default value $post->name("John Doe") and after that you got it returned as well as stored.

Widget answered 30/7, 2013 at 13:51 Comment(0)
T
0

I know this doesn't shorten anything up for you but thought I'd just share this, I use this alot in my applications to make sure something is set and has a value.

function is_blank($var = NULL){
    return empty($var) && !is_numeric($var) && !is_bool($var);
}    

function chk_var($var = NULL){
    return (isset($var) && !is_null($var) && !is_blank($var));
}

Then...

if(chk_var($myvar)){ ... }
Traitorous answered 4/9, 2013 at 21:15 Comment(0)
O
-1

You if you have to do it often, you are probably missing the point.

In fact, variables should be defined before use.
So, there oughtn't be a case when you have your param undefined.
Just create a default params file, and initialize every your variable.

$params['myParam'] = 'defaultValue'; 

later it can be changed under some circunstances but it never be undefined.
Got the idea?

Orometer answered 21/11, 2011 at 18:47 Comment(2)
Well, yes. They should. But it is a bit complicated in reality. You may inherit a legacy code for example.Silken
Nothing complicated in reality. Anyway, this is the only answer presenting a good practice, not following bad one, so, I hope it deserves the right to be here, even if someone believes it doesn't suit them.Orometer

© 2022 - 2024 — McMap. All rights reserved.