Skip to content

Instantly share code, notes, and snippets.

@jrtashjian
Created March 17, 2011 15:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrtashjian/874573 to your computer and use it in GitHub Desktop.
Save jrtashjian/874573 to your computer and use it in GitHub Desktop.
<?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');
}
@erkattak
Copy link

return ($poll_id = $this->EE->input->post('poll_id')) ? $poll_id : false;

@jrtashjian
Copy link
Author

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