Php Logic statement synatx error due to comma - php

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

Related

Using 'and' and 'or' in an if/else PHP statement

I am attempting to use both AND and OR statements in my IF/ELSE statement, but I cannot get the desired effect.
What I would like to achieve is that if either 'a' or 'b' has a value of '1' but both 'c' and 'd' must be 1 then I get 'Yes'.
All my attempts have given me either 'Yes' or have not worked (blank screen).
<?php
$a = "0";
$b = "1";
$c = "1";
$d = "1";
if (($a == "1") || ($b == "1") && ($c == "1") && ($d == "1")) {
echo "Yes";
}
else {
echo "No";
}
?>
Thank you.
You need and extra parenthesis, to make sure the evaluation order will be done correctly, like in math:
if ( ( ($a == "1") || ($b == "1") ) && ($c == "1") && ($d == "1")) {
^ ^
That way, let's say for example:
$a = 1;
$b = 2;
$c = 1;
$d = 2;
The first parenthesis will be evaluated as true || false. The final result will be true.
So now you have true && ($c == "1") && ($d == "1")
$c = 1, so again, the next evaluation will be true && true && ($d == 1)
$d = 2, so the next round will be true && true && false, final result, in this example, will be false.
You need to add parenthesis.
Why?
Because inner parenthesis are evaluated first before outer parenthesis. Take this example:
((1 == 1 && (2 == 2)) || 3 == 3)
What will be evaluated first? The 2 == 2 then the 1 == 1 and then the 3 == 3. In your if condition, because you are mixing AND's and OR's, you will not get the desired affect.
( (($a == "1") || ($b == "1")) && ($c == "1") && ($d == "1") )
Should work for you. In fact you can do this so that it looks even better:
(($a == 1 || $b == 1) && $c == 1 && $d == 1)
Because it is not necessary to put 1 in quotes ie: "1". PHP's truthiness will evaluate 1 == "1" to be true. However if you wanted to check for an actual string that contains 1, then you would use the === operator.
$a = 1;
$b = "1"
$a == "1"; // true
$b == 1; // true
$a === "1"; // false
$b === "1"; // true
However for more information go here: http://php.net/manual/en/language.operators.precedence.php
The equality operators will be evaluated first, then &&, then ||. Parentheses will be evaluated before anything else, so adding them can change the order.
Check the answer In Java, what are the boolean "order of operations"?
It will always echo a Yes because PHP interpreter places The AND operation before the OR operation.
So your if statement interpretes like this:
If
a = 1 or b = 1 and c = 1 and d = 1
then
echo 'Yes'
else
echo 'No'
That's why you always get a yes..

How to do decompose conditional refactoring?

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;
}

PHP basic comparison with negative

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

PHP "if" statement looking for between two numbers

I need help on a if statement here is what is going on.
I have a value being pulled $paint['product_id']
I need to say if that value is between
81501 - 81599 or
81701 - 81799
say blah
else if that value is between
81001 - 81099 or
81301 - 81399
say blah2
else if
86501 - 86599 or
86001 - 86099 or
85001 - 85099
say blah3
and say nothing if it does not apply.
What id did try
<? if ($paint['product_id'] >= 81501 && $x <= 81599 || $paint['product_id'] >= 81701 && $x <= 81799):?>
blah
<? elseif ($paint['product_id'] >= 81001 && $x <= 81099 || $paint['product_id'] >= 81301 && $x <= 81399):?>
blah2
<? elseif ($paint['product_id'] >= 86501 && $x <= 86599 || $paint['product_id'] >= 86001 && $x <= 86099 || $paint['product_id'] >= 85001 && $x <= 85099):?>
blah3
<? endif;?>
The problem I am having is "blah" is showing up on items in the blah3 category.
Hope that makes sense and thanks in advance for any help!
Replace $x with $paint['product_id'].
You should group them with brackets:
if( ($x > 10 && $x < 20) || ($x > 40 && $x < 50) ) { ...
Consider creating a user defined function, e.g.
function between($x, $lim1, $lim2) {
if ($lim1 < $lim2) {
$lower = $lim1; $upper = $lim2;
}
else {
$lower = $lim2; $upper = $lim1;
}
return (($x >= $lower) && ($x <= $upper));
}
Then the rest of your code becomes much more legible:
if between($paint['product_id'], 81501, 81599) blah;
As given, the "between" function will work even if you don't know ahead of time whether the first or the second argument is larger.
You need more parenthesis
<?php
<? if (($paint['product_id'] >= 81501 && $x <= 81599) || ($paint['product_id'] >= 81701 && $x <= 81799)):?>
blah
<? elseif (($paint['product_id'] >= 81001 && $x <= 81099) || ($paint['product_id'] >= 81301 && $x <= 81399)):?>
blah2
<? elseif (($paint['product_id'] >= 86501 && $x <= 86599) || ($paint['product_id'] >= 86001 && $x <= 86099) || ($paint['product_id'] >= 85001 && $x <= 85099)):?>
blah3
<? endif;?>
there are at least 3 ways to solve it.
First solution has been already posted by users
Second solution is to create between function and use it.
function between($number, $from, $to)
{
return $number>$from && $number<$to;
}
if(between($paint['product_id'], 81501, 81599) || $paint['product_id'], 81701, 81799))
echo 'blah';
else if(between($paint['product_id'], 81001, 81099) || $paint['product_id'], 81301, 81399))
echo 'blah2';
else if(between($paint['product_id'], 86501, 86599) || $paint['product_id'], 86001, 86099) || $paint['product_id'], 85001, 85099))
echo 'blah3';
else echo 'it does not apply';
Third solution is to use range() function and in_array() function
example if(in_array($paint['product_id'], range(81501, 81599)))
rest goes the same
if ($paint['product_id'] >= 81501 && $x <= 81599 || $paint['product_id'] >= 81701 && $x <= 81799){
echo "blah";
}
elseif ($paint['product_id'] >= 81001 && $x <= 81099 || $paint['product_id'] >= 81301 && $x <= 81399){
echo "blah2";
}
elseif ($paint['product_id'] >= 86501 && $x <= 86599 || $paint['product_id'] >= 86001 && $x <= 86099 || $paint['product_id'] >= 85001 && $x <= 85099){
echo "blah2";
}
Hellow i'm going to desribe how to use if condition between two numbers in PHP in easy steps.
<?php
$a= 65;
$b= 9;
if($a<$b)
{
echo "$a this is correct";
}
else{
echo "$b this is greater than $a";
}
?>

PHP: Conditional statement with possible empty variables

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 == '')

Categories