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?

NikiC
  • 100,734
  • 37
  • 191
  • 225
MartyIX
  • 27,828
  • 29
  • 136
  • 207

9 Answers9

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.

NikiC
  • 100,734
  • 37
  • 191
  • 225
  • -1 since this leaves your tested variables floating around with NULL values if they didn't exist before the `getOr()` call. – salathe Nov 21 '11 at 19:57
  • 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. – ahmd0 Jan 30 '15 at 20:12
  • @ahmd0 PHP 7 will have `$params['myParam'] ?? 'defaultValue'` for this ("null coalesce operator"). – NikiC Jan 30 '15 at 20:25
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';
MartyIX
  • 27,828
  • 29
  • 136
  • 207
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.)

simshaun
  • 21,263
  • 1
  • 57
  • 73
1

This is what I use:

function getindex($arr, $index, $default = null) {
    return isset($arr[$index]) ? $arr[$index] : $default;
}
mmeyer2k
  • 422
  • 5
  • 10
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.

Jakub Riedl
  • 1,066
  • 2
  • 10
  • 27
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)){ ... }
m.e.conroy
  • 3,508
  • 25
  • 27
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.

str
  • 42,689
  • 17
  • 109
  • 127
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

matthiasmullie
  • 2,063
  • 15
  • 17
-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?

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 1
    Well, yes. They should. But it is a bit complicated in reality. You may inherit a legacy code for example. – MartyIX Nov 21 '11 at 18:51
  • 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. – Your Common Sense Nov 22 '11 at 03:44