PHP Short If Statment with OR - php

I have the following PHP script
<?php
echo (($statusSet) == 'all') ? "class='selected'": "";
?>
What I would like to do is include an OR into this to say where $statusSet is equal to all OR NULL
Im competely lost with hwo to write this as I tried adding the normal type of OR statement which didnt work
<?php
echo ((($statusSet) == 'all')||(($statusSet) == 'all'))) ? "class='selected'": "";
?>

All you're doing is adding another expression to be evaluated in the overall if statement.
<?php echo (($statusSet == 'all' || $statusSet === null) ? "class='selected'": ""); ?>
Someone below posted a nearly-identical snippet to mine, but used is_null() instead. Note that using is_null() or === null is fine, but using == null isn't best practices - it won't ensure type equality so if $statusSet was set to (int)0, doing $statusSet == null would return true when it's actually not a null value.

So like this?
echo $statusSet == 'all' || $statusSet === NULL ? "class='selected'": "";?>
I'm not sure that you copied the correct line into the question, but the reason that yours isn't working is because the two conditions are exactly the same, hence if one is true then then so is the other.

<?php echo (($statusSet == 'all') || (is_null($statusSet)) ) ? "class='selected'": "";?>

Related

Logical operation in variable value

I have a variable, which hold the logical operations.
{$logic="1 && || 1";}
i want to perform logical operations in that variable.
if i do the below scenario, it displays wrong result.
{if($logic) { echo "TRUE";} else {echo "FALSE";}}
how can i do the logical operation the value stored in the variable.
You can use eval PHP function:
$logic="1 || 1;";
echo eval($logic) ? 'TRUE' : 'FALSE';

Display HTML if two conditions are true or another two are true or third part of conditions are true

