Created
March 17, 2011 15:52
-
-
Save jrtashjian/874573 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* The only function of this statement is to return $poll_id if it's set. Which | |
* snippet would you use and why? | |
* | |
* I (@jrtashjian) prefer and wrote snippet 1. I like how contained the | |
* function is, and the fact I'm only calling the post() function once. | |
* | |
* Each snippet works correctly, this is just preference. I find it redundant | |
* to call the post() function more than once (as shown in snippet 3). I also | |
* find it unnecessary to create a variable ($poll_id) to store the result of | |
* function post() when I'll only be using it for the if statement. | |
* | |
* Let me know on Twitter, @jrtashjian. | |
*/ | |
// Snippet 1 | |
if( $poll_id = $this->EE->input->post('poll_id') ) | |
{ | |
return $poll_id; | |
} | |
// Snippet 2 | |
$poll_id = $this->EE->input->post('poll_id'); | |
if( $poll_id ) | |
{ | |
return $poll_id; | |
} | |
// Snippet 3 | |
if( $this->EE->input->post('poll_id') ) | |
{ | |
return $this->EE->input->post('poll_id'); | |
} |
I probably should have said that this is a statement which short-circuits a function if $poll_id is set. So, if $poll_id is not set, the function continues to run.
So, you are right in that syntax if it's the final return statement. Wouldn't work for this instance (which I should have stated) but I would absolutely use this in the right circumstances.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
return ($poll_id = $this->EE->input->post('poll_id')) ? $poll_id : false;