johnmerlino Posted March 16, 2011 Share Posted March 16, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230806-equivalent-double-pipe-syntax-to-check-if-variable-already-initialized-in-php/ Share on other sites More sharing options...
kenrbnsn Posted March 16, 2011 Share Posted March 16, 2011 I think what you're looking for is something like <?php if (!isset($b)) { $b = $a; } ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/230806-equivalent-double-pipe-syntax-to-check-if-variable-already-initialized-in-php/#findComment-1188203 Share on other sites More sharing options...
johnmerlino Posted March 16, 2011 Author Share Posted March 16, 2011 Yeah that would work. Quote Link to comment https://forums.phpfreaks.com/topic/230806-equivalent-double-pipe-syntax-to-check-if-variable-already-initialized-in-php/#findComment-1188208 Share on other sites More sharing options...
AbraCadaver Posted March 16, 2011 Share Posted March 16, 2011 Or since you brought up ternary: $b = isset($b) ? $b : $a; Quote Link to comment https://forums.phpfreaks.com/topic/230806-equivalent-double-pipe-syntax-to-check-if-variable-already-initialized-in-php/#findComment-1188231 Share on other sites More sharing options...
johnmerlino Posted April 3, 2011 Author Share Posted April 3, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/230806-equivalent-double-pipe-syntax-to-check-if-variable-already-initialized-in-php/#findComment-1196314 Share on other sites More sharing options...
AbraCadaver Posted April 3, 2011 Share Posted April 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/230806-equivalent-double-pipe-syntax-to-check-if-variable-already-initialized-in-php/#findComment-1196378 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.