Return to Snippet

Revision: 54555
at June 17, 2012 00:53 by beneberle


Updated Code
<?php

$var = [condition] ? [true] : [false];

/* the above statement is the same as this more verbose syntax: */

if (condition) {
    $var = [true];
} else {
    $var = [false];
}

//Example

$isNew = false;
$output = $isNew ? 'New' : 'Not New';
print($output); // ='Not New';

$isNew = true;
$output = $isNew ? 'New' : 'Not New';
print($output); // ='New';

?>

Revision: 54554
at January 3, 2012 16:32 by beneberle


Updated Code
<?php

$var = [condition] ? [true] : [false];

/* the above statement is the same as this enlarged structure: */

if (condition) {
    $var = [true];
} else {
    $var = [false];
}

//Example

$isNew = false;
$output = $isNew ? 'New' : 'Not New';
print($output); // ='Not New';

$isNew = true;
$output = $isNew ? 'New' : 'Not New';
print($output); // ='New';

?>

Revision: 54553
at January 3, 2012 16:25 by beneberle


Initial Code
<?php

$var = [condition] ? [true] : [false];

/* the above statement is the same as this enlarged structure: */

if (condition) {
    $var = [true];
} else {
    $var = [false];
}

//Example

$isNew = false;
$output = $isNew ? 'Add Form' : 'Edit Form - '.$name;
print($output); // 'Edit Form - ';

$isNew = true;
$output = $isNew ? 'Add Form' : 'Edit Form - '.$name;
print($output); // 'Add Form';

?>

Initial URL

                                

Initial Description
Condition = The condition which must be met.
True = Executed if the condition is met.
False = Executed if the condition failes.

Initial Title
Ternary Conditional Syntax

Initial Tags
php

Initial Language
PHP