Jump to content

equivalent double pipe syntax to check if variable already initialized in PHP?


johnmerlino

Recommended Posts

Hey all,

 

Let's say I want to do something like this:

 

<?php
function exists($a){
	$b ||= $a;
}

echo exists(1);
?>

Basically, every time exists is called, it will only assign b to a if b  isn't already initialized.

 

What's the most effective way to achieve the equivalent in php? ternary?

 

Thanks for response.

  • 3 weeks later...

Or since you brought up ternary:

 

$b = isset($b) ? $b : $a;

 

That won't work if the result is returning a function:

 

$limit = isset($this->input->get('limit')) ? $this->input->get('limit') : 25;

 

Well no, but that wasn't their example.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.