PHP "if" statement looking for between two numbers - php

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

Related

Multiple conditionals in php

if
(
($page_num >=10 && $page_num<= 20) ||
($page_num >= 21 && $page_num =< 30) ||
($page_num >=31 && $page_num =<40)
){
if($i==1){
$i = $page_num;
}
}
I am trying to achieve multiple conditionals using the above in PHP. I tried it and it kept outputting an error, so I don't know if I am making a mistake or what I am trying above is not possible in PHP or programming.
The error message
Parse error: syntax error, unexpected '<' in C:addsdcredit.php on line 398
You have =< on your code, change them like this: <= .
check http://php.net/manual/fa/language.operators.precedence.php.
Hope it helps.
$page_num = 11;
$i=1;
if(($page_num >=10 && $page_num<= 20) || ($page_num >= 21 && $page_num <= 30) || ($page_num >=31 && $page_num <=40)){
if($i==1){
$i = $page_num;
}
}

Php Logic statement synatx error due to comma

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

Why do variables inside this for loop return null?

I have the following code and it's giving me hell of a time as to why the variables inside the for ($x = 1; $x <= $quantity; $x++) loop are not returning anything but null when called in the database insert query that's following it, or when I try to debug with var_dump($cVnum); for instance.
Here is the code:
if ($cash >= $product['cost'] * $quantity) {
// Substract cash
$receiver = $userData['login'];
if ($receiver != "") {
//all variables safe
$database->setDB("account")->mkquery("UPDATE {{table}} SET ".$cashfield." = (".$cashfield." - ".$product['cost'] * $quantity.") WHERE id = '".$accountid."' LIMIT 1", "account");
// For each quantity
for ($x = 1; $x <= $quantity; $x++) {
// Insert vnums to item_award
for ($i = 1; $i <= 4; $i++) {
$cVnum = $product['vnum'.$i];
if ($cVnum > 0) {
$socket0 = 0;
$socket2 = 0;
if ($product['vnum'.$i.'_time'] > 0) {
if ($cVnum == 72701 || ($cVnum > 71069 && $cVnum < 71075) || ($cVnum > 72722 && $cVnum < 72731)) {
$socket2 = $product['vnum'.$i.'_time'];
} else if ($cVnum == 47001 ||
($cVnum > 41136 && $cVnum < 41145) ||
($cVnum > 45078 && $cVnum < 45084) ||
($cVnum > 71164 && $cVnum < 71168) ||
($cVnum >= 45139 && $cVnum <= 45144) ||
($cVnum >= 41311 && $cVnum <= 41314)) {
$socket0 = time() + ($product['vnum'.$i.'_time']*60*60*24);
} else {
$socket2 = time() + ($product['vnum'.$i.'_time']*60*60*24);
}
}
}
}
}
$database->setDB("player");
$insert = [
"login" => $receiver,
"vnum" => $cVnum,
"count" => $quantity,
"given_time" => array("func", "NOW()"),
"socket0" => 22,
"mall" => 1
];
$logok = $database->insert($insert, "item_award");
}
}
I've been at it for like six hours now and can't figure out what's wrong.
UPDATE: I narrowed down where the problem is with $cVnum = $product['vnum'.$i];. I noticed that when I removed $i and it became $product['vnum'] the variable returned the value I expected. Could there be a fix so that I can use with the $i variable though? Why is it returning null when $i variable is included?
With String Operators you have to use double quotes if you want PHP parse your variable : $cVnum = $product["vnum".$i];

Optimize if-statement to check values

How can I optimize this if-statement?
if ($min && $max && $value && ($min <= $max) && ($min <= $value) && ($value <= $max)) {
// do anything
}
What it should do:
I got three values (min, max and value). First of all, all values should be != 0. Second, min <= value <= max.
Valid:
min = 1; max = 3; value = 2;
min = 2; max = 2; value = 2;
this:
if ( 0 < $min && $min <= $value && $value <= $max ){
echo 'good';
}
The answer is:
if(isset($min , $max , $value ) && ($min <= $value) && ($value <= $max)){
//Insert your code here
}
I think this prevents any value from being a 0 and makes sure value is inside the min and max range.
if ( ( $min > 0 && $max >= $min ) && ( $value >= $min && $value <= $max ) ) {
echo "Good";
} else {
echo "Bad";
}

