I cannot find an answer to this and I am sure it is right in front of me. How do I say in PHP as part of an IF statement the following:
if NOT ( (variable1 == 10) && (variable2 == 20) )
Thanks!
You can use ! to achieve this
if ( (variable1 != 10) || (variable2 != 20) )
Or simply
if (!((variable1 == 10) && (variable2 == 20)))
if NOT ( (variable1 == 10) && (variable2 == 20) )
==>
if (! ( (variable1 == 10) && (variable2 == 20) ) )
<==>
if ( (variable1 != 10) ||(variable2 != 20) )
! mean not
so U can use it like this
if (! ( ($variable1 == 10) && ($variable2 == 20) ) )
or
if ( ($variable1 != 10) ||($variable2 != 20) )
&& will be || because !&& = ||
and U can't use variable1 without $ if U don't define it like this
define('variable1','value of variable1');
but then U can't change it's value so if statement will always have the same result and for that U should use $variable1
Related
I'm trying to check if these 3 numbers are odd:
if ( ($pezzi % 1) && ($base % 1) && ($altezza % 1) ) {
}
Why my script don't work? I do not understand why..
if ( ($pezzi % 2 !=0) and ($base % 2 !=0) and ($altezza % 2 !=0) ) {
}
Try this way:
if((number1 % 2) != 0 AND (number2 % 2) != 0 AND (number3 % 2) != 0){
-- do something
}
You should do like this:
if ($pezzi%2!=0 && $base%2!=0 && $altezza%2!=0) {
}
if ( ($pezzi & 1) && ($base & 1) && ($altezza & 1) ) {
}
Your statement is true. You just didn't compare it with 0:
if(($pezzi % 2) != 0 && ($base % 2) != 0 && ($altezza % 2) != 0){
}
Can anyone help
<?php
if(((count( $replies ) > 6) and
(count( $replies ) <= 12)) and
($replyNumber == '3','4','5'))
{ ?>
<execute code......>
<?php } ?>
Why due to use of comma (in replynumber as 3,4,5 is getting an error in code
Change your code like this :
<?php
$varArray = array('3','4','5');
if( ((count( $replies ) > 6) && (count( $replies ) <= 12)) && (in_array($replyNumber,$varArray)) ) { ?>
<execute code......>
<?php } ?>
your code have error in
($replyNumber == '3','4','5'))
and it could be like
($replyNumber == '3' && $replyNumber == '4' && $replyNumber == '5'))
in your code
'and'
should be converted to
'&&' as 'and' is not allowed in php
and complete code be like
<?php
if(((count( $replies ) > 6) &&
(count( $replies ) <= 12)) &&
($replyNumber == '3' && $replyNumber == '4' && $replyNumber == '5'))
{ ?>
<execute code......>
<?php } ?>
You Have an syntax error, of comma's and (You Have To Used or for $replyNumber values )
<?php
if( ((count($replies)>6) or (count($replies)<=12)) and $replyNumber=='3' or $replyNumber=='4' or $replyNumber=='5' )
{
// your execute code here
}
?>
NOTE:
or & || are Or Logical Operators
Ex: $x or $y True if either $x or $y is true
Ex: $x || $y True if either $x or $y is true
and & && are And Logical Operators
Ex: $x and $y True if both $x and $y are true
Ex: $x && $y True if both $x and $y are true
I've this situation in code where i think the code is unnecessary complex and i believe i can refactor it to make it more easier to understand and read.
So i googled about it and found decompose conditional refactoring, but i'm still in doubt how to do refactoring
if(count($bagTypes) == 1 && (array_key_exists('type1', $bagTypes)
|| array_key_exists('type2', $bagTypes)
|| array_key_exists('type3', $bagTypes))){
$flag = 1;
}
if(count($bagTypes) == 2 && (
(array_key_exists('type1', $bagTypes) && array_key_exists('type2', $bagTypes)) ||
(array_key_exists('type1', $bagTypes) && array_key_exists('type3', $bagTypes)) ||
(array_key_exists('type2', $bagTypes) && array_key_exists('type3', $bagTypes)))
){
$flag = 1;
}
Is there any better way of doing this?
You could try something like this:
$arrayKeys = array(
'type1',
'type2',
'type3'
);
$bagTypesKeys = array_keys($bagTypes);
if ((count($bagTypes) == 1 && count(array_diff($arrayKeys, $bagTypesKeys)) < 3)
|| (count($bagTypes) == 2 && count(array_diff($arrayKeys, $bagTypesKeys)) < 2))
{
$flag = 1;
}
I basically have this PHP code:
<?php
$num_1 = $_POST['num_1'];
if( $num_1 == 1 || $num_1 == 2 ){
// Do something
}
?>
What I know is that $num_1 can either be 1 or 2. This is the value I expect from $_POST['num_1']
What I want to know is that instead of using if( $num_1 == 1 || $num_1 == 2 ) could I use or is that the same to use:
<?php
$num_1 = $_POST['num_1'];
if( $num_1 == ( 1 || 2 ) ){
// Do something
}
?>
The difference here is that previously I was using if( $num_1 == 1 || $num_1 == 2 ){} but now I am using if( $num_1 == ( 1 || 2 ) ){}
I am trying to wrap my head around this. Could anyone provide a helpful explanation? Which is more efficient and best practice?
Not the same. You can test it just by doing something like that:
$num_1 = 1;
var_dump($num_1 == ( 1 || 2 )); //true
var_dump($num_1 == 1 || $num_1 == 2); //true
$num_1 = 2;
var_dump($num_1 == ( 1 || 3 )); //true
var_dump($num_1 == 1 || $num_1 == 3); //false
The main reason for this is that when you are trying to do this:
if(1 || 2)
PHP will convert integers to booleans. So, your expresion become:
if(true||true) //what is actually true
I'm creating a custom search form and when I try and sort the results I get all the objects displayed instead of the matched criteria. The reason I discovered was that some of the inputs from the form don't have a default value and when this is not declared in the conditional statement later on (for sorting) it just shows all the objects, whether the other requirements are met or not. I tried applying an OR statement with the specific variables able to be empty, but it gave the same result. Like so -
<?php if ($bedrooms >= $min_rooms
&& $bedrooms <= $max_rooms
&& $space >= $min_space
&& $space <= $max_space
&& $price >= $min_price
&& $price <= $max_price
&& $sel_type == $type
|| $sel_type == ''
&& $country == $sel_country
|| $sel_country == '' ) { ?>
(See the last two statements)
I was thinking of checking each variable in the conditional statement before including it but it feels like unnecessary code. How would you do it?
The && operator has a higher precedence than the || operator, so your expression is currently grouped like this, probably not what you want:
($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && $sel_type == $type)
||
($sel_type == '' && $country == $sel_country)
||
($sel_country == '' )
Try adding parentheses like this to achieve correct grouping:
($bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '') )
Your expression may fail as the && operator has a higher precedence than the || operation. That means an expression like this:
… && $sel_type == $type || $sel_type == ''
is equivalent to this (operator precedence highlighted by using parentheses):
(… && $sel_type == $type) || $sel_type == ''
To fix that put the || expressions in parentheses:
$bedrooms >= $min_rooms && $bedrooms <= $max_rooms && $space >= $min_space && $space <= $max_space && $price >= $min_price && $price <= $max_price && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')
Additionally, your expression is probably easier to read and to maintain if you use some helper functions like a between function:
function between($val, $min, $max) {
return $min <= $val && $val <= $max;
}
Then your expression reads:
between($bedrooms, $min_rooms, $max_rooms) && between($space, $min_space, $max_space) && between($price, $min_price, $max_price) && ($sel_type == $type || $sel_type == '') && ($country == $sel_country || $sel_country == '')