Multiple conditionals in php - 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;
}
}

Related

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

PHP - if-statement not working properly

I am currently in a project to make an auction website. To make sure people can't bid too little above the current bid, I have made this if-statement:
if(
($this->getHighestBid() < 50 && $this->getHighestBid() >= 0 && $bid > $this->getHighestBid()+0.50) ||
($this->getHighestBid() < 500 && $this->getHighestBid() >= 50 && $bid > $this->getHighestBid()+1) ||
($this->getHighestBid() < 1000 && $this->getHighestBid() >= 500 && $bid > $this->getHighestBid()+5) ||
($this->getHighestBid() < 5000 && $this->getHighestBid() >= 1000 && $bid > $this->getHighestBid()+10) ||
($this->getHighestBid() >= 5000 && $bid > $this->getHighestBid()+50)
){
...
}
This should make it so that when the current highest bid is in a specified range, you have to raise the price by at least the price specified.
My problem here is that it won't let me bid on anything at all. Does anyone know what's going on here?
Your code appears to work in my testing, it sounds like the type of the variable is not a numeric type(float or integer) and is actually a string or similarly strange type.
To confirm:
var_dump($this->getHighestBid());
I would like to add while I'm writing my answer though that you will probably want to edit your code to read more like:
function getMinimumBid() {
$maxBid = (float)$this->getHighestBid();
return $maxBid + $this->getBidIncrement();
}
function getBidIncrement() {
$maxBid = (float)$this->getHighestBid();
switch (true) {
case $maxBid < 50:
return 0.50;
case $maxBid < 500:
return 1;
case $maxBid < 1000:
return 5;
case $maxBid < 5000:
return 10;
default:
return 50;
}
}
// ...
if ($bid >= $this->getMinimumBid()) {
}
// ...
Here's your code:
$highest_bid = $this->getHighestBid();
if(
($highest_bid < 50 && $highest_bid >= 1 && $bid > ($highest_bid+0.50)) ||
($highest_bid < 500 && $highest_bid >= 50 && $bid > ($highest_bid+1)) ||
($highest_bid < 1000 && $highest_bid >= 500 && $bid > ($highest_bid+5)) ||
($highest_bid < 5000 && $highest_bid >= 1000 && $bid > ($highest_bid+10)) ||
($highest_bid >= 5000 && $bid > ($highest_bid+50))
){
// Condition 1:
// Highest bid should be >= 1 and < 50
// Bid should be > 1.5 and < 50.5
// Condition 2:
// Highest bid should be >= 50 and < 500
// Bid should be > 51 and < 501
// Condition 3:
// Highest bid should be >= 500 and < 1000
// Bid should be > 505 and < 1005
// Condition 4:
// Highest bid should be >= 1000 and < 5000
// Bid should be > 1010 and < 5010
// Condition 5:
// Highest bid should be >= 5000
// Bid should be > 5050
}
There's a major problem here:
there's some numbers which aren't handled.
you should consider doing this business logic in a function.
I changed your code a little bit, so I could execute it in my IDE:
<?php
$hbid = 50;
$bid = 52;
if(
($hbid < 50 && $hbid >= 0 && $bid > $hbid+0.50) ||
($hbid < 500 && $hbid >= 50 && $bid > $hbid+1) ||
($hbid < 1000 && $hbid >= 500 && $bid > $hbid+5) ||
($hbid < 5000 && $hbid >= 1000 && $bid > $hbid+10) ||
($hbid >= 5000 && $bid > $hbid+50)
)
{
echo "ok";
}
else
{
echo 'not ok';
}
?>
When I change the parameters, it always works fine. I guess your problem is somewhere else.

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

How to use operators so that they exclude zero?

In my database I have "maximum files" but I would like to set it at 0 for unlimited.
Therefore I need a way to
if ($count >= $max){
//a value of 0 in $max should not be here
} else {
//but here
}
Is this possible or do I have to create an exclusion for 0?
if($max && ($count >= $max)) ....
if (($max !== 0) && ($count >= $max) ){
if (!empty($max) && $count >= $max ) { ...

How to write long IF more prettier?

I have long IF:
if(rand(1, 100) == 22 && $smth < time() &&
$smths > 5 && $sxsxsx > 250 &&
!$_SESSION['false'])
{
echo "wow, big if just happened!";
}
How to write it more "prettier"?
I prefer breaking before the boolean operators.
if(rand(1, 100) == 22
&& $smth < time()
&& $smths > 5
&& $sxsxsx > 250
&& !$_SESSION['false']
)
I like to name my conditions and group them so its clear what their purpose is.
$is22 = rand(1, 100) == 22;
$someTime = $smth < time() && $smths > 5;
$meetsSx = $sxsxsx > 250;
$inSession = !$_SESSION['false'];
if ($is22 && $someTime && $meetsSx && $inSession) {
// do something
}
$isSomethingValid = rand(1, 100) == 22
&& $smth < time()
&& $smths > 5
&& $sxsxsx > 250
&& !$_SESSION['false'];
if ($isSometingValid) {
// do something
}
In accordance with my answer to the related
Multiple condition IF statement
this should be refactored with Decompose Conditional, which means you should make the individual tests into separate functions. And you should get rid of the magic numbers and meaningless variable names. I would give you an example on how to do that for your code, but the code is incomprehensible.
Always indent to the enclosing statement one extra than the body of the block. You would write a function like this:
function (reallylongparam, reallylongparam, reallylongparam,
reallylongparam, reallylongparam) {
doStuff()
}
so you'd write your if statement like this:
if(rand(1, 100) == 22 && $smth < time() && $smths > 5
&& $sxsxsx > 250 && !$_SESSION['false']) {
doStuff();
}
probably
if(
rand(1, 100) == 22 &&
$smth < time() &&
$smths > 5 &&
$sxsxsx > 250 &&
!$_SESSION['false']
) {
echo "wow, big if just happened!";
}
cheers
Making your code readable is a very important aspect when it comes to supporting your code - someone else might have to do that support.
Have a look at coding styles (search around for more info if you must).
Personally I would format that snippet like so:
if (
rand(1, 100) == 22
&&
$smth < time()
&&
$smths > 5
&&
$sxsxsx > 250
&&
!$_SESSION['false']
)
{
echo "wow, big if just happened!";
}
Just encapsulate the boolean logic in a seperate function
You could also make your variable names easier to read.

Categories