2
if ($form->isValid()) {
// ... perform some action, such as saving the task to the database

$nextAction = $form->get('saveAndAdd')->isClicked()
    ? 'task_new'
    : 'task_success';

return $this->redirect($this->generateUrl($nextAction));
}

Here is the link to the documentation

http://symfony.com/doc/current/book/forms.html

The class documentation says that it returns a bool.

What is the point of

? 'task_new' 
: 'task_sucess'; 
user1599755
  • 149
  • 1
  • 6
  • 1
    http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary – CBroe Jan 29 '14 at 15:18

6 Answers6

6

That is called "ternary" and it's awesome:

This is assigning the value $nextAction based on a condition. The first part (after the =) is the condition, like an if statement, the second part (after the ?) is the value assigned if the condition is true, and the last part (after the :) is the value assigned if the condition is false.

               //the condition
$nextAction = $form->get('saveAndAdd')->isClicked()
    ? 'task_new' //true value
    : 'task_success'; //false value

It is a shorter way of writing this:

if ($form->get('saveAndAdd')->isClicked()) {
  $nextAction = 'task_new';
}
else {
  $nextAction = 'task_success';
}

So, here's some easy examples:

$foo = (true) ? 'True value!' : 'False value!';
echo $foo; //'True value!' of course!

$foo = (false) ? 'True value!' : 'False value!';
echo $foo; //'False value!' of course!
m59
  • 43,214
  • 14
  • 119
  • 136
3

It's the Ternary operator. The syntax is as follows:

value = (condition) ? run if true : run if false;

In this case, if $form->get('saveAndAdd')->isClicked() is true, then task_new. Else task_success.

If could be rewritten like so:

if($form->get('saveAndAdd')->isClicked()) {
    $value = "task_new";
} else {
    $value = "task_success";
}
War10ck
  • 12,387
  • 7
  • 41
  • 54
1

The ternary operator is a shorter form for an if statement. The : is the "else" part. Example in Java:

boolean bool;
true ? bool = true : bool = false;

It's a senseless example, but shows the ternary operator very well. if the condition, here true is "true", then fill into the variable bool true, else false.

alternative if-statement in Java to the code example above:

boolean bool;

    if(true)
        bool = true;
    else
        bool = false;
  • 1
    welcome to stackoverflow! this answer would be better, if you posted an example of proper use and how it translates to an if statement. – Corley Brigman Jan 29 '14 at 15:35
0

This is a Ternary Operator which is a short hand if else statement. This is equivalent to

if($form->get('saveAndAdd')->isClicked()){
    $nextAction = 'task_new'
else{
    $nextAction = 'tassk_success'
}
Sehael
  • 3,678
  • 21
  • 35
0

This is the ternary opeator, a short-hand expression that works the same as an if

$value = someFunc() ? "whatever" : "the other"

is equivalent to

if (someFunc()) {
  $value = "whatever";
} else {
  $value = "the other";
}
Carlos Campderrós
  • 22,354
  • 11
  • 51
  • 57
0

This is equivalent to "if" and "else" statements.

This code :

$nextAction = $form->get('saveAndAdd')->isClicked()
    ? 'task_new'
    : 'task_success';

is equivalent to this code :

if ( $form->get('saveAndAdd')->isClicked() )
{
    $nextAction = 'task_new';
}
else
{
    $nextAction = 'task_success';
}
Benjamin Bini
  • 311
  • 4
  • 15