PHP "AND or &&" Operator Problem - php

I have:
if($this->itemCount <= 11 ) {
$item['subtotal'] = 12.95 * $item['qty'];
$item['price'] = 12.95;
$this->update_item($item['id'], $item['qty'], $item['price']);
}
But I need an "and" operator in there to check whether or not its also equal to or greater than 2.
if($this->itemCount <= 11 && => 2 )
I don't know how to do this in PHP. :(

if($this->itemCount <= 11 && $this->itemCount >= 2) {
// Your code here
}

if($this->itemCount <= 11 && $this->itemCount => 2 )

if($this->itemCount <= 11 && $this->itemCount >= 2 )

You just need to write the full expression out:
if($this->itemCount <= 11 && $this->itemCount >= 2 )

Related

Complex php if statement

I'm trying to generate all different combinations for something that have x>=y, x<=y
ex: $abc <= 123 && $abc >= 123 && $abb >= 123 && $abb <= 123 && $acc >= 123
however, I don't want things such as "$abc <= 123 && $abc >= 123" to be saved (I'd want the combination: $abc <= 123 && $abb >= 123 && $acc >= 123 to be saved, no conflicting arguments), so I'm trying to use an if statement to filter them out, sometimes there are multiple conflicting arguments such as "$abc <= 123 && $abc >= 123 && $abb >= 123 && $abb <= 123" but I'm having trouble
$string = '$abc <= 123 && $abc >= 123 && $abb >= 123 && $abb <= 123 && $acc >= 123';
if(substr_count($string, "abc <=") == 0 && substr_count($string, "abc >=") == 1 || substr_count($string, "abc <=") == 1 && substr_count($string, "abc >=") == 0) {
echo("hello");
}
In this statement it won't return hello, which is right, but once you remove one conflicting argument from $string it will return hello, but I don't want this because still another conflicting argument exists. Is it possible to do something like this or I must do something like:
substr_count($string, "abc <=") == 0 && substr_count($string, "abc >=") == 1 && substr_count($string, "abb <=") == 1 && substr_count($string, "abb >=") == 0
Please advise
Sorry if this is a poorly asked question, it's hard to explain
This is what bitmasks are for.

IF condition using combination of AND and OR [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'd like to know if my conditions are written with the right way (for the first statement). (in therm of optimization, readability)
if(($nb_post_by_user >= 3 && $nb_post_by_user < 5) || ( $nb_post_by_user >= 3 && ($nb_comm_by_user < 15 || $percent_voted < 25) )){
// Call function A();
}
else if( ($nb_post_by_user >= 5 && $nb_post_by_user < 10) && ($nb_comm_by_user >= 15 && $nb_comm_by_user < 30) && ($percent_voted >= 25 && $percent_voted < 70) ){
// Call function B();
}
Or does the first statement could be written that way? (second statement will be then nested).
if($nb_post_by_user >= 3){
if($nb_comm_by_user >= 15 || $percent_voted >= 25){
// Call function B
}
else{
// Call function A
}
}
if(($nb_post_by_user >= 3 && $nb_post_by_user < 5) || ( $nb_post_by_user >= 3 && ($nb_comm_by_user < 15 || $percent_voted < 25) )){
// Call function A();
} else if( ($nb_post_by_user >= 5 && $nb_post_by_user < 10) && ($nb_comm_by_user >= 15 && $nb_comm_by_user < 30) && ($percent_voted >= 25 && $percent_voted < 70) ){
// Call function B();
}
Edited one :
if($nb_post_by_user >= 3) {
if($nb_post_by_user < 5 || $nb_comm_by_user < 15 || $percent_voted < 25) {
// Call function A();
} else if($nb_post_by_user < 10 && $nb_comm_by_user >= 15 && $nb_comm_by_user < 30 && $percent_voted >= 25 && $percent_voted < 70) {
// Call function B();
}
}
In my opinion both the statements are not doing the same piece of work. The best logical reason i came up with is this :
The first piece of code will get executed if one of the condition written in the if statements are true. But in the second piece of code. The code will always gets executed if the first if condition holds true because of the presence of else statement instead of if else. So both are doing a different work.
hope it helps :)

php comparing 2 co-ords

I don't know why, but this doesn't seem to work.
Basically I want this to be true if the player1 ($playerX, $playerY) is within one square from player2 ($rs[x], $rs[y])
if (($rs[x] > $playerX-2 or $rs[x] < $playerX+2) && ($rs[y] > $playerY-2 or $rs[y] < $playerY+2)) {
// code
Any idea what I'm doing wrong?
If you replace with some test values, you might get:
if( (3 > 5 or 3 < 9) && (13 > 15 or 13 < 19))
Clearly, this is always true.
You should use && instead of or in this case.
It's just a case of replacing or with and. Here's a neater way of writing your if statement for bonus points:
if(abs($playerX - $rs[x]) < 2 && abs($playerY - $rs[y]) < 2) {
// code
Your conditions end up on true due to the OR operator
if (($rs[x] > $playerX-2 && $rs[x] < $playerX+2) && ($rs[y] > $playerY-2 && $rs[y] < $playerY+2)) {
//your code
}

How to find a numeric value within a range in php

How can I find a value let: 745 is within a range of 0 to 1000 using php?
(0 <= $value && $value <= 1000)
How about using <= and >= ?
$x=745;
$inrange=(0<=$x)&&($x<=1000)
Use a condition
<?php
$val = 745;
if ($val >= 0 && $val <= 1000)
{
// Ok
}
else
{
// Not ok
}

PHP Conditionals --> If Else with multiple decisions

if I have something like:
if (($browser->getBrowser() == Browser::BROWSER_SAFARI && $browser->getVersion() >= 3 ) ||
($browser->getBrowser() == Browser::BROWSER_CHROME && $browser->getVersion() >= 5 ))
{
// code here
}
but I really want to say also if Chrome >= 5 but less 6...
I will add an else if for 6+
later on in else () less than version 5 would fall into..
How would I write >= 5 but < 6?
So you can do this directly by adding another condition:
if (($browser->getBrowser() == Browser::BROWSER_SAFARI
&& $browser->getVersion() >= 3 ) ||
($browser->getBrowser() == Browser::BROWSER_CHROME
&& $browser->getVersion() >= 5 && $browser->getVersion() < 6 ))
{
// code here
}
I'm assuming getVersion must be capable of returning non-integer numbers, otherwise you could just check for equality ($browser->getVersion() == 5).

Categories