php comparing 2 co-ords - php

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
}

Related

How do I add empty value to a variable between 5 AM to 10 AM in PHP

what's wrong with this code? I want to add empty value when the timing is between 5 AM to 10 AM.
I tried below code but not working.
if(date('H')=='05' && date('H')<'10')
{
$san="";
}
Maybe you mean >= ? the first condition has == in your code
if(date('H') >= 5 && date('H') < 10)
{
$san="";
}
Can you try this
$currentDateHour = date('G');
if(in_array($currentDateHour, range(5,10)))
{
$san="";
}
This following if statment will true between 05:00:00 and 10:59:59

check if a number is a multiple of another inside for [duplicate]

Within a for loop, i need to add some HTML that outputs only when the loop is on a [(multiple of 3) minus 1].
For example, what i could do is:
for($i=0; $i<count($imagearray); $i++)
{
if($i=="0" || $i=="2" || $i=="5" || $i=="8" || $i=="11")
{
echo 'string';
}
}
but this isnt very elegant and extremely useless for big for loops, is there a proper way to do this?
if ( $i==0 || ($i+1)%3 == 0 )
{
//do stuff
}
What this will do, is go to the next index, divide it by 3, and see if there is a remainder. If there is none, then that means that the current index is one less than a number that is divisible by 3
Use the modulus operator.
if (! (($i+1) % 3) ) {
If $i+1 divides into 3 with no remainder, the result will be zero. Then you just need a boolean not.
If you want to match 0 as well (since you use it in your example, but it doesn't match your description) then you will have to special case it with an ||.
You want to use the modulo for that:
(1 % 3) == 1
(2 % 3) == 2
(3 % 3) == 0
(4 % 3) == 1
Good luck
Modulo is the same thing as saying, give me the remainder of a division. So 1 / 3 equals 0 remainder 1, and so on.
if(($i+1)%3 == 0){
//do something
}
The % operator is known as the modulus operator and returns the remainder of a division.
the most elegent method is thus
if ($i % 3 === 2) {
//do stuff
}
as it doesn't add things to the $i value, but all answers are essentially correct!

Logic in if statement with numbers PHP

Can someone explain what's happening here:
if(2 && 5 < 4)
If i've got for example
$x = 2 && 3;
and var_dump($x) it gives boolean(true) no matter what numbers are.
But here it looks like numbers are comparing to 4 one by one.
Look at the PHP comparison table for PHP http://php.net/manual/en/types.comparisons.php
For integers a number other than 0 returns true in comparison.
if (2 && 5 < 4) => if (true && false) => false
$x = 2 && 3 = 1 && 1 = 1
because if a variable has an integer value, true becomes 1 because of type conversion.

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