I have php if statement that should display certain HTML code if two conditions are true or another two are true or third part of conditions are true.
I have several arrays - $Options_arr, $MoreOptions_arr, $Special_arr .
To explain in the easiest possible way I want to do this:
if(!empty($Options_arr[0]) && $Options_arr[0]!="") or
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") or
(!empty($Special_arr[0]) && $Special_arr[0]!="")
{?> some HTML here
All help will be appreciated thank you.
empty() already checks for empty string "" so it's shorter:
if(!empty($Options_arr[0]) || !empty($MoreOptions_arr[0]) || !empty($Special_arr[0])) {
//some HTML here
}
BragG, you can use elseif
Like:
if((!empty($Options_arr[0]) && $Options_arr[0]!="") ||
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") ||
(!empty($Special_arr[0]) && $Special_arr[0]!=""))
{
// some html or any code
}
I hope that is what you were looking for..
Feel free to ask any question.
You are just missing some brackets. Also || is more frequently used than OR
if((!empty($Options_arr[0]) && $Options_arr[0]!="") || (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") || (!empty($Special_arr[0]) && $Special_arr[0]!="")){
echo '<p>hello</p>';
}
You're basically already there...
if (
(!empty($Options_arr[0]) && $Options_arr[0]!="")
|| (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="")
|| (!empty($Special_arr[0]) && $Special_arr[0]!="")
){
...do something
Basically you write an if statement that resolves if any of the sub-statements are true by joining the sub-statements together with ORs

PHP AND OR , Is this valid?

Is this valid? I want to check if 'One' is not blank AND two is either blank or undefined. Can i use the brackets that way around both of GET[two]?
if( $_GET["one"] == '' && ($_GET["two"] == '' || $_GET["two"] == 'undefined') ) {
// do X
}
Thanks
Yes, it's certainly valid. Basically, your if checks if BOTH cases (1) one == '' and (2) two == '' or two == 'undefined' exists. (2) evaluates as true if two is either '' or 'undefined'
The brackets are perfectly valid. :)
Yes, that is valid. It should do exactly what you want.
If you need to check for empty strings or NULL values, you can also use the empty() function:
if (empty($_GET['one']))

Two if statements using ternary condition

The title seems confusing but this is my first time using ternary conditions. I've read that ternary is meant to be used to make an inline if/else statement. Using no else is not possible. Is it true?
I want to change this with ternary condition for practice
if (isset($_SESSION['group']
{
if ($_SESSION['item'] == 'A')
{
echo "Right!";
}
}
It has two if statements only. The second if is nested with the other. I've also read that to make a no else possible for ternary, it just have to be set to null or empty string.
It's a bad example because you can use an AND-operator on the nested if:
$result = isset($_SESSION['group'] && $_SESSION['item'] == 'A' ? true : false;
Of course you can nest ternary operator, too:
$result = isset($_SESSION['group'] ? ( $_SESSION['item'] == 'A' ? true : false ) : false;
with echo
echo isset($_SESSION['group'] ? ( $_SESSION['item'] == 'A' ? "Right!" : "false" ) : "false";
echo (isset($_SESSION['group']) && $_SESSION['item'] == 'A') ? "Right" : ""
Better still (readable, maintainable), use:
if (isset($_SESSION['group']) && $_SESSION['item'] == 'A')
{
echo "Right!";
}
isset($_SESSION['group'] ? (if ($_SESSION['item'] == 'A') ? echo "Right" : null) : null
Try this, I think it might work =].
For further reading on ternary conditions in Java/ whatever you're using look at http://www.devdaily.com/java/edu/pj/pj010018
You can nest two ternary statements as this example:
echo (isset($_SESSION['group']))?($_SESSION['item']== 'A')?'Right!':null:null;
Did you know you can do this as well? (isset($_SESSION['group']) && ($_SESSION['item']=='A')) &&($result$c= 1); or echo (isset($_SESSION['group']) && ($_SESSION['item']=='A')) ? 'Hello!':'World!';

PHP: what's an alternative to empty(), where string "0" is not treated as empty? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fixing the PHP empty function
In PHP, empty() is a great shortcut because it allows you to check whether a variable is defined AND not empty at the same time.
What would you use when you don't want "0" (as a string) to be considered empty, but you still want false, null, 0 and "" treated as empty?
That is, I'm just wondering if you have your own shortcut for this:
if (isset($myvariable) && $myvariable != "") ;// do something
if (isset($othervar ) && $othervar != "") ;// do something
if (isset($anothervar) && $anothervar != "") ;// do something
// and so on, and so on
I don't think I can define a helper function for this, since the variable could be undefined (and therefore couldn't be passed as parameter).
This should do what you want:
function notempty($var) {
return ($var==="0"||$var);
}
Edit: I guess tables only work in the preview, not in actual answer submissions. So please refer to the PHP type comparison tables for more info.
notempty("") : false
notempty(null) : false
notempty(undefined): false
notempty(array()) : false
notempty(false) : false
notempty(true) : true
notempty(1) : true
notempty(0) : false
notempty(-1) : true
notempty("1") : true
notempty("0") : true
notempty("php") : true
Basically, notempty() is the same as !empty() for all values except for "0", for which it returns true.
Edit: If you are using error_reporting(E_ALL), you will not be able to pass an undefined variable to custom functions by value. And as mercator points out, you should always use E_ALL to conform to best practices. This link (comment #11) he provides discusses why you shouldn't use any form of error suppression for performance and maintainability/debugging reasons.
See orlandu63's answer for how to have arguments passed to a custom function by reference.
function isempty(&$var) {
return empty($var) || $var === '0';
}
The key is the & operator, which passes the variable by reference, creating it if it doesn't exist.
if(isset($var) && ($var === '0' || !empty($var)))
{
}
if ((isset($var) && $var === "0") || !empty($var))
{
}
This way you will enter the if-construct if the variable is set AND is "0", OR the variable is set AND not = null ("0",null,false)
The answer to this is that it isn't possible to shorten what I already have.
Suppressing notices or warnings is not something I want to have to do, so I will always need to check if empty() or isset() before checking the value, and you can't check if something is empty() or isset() within a function.
function Void($var)
{
if (empty($var) === true)
{
if (($var === 0) || ($var === '0'))
{
return false;
}
return true;
}
return false;
}
If ($var != null)

Categories