One Line If Statement in Php

I'd like to to some thing similar to JavaScript's

var foo = true;
foo && doSometing();

but this doesn't seem to work in PHP.

I'm trying to add a class to a label if a condition is met and I'd prefer to keep the embedded PHP down to a minimum for the sake of readability.

So far I've got:

<?php $redText='redtext ';?>
<label class="<?php if ($requestVars->_name=='')echo $redText;?>labellong">_name*</label>
<input name="_name" value="<?php echo $requestVars->_name; ?>"/>

but even then the IDE is complaining that I have an if statement without braces.

8 Answers

use the ternary operator ?:

change this

<?php if ($requestVars->_name == '') echo $redText; ?>

with

<?php echo ($requestVars->_name == '') ? $redText : ''; ?>

In short

// (Condition)?(thing's to do if condition true):(thing's to do if condition false);
1

You can use Ternary operator logic Ternary operator logic is the process of using "(condition)? (true return value) : (false return value)" statements to shorten your if/else structures. i.e

/* most basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
1

Something like this?

($var > 2 ? echo "greater" : echo "smaller")
1

I like to use the minimalist PHP text output syntax:

HTML stuff <?= $some_string ?> HTML stuff

(This works the same as using an <?php echo $some_string; ?>)

You can also use the ternary operator:

//(condition) ? (do_something_when_true) : (do_something_when_false);
($my_var == true) ? "It's true" : "It's false ;

Ending up like this:

<?= ($requestVars->_name=='') ? $redText : '' ?>

Use ternary operator:

echo (($test == '') ? $redText : '');
echo $test == '' ? $redText : ''; //removed parenthesis

But in this case you can't use shorter reversed version because it will return bool(true) in first condition.

echo (($test != '') ?: $redText); //this will not work properly for this case

Sample Usage

Here are a couple more uses of ternary operators, ranging from simple to advanced:

Basic Usage:

$message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest');

Short hand Usage:

$message = 'Hello '.($user->get('first_name') ?: 'Guest');

Echo Inline

echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody');

A bit Tougher

$score = 10;
$age = 20;
echo 'Taking into account your age and score, you are: ',($age > 10 ? ($score < 80 ? 'behind' : 'above average') : ($score < 50 ? 'behind' : 'above average')); // returns 'You are behind'

complicated level

 $days = ($month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year %400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31)); //returns days in the given month

To learn more about ternary operators and usage, visit PHP.net Comparison Operators or here.

Ill provide with an other answer since the original question specifies the use of if() in html

<a class="menu-item" href="/about-us"><?= (pll_current_language() == 'en') ? 'About us' : 'Om oss' ?></a>

The provided answers are the best solution in your case, and they are what I do as well, but if your text is printed by a function or class method you could do the same as in Javascript as well

function hello(){
echo 'HELLO';
}
$print = true;
$print && hello();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest