• About
  • FAQ
  • Contact Us
DIYWebmastery WordPress Training, Consultancy, Web Design
  • Home
  • Our WordPress Plugins
    • AuthorSure
    • CaptionPix
    • Footer Putter
    • Genesis Club Lite
    • Genesis Club Pro
    • Genesis Dambuster
    • Slickr Flickr
    • SubscriptionBoss
  • Our Services
    • WordPress Site Design and Development
    • WordPress Individual Tuition
    • WordPress Site Review
    • WordPress Security and Forensics
    • Infusionsoft Consultancy
    • Online Business Strategy
  • Tutorials
  • Blog
Home » Tutorials » PHP One Line If Statement – Single Line If – Shorthand If

PHP One Line If Statement – Single Line If – Shorthand If

by Elizabeth

When writing PHP, I don’t know why but I keep having to research the PHP one line if statement again and again. I just can’t seem to remember it. Totally bizarre.

There are many names for this construct. The one liner if is also known as the ternary operator if you want to look it up in the PHP documentation.

It has also been called the short if statement and the single line if statement. If you are like me, and continually having to look this up – here you go:

One Line If Statement Example

[php]
$myvalue = 99;
$x = ($myvalue == 99) ? "x is 99": "x is not 99";
echo("<br>".$x);
[/php]

So if you were to run the above code, you’d find that because the value of $x had been initially set to 99, that the code would print out :

x is 99

Anything else would cause the program to print out :

x is not 99

Filed Under: Tutorials

Return To Top
  • About
  • Affiliates
  • Contact
  • Privacy Statement
  • Terms And Conditions
2009-2025 DIYwebmastery
The Studio · Hatherleigh · Devon · EX20 3NA · UK[email protected]

DIYWebmastery Plugins

AuthorSure CaptionPix ClickMastery Footer Putter Genesis Club Lite Genesis Club Pro Genesis Dambuster Slickr Flickr SubscriptionBoss