one line if statement in php

php if statement multiple conditions
php ternary else if
php if else
php shorthand if without else
php ternary operator echo
php if else html
php variable if condition
if else condition in mysql query in php

I'd like to to some thing similar to javascripts

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

but this doesnt 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 do 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 with out braces.

use the ternary operator ?:

change this

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


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

In short

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

if - Manual, The if construct is one of the most important features of many languages, PHP included. If expression evaluates to TRUE , PHP will execute statement, and if it  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.

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

PHP Shorthand If/Else Using Ternary Operators (?:), If/Else statements aren't optimal (or necessary) in all situations. PHP's behaviour when using more than one ternary operator within a single As a rule I use ternary only if the If/Else is a 'single line of code' <— Yes this is  PHP features an if structure that is similar to that of C: if (expr) statement. As described in the section about expressions, expression is evaluated to its Boolean value. If expression evaluates to TRUE , PHP will execute statement, and if it evaluates to FALSE - it'll ignore it.

Something like this?

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

PHP One Line If Statement – Single Line If , PHP One line if statement reminder - a quick example to remind you how to construct the one liner if. Not sure why so many people find this difficult to remember,  PHP's behaviour when using more than one ternary operator within a single statement is non-obvious." If you aren't experienced with using ternary operators, write your code using if/else first, then translate the code into ?'s and :'s.

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();

IF-ELSEIF-ELSE statement in one line? - PHP, Hi, I know that we can write IF-ELSE statement in one line, such as, $eatit = ($​applet !="gree") ? "Eat" : "Dont Eat"; but how about IF-ELSEIF-ELSE statement?? You need the semi-colon at the end of the print statement. But if your if statement only runs to one line, you can just do this: if ($kitten_image == 1) { print ("<IMG SRC = images/kitten.jpg>"); } In other words, keep everything on one line. PHP doesn't care about your spaces, so it's perfectly acceptable code.

Shorthand comparisons in PHP, You probably already know some comparison operators in PHP. Things like The ternary operator is a shorthand for the if {} else {} structure. You could write this expression the same way using the normal ternary operator: The if statement execute a single statement or a group of statements if a certain condition is met. It can not do anything if the condition is false. For this purpose else is used.

How to use the PHP ternary operator · A Beautiful Site, If you're not sure what the ternary operator is or how it works, you're missing out on a really cool Let's say we want to assign one of two values to $x based on a certain condition. Let's do it in one line with ternary logic:. In PHP we have the following conditional statements: if statement - executes some code if one condition is true. ifelse statement - executes some code if a condition is true and another code if that condition is false. ifelseifelse statement - executes different codes for more than two conditions. switch statement - selects one of many blocks of code to be executed.

Mastering the use of PHP conditionals, The above nested if statements are equivalent to our single if statement from earlier. How does PHP evaluate a logical condition like the one above? If those “Do stuff” comments were 20-30 lines of code, it would be a lot harder to read or  Personally, I think the readability of nested ternary statements suck. It's not that I can't understand them, but rather I can clearly read an if/elseif/else statement pretty much instantly, but

  • The short version is called the ternary operator
  • I did think of using a ternary if, but dismissed it as i didn't have an else condition. using '' as the else is a good idea.