WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Mar 20 2011

PHP Tertiary Operator and Complex Conditional Assignment: Variable = Condition ? True Statement : False Statement

If you've been dealing with WordPress or PHP, more than likely you have seen the Ternary Operator in a statement. If you were like me, when you saw it for the first time, you had no idea what sort of assignment statement it was except possibly a conditional assignment. So what is it? The Ternary Operator is an operator that takes three (3) arguments: Condition, True Statement, and False Statement. So a complex conditional assignment is the same as a ternary operator but with more than three (3) arguments.

From Wikipedia: Ternary Operation:
[php]if ($a > $b) {
$result = $x;
} else {
$result = $y;
}
[/php]

Rewritten using the ternary operator:
[php]$result = ($a > $b) ? $x : $y;[/php]

So,
[php]$my_var = ($var1 > $var2) ? /*simple true statement */ : /*simple false statement */; [/php]

From the PHP manual:
[php]<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

// The above is identical to this if/else statement
if (empty($_POST['action'])) {
$action = 'default';
} else {
$action = $_POST['action'];
}

?>
[/php]

To create a switch-like conditional assignment as demonstrated in Wikipedia: Conditional Assignment ?:
[php]<?php
$arg = "T";
$vehicle = ($arg == 'B') ? 'bus' :
(($arg == 'A') ? 'airplane' :
(($arg == 'T') ? 'train' :
(($arg == 'C') ? 'car' :
(($arg == 'H') ? 'horse' :
'feet'))));
echo $vehicle;
?>
[/php]

Written by Travis Smith · Categorized: WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Bluesan says

    May 17, 2011 at 4:14 am

    1) How to delete ‘Read More link’ from home page ‘excerpt’?

    2) How to delete the ‘date and admin link’ shown on the featured properties on the home page and on the post pages?

    Reply
    • wpsmith says

      May 17, 2011 at 10:12 am

      Hello Nick,

      Thank you for your comment. To remove the Read More Link, simply uncheck or delete the text in the Read More input box on the Widgets page. To delete the date and admin link, are you referring to the post information? If so, check out this post: https://wpsmith.net/frameworks/how-to-edit-author-date-time-comments-link-tags-and-categories-post-information-in-genesis/

      Thanks,

      Travis

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in