Part of pascal function

I'm trying to rewrite a pascal program to PHP, and don't understand what this part of pascal function do:
while (u[3] <> 1) and (u[3]<>0) and (v[3]<>0)do
begin
q:=u[3] div v[3];
for i:=1 to 3 do
begin
t:=u[i]-v[i]*q;
u[i]:=v[i];
v[i]:=t;
{writeln('u',i,'=',u[i],' v',i,'=',v[i]); }
end;
end;
if u[1]<0 then u[1]:=n+u[1];
rae:=u[1];
Please help to rewrite it to PHP.
Thanks.
A very literal translation of that code, should be this one:
while ($u[3] != 1 && $u[3] != 0 && $v[3] != 1 )
{
$q = floor($u[3] / $v[3]);
for ($i = 1; $i <= 3; $i++)
{
$t = $u[$i] - $v[$i] * $q;
$u[$i] = $v[$i];
$v[$i] = $t;
//writeln('u',i,'=',u[i],' v',i,'=',v[i]);
}
}
if ($u[1] < 0 )
$u1] = $n + $u[1];
$rae = $u[1];
Of course, u and v are arrays. Sorry for not giving any more info, but it's been like 10 years since Pascal and I last saw each other, but we had a profound romance for a long time, since I feel inlove for to hotties(C# and PHP) :)
while ($u[3] != 1) && ($u[3] != 0) && ($v[3] != 0) {
$q = floor($u[3] / $v[3]);
for ($i = 1; $i <= 3; $i++) {
$t = $u[$i] - $v[$i] * $q;
$u[$i] = $v[$i];
$v[$i] = $t;
echo "u$i={$u[$i]} v$i={$v[$i]}\n";
}
}
if ($u[1] < 0) {
$u[1] = $n + $u[1];
}
$rae = $u[1];
2 small corrections to David's code:
while ($u[3] != 1 && $u[3] != 0 && $v[3] != 1 )
should be
while ($u[3] != 1 && $u[3] != 0 && $v[3] != 0 )
and
for ($i = 1; $i < 3; $i++)
i never reaches the value of 3
for ($i = 1; $i <= 3; $i++)
May be the Writeln can be translated to
echo 'u'.$i.'='.$u[$i].' v'.$i.'='.$v[$i];
When you do the translation of arrays, take into account that arrays in php uses 0 as the first index.
$u= array( 3, 5, 22 )
echo u[1]; // prints 5
while($u[3] != 1 && $u[3] != 0 && $v[3] != 0)
{
$q = ($u[3] - ($u[3] % $v[3]) ) / $v[3]; //just the same as floor($u[3]/$v[3]), but i want to use % here :)
for ($i = 1; $i <= 3; $i++)
{
$t = $u[$i] - $v[$i]*$q;
$u[$i] = $v[$i];
$v[$i] = $t;
echo '<br />u'.$i.'='.$u[$i].' v'.$i.'='.$v[$i];
}
}
if ($u[1] < 0) $u[1] = $n + $u[1];
$rae = $u[1];
I dont know pascal But i have tried :)
while ($u[3]!=1 && $u[3]!=0 && $v[3]!=0) [
$q=floor($u[3]/ $v[3]);
for ($i=1;$i<3;$i++) {
$t=$u[$i]-$v[$i]*$q;
$u[$i]=$v[$i];
$v[$i]=$t;
echo "u".$i."=".$u[$i]."v".$i."=".$v[$i];
}
if ($u[1]<0) {
$u[1]=$n+$u[1];
}
$rae=$u[1];
In php variable Name Start With $
No Begin End used here in php only braces :)

Categories