I can't seem to find out what the exact control structures are for PHP if/else.
Currently I have this function but can anyone tell me if PHP is just looking for the "tabs"? And is it possible to trigger multiple functions within just 1 statement? Like now its just redirecting to a route but lets say I want to like email a user when hes updated before the redirect. Is this still possible with shorthand?
public function store()
{
return ($user = $this->user->store(Input->all())) ?
Redirect::route('user.index')
->with('flash_message', 'User succesfully created!', ['user' => $user]) :
Redirect::route('user.create')
->withInput()
->withErrors($this->user->getErrors());
}
This is the documentation for php if/else:
http://www.php.net/manual/en/control-structures.elseif.php
? :
structure is a shorthand for if/else, but it isn't a good practice to use it for complex things. It makes your code unreadable. http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary
Otherwise php doesn't care about whitespaces.
With if/else, it would look like this:
public function store()
{
if ($user = $this->user->store(Input->all()))
{
return Redirect::route('user.index')
->with('flash_message', 'User succesfully created!', ['user' => $user]);
}
else
{
return Redirect::route('user.create')
->withInput()
->withErrors($this->user->getErrors());
}
}
It would be easier to read still if the if
didn't do the assignment. eg:
public function store()
{
$user = $this->user->store(Input->all())
if ($user)
{
return Redirect::route('user.index')
->with('flash_message', 'User succesfully created!', ['user' => $user]);
}
else
{
return Redirect::route('user.create')
->withInput()
->withErrors($this->user->getErrors());
}
}