/    Sign up×
Community /Pin to ProfileBookmark

Various Ways Of The Ternary Operator

Folks,

Experimented like this about a month ago.
Look at the various ternary operator you can write:

1.

[code]
<?php
//Example 1
$i = 1;

//if(condition ? THEN : ELSE).
if($i == 1 ? $i++ : 1);

echo $i; echo ‘<br>’;
?>
[/code]

2.

[code]

<?php
//Example 2
$i = 1;

//condition ? THEN : ELSE.
$i == 1 ? $i++ : 1;

echo $i; echo ‘<br>’;

?>
[/code]

3.

[code]
<?php
//Example 3
$i = 1;

/*if(condition ?
THEN :
ELSE);
*/
if($i != 1 ?
$i=1 :
$i++);

echo $i; echo ‘<br>’;
?>
[/code]

4.

[code]
<?php
//Example 4
$i = 1;

/*condition ?
THEN :
ELSE.
*/
$i != 1 ?
$i=1 :
$i++;

echo $i; echo ‘<br>’;
?>
[/code]

5.

[code]
<?php
//Example 5
$i = 1;

/*if(condition
? THEN
: ELSE).
*/
if($i != 1
? $i=1
: $i++);

echo $i; echo ‘<br>’;
?>
[/code]

6.

[code]
<?php
//Example 6
$i = 1;

/*condition
? THEN
: ELSE.
*/
$i != 1
? $i=1
: $i++;

echo $i; echo ‘<br>’;
?>
[/code]

7.

[code]
<?php

$default = 0;

//(Do this if result is TRUE ? : ELSE).
if($default++ ? : 1);

echo $default; echo ‘<br>’;
?>
[/code]

8.

[code]
<?php
$default = 0;

//Do this if result is TRUE ? : ELSE.
$default++ ? : 1;

echo $default; echo ‘<br>’;
?>
[/code]

9.

[code]
<?php
$default = 1;

//(Do this if result is TRUE ? : ELSE).
if($default++ ? : 1);

echo $default; echo ‘<br>’;
?>
[/code]

10.

[code]
<?php

$default = 1;

//Do this if result is TRUE ? : ELSE.
$default++ ? : 1;

echo $default; echo ‘<br>’;
?>
[/code]

Do you know of any more ways of shortcutting the ternary operator ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 28.2021 — At a quick glance, they all look like a waste of time unless your objective is to write hard to maintain code that is likely to have hidden bugs in it. Do you actually understand what each of those is doing and have some reason why you would want to do it that way?
Copy linkTweet thisAlerts:
@developer_webauthorOct 28.2021 — @NogDog#1638781

Yeah, I understand them. Was learning the various ways to see which is the shortest.

https://www.phptutorial.net/php-tutorial/php-ternary-operator/

Do checkout that link.
Copy linkTweet thisAlerts:
@developer_webauthorOct 28.2021 — @NogDog#1638781

NogDog,

Have you ever come across chaining ternary operators ?

https://www.phptutorial.net/php-tutorial/php-ternary-operator/

<i>
</i>&lt;?php

$eligible = true;
$has_credit = false;

$message = $eligible
? ($has_credit
? 'Can use the credit'
: 'Not enough credit')
: 'Not eligible to buy';

echo $message;


To me, above code doesn't seem to complicated. But this one does ....
<i>
</i>&lt;?php

$path = '/about';
$url = $path ?: '/';

echo $url; // /about

It's hard to remember the code.

Formula: **$result = $initial ?: $default;**
Copy linkTweet thisAlerts:
@boohooOct 29.2021 — Remember that the shortest doesn't necessarily mean the best. Readability is very important. And for me, I sometimes use nested ternary, but it's not the cleanest to read for sure.
Copy linkTweet thisAlerts:
@developer_webauthorOct 30.2021 — @boohoo#1638815

Yeah. Without the nests, I also get confused. The more the nests, the better to figure-out the code when you are checking at a later date. Agree ?

Anyway, since it seems like you know what you are talking about then how-about you engage in my threads a lot more when you get the times ? Do checkout my profile for my threads.

Cheers!
Copy linkTweet thisAlerts:
@NogDogOct 30.2021 — > @developer_web#1638838 The more the nests, the better to figure-out the code

No: the more nesting of any sort, the more it's time to refactor your code to get rid of the nesting. (Assuming you're using the term "nesting" meaning things embedded in things embedded in things... as opposed to just visually typing any nesting you do have across multiple lines with indenting, which is more likely to be a good thing.)
×

Success!

Help @developer_web spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...