PHP If Statement with Multiple Conditions
if($var == "abc" || $var == "def" || ...)
{
echo "true";
}
Using "Or" instead of "And" would help here, i think
PHP if-Statement with multiple conditions
&&
has higher precedence than ||
, so your condition is interpreted as
if ($properties->offering_type === 'y' ||
($properties->offering_type === 'p' && $properties->sold != 'y')) {
You need to add parentheses to group the ||
together.
if (($properties->offering_type === 'y' || $properties->offering_type === 'p')
&& $properties->sold != 'y') {
PHP If Statement with Multiple Conditions in one
You need to use a regex, use multiple conditions, or put the values in an array. I'd use http://php.net/manual/en/function.in-array.php.
if (in_array($image_ex , array('gif', 'jpg', 'png', 'jpeg'))) {
A regex approach could be:
if(preg_match('/^(?:gif|png|jpe?g)$/', $image_ex)){
or the long multiple conditions approach:
if($image_ex == "gif" || $image_ex == "jpg" || etc..){
If statement with && and multiple OR conditions in PHP
&& takes precedence over ||, thus you would have to use parentheses to get the expected result:
if (isset($_POST['cookieGiftBox']) && (!isset($POST['sugar']) || ...)
Actually, to get check if nothing is selected, you would do something like this:if checkbox is checked and not (sugar is checked or chocolatechip is checked)
or the equivalent:if checkbox is checked and sugar is not entered and chocolatechip is not entered...
.
If you want to know more, search for information about Boolean algebra.
Update: In your example and in the correct syntax the sentence I took as an example would like this (for the first sentence, note the not (!) and the parentheses around the fields, not the checkbox):
if (isset($_POST['cookieGiftBox']) &&
!(
isset($_POST['sugar']) ||
isset($_POST['chocolatechip'])
)
) { error ...}
Or the second sentence, which might be easier to understand (note the && instead of ||):
if (isset($_POST['cookieGiftBox']) &&
!isset($_POST['sugar']) &&
!isset($_POST['chocolatechip'])
) { error...}
Using ands makes sure it is only true (and thus showing the error) if none of the sugar, chocolatechips, etc. fields are set when the giftbox checkbox is checked.
So if the checkbox is checked, and no fields are set it looks like this:(true && !false && !false)
which is equivalent to (true && true && true)
which is true
, so it shows the error.
If the checkbox is checked and sugar is entered it would look like this:(true && !true && !false)
, equ. to (true && false && true)
, equ. to false
and thus no error is shown.
If both sugar and chocolatechip are entered also no error will be shown.
how to write single line if statement with multiple conditions?
You can achieve this by using a ternary operator. Look at the following code:
$prominentSideNumberArray[] = ((($colorLevel['name'] === 'ATTR_VPMCV13') &&
($colorLevel['level'] >= 80) )? 10 : (($colorLevel['name'] == 'ATTR_VPMCV13') &&
($colorLevel['level'] >= 60) && ($colorLevel['level'] <= 70)?8:"")) ;
EDIT As per comments and you have to compare same value its better to define name
$color_name = "ATTR_VPMCV13";
if($colorLevel['name'] == $color_name )
$prominentSideNumberArray[] = (($colorLevel['level'] >= 80)? 10 : (
($colorLevel['level'] >= 60) && ($colorLevel['level'] <= 70)?8:"")) ;
DEMO with different approach
EDIT
Keep in mind that this solution is less readable than if-else
statement.
PHP multiple OR in if condition
In a condition like if (p || q || r)
, the whole if
statement evaluates to true
if at least one of the three conditions is true
. If you don't have MANAGEMENT role, then !in_array('MANAGEMENT',$_SESSION['roles'])
will be true
, hence access will be denied.
I would recommend you to invert the if
statement, so that if true, the access is granted, otherwise it's denied. So:
if (in_array('ADMIN', $_SESSION['roles']) || in_array('MANAGEMENT', $_SESSION['roles']) || $requester == $_SESSION['tnumber'] ) {
// allowed
} else {
// denied
}
It will also help the readability of your code if you extract the big condition to a separate function.
PHP If Statement with Multiple Conditions and Results
You can do this with either a switch
statement or an elseif
:
if ($row['rank'] == 1 |){
echo 'Administrator';
}
elseif ($row['rank'] == 2){
echo 'Moderador';
}
elseif ($row['rank'] == 3) {
echo 'Helper';
}else{
echo "Not Ranked";
}
OR
switch ($row['rank']) {
case 1:
echo 'Administrator';
break;
case 2:
echo 'Moderator';
break;
case 3:
echo 'Helper';
break;
default:
echo "Not Ranked";
}
PHP IF Statement - Sectioning off multiple conditions
You can test this easy but you had an extra paren
<?php
function testNest($a, $b, $c, $d){
if ($a < $b || ( $a <= $b && $c = $d )) {
echo 'foo';
} else {
echo 'bar';
}
}
testNest(3, 2, 3, 2); //bar
testNest(1, 2, 3, 2); //foo
function testElif($a, $b, $c, $d){
if ($a < $b ){
echo 'foo';
} elseif ( $a <= $b && $c = $d ) {
echo 'foo';
} else {
echo 'bar';
}
}
testElif(3, 2, 3, 2); //bar
testElif(1, 2, 3, 2); //foo
I'll let you come up with more examples but there really is no need for elseif
Related Topics
Deny Direct Access to a Folder and File by Htaccess
PHP Sort Array by Subarray Value
How to Return Outer HTML of Domdocument
Turning Multidimensional Array into One-Dimensional Array
PHP Datetime::Modify Adding and Subtracting Months
Getting Checkbox Values on Submit
Getting Title and Meta Tags from External Website
How to Put Composite Keys in Models in Laravel 5
PHP: Custom Error Handler - Handling Parse & Fatal Errors
What's the Use of Ob_Start() in PHP
A Numeric String as Array Key in PHP
Pdo MySQL: How to Know If Insert Was Successful
I Need My PHP Page to Show My Blob Image from MySQL Database