PHP - if-statement not working properly - php

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.

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 user rating based on post and read count

Im trying to get user stars based on their post and read count.
0 post, 0 reads – 0 stars
3 post, 3,000 reads – 1 star
15 post, 15,000 reads – 2 star
25 post, 30,000 reads – 3 star
35 post, 40,000 reads – 4 star
50 post, 70,000 reads – 5 star
code:
$read = 15000;
$news = 13;
if($read < 3000 || $news < 3){
echo '0';
}
else if(($read >= 3000 & $news >= 3)
&& ($read < 15000 & $news < 15)){
echo '1';
}
else if(($read >= 15000 & $news >= 15)
&& ($read < 30000 & $news < 25)){
echo '2';
}
else if(($read >= 30000 & $news >= 25)
&& ($read < 40000 & $news < 35)){
echo '3';
}
else if(($read >= 40000 & $news >= 35)
&& ($read < 70000 & $news < 50)){
echo '4';
}
else if($read >= 70000 & $news >= 50){
echo '5';
}
this does not work well..
check this,
<?php
$read = 50;
$post = 44;
if($post >= 50 && $read >= 70000){
echo "5 stars";
}else if($post >= 35 && $read >= 40000){
echo "4 stars";
}else if($post >= 25 && $read >= 30000){
echo "3 stars";
}else if($post >= 15 && $read >= 15000){
echo "2 stars";
}else if($post >= 3 && $read >= 3000){
echo "1 stars";
}else{
echo "0 stars";
}
?>
Your code "doesn't work" now because it does not match any of your if else statements. If you change your code to this, it should work with the given arguments:
$read = 15000;
$news = 13;
if($read < 3000 || $news < 3){
echo '0';
}
else if(($read >= 3000 & $news >= 3)
&& ($read <= 15000 & $news <= 15)){
echo '1';
}
else if(($read >= 15000 & $news >= 15)
&& ($read <= 30000 & $news <= 25)){
echo '2';
}
else if(($read >= 30000 & $news >= 25)
&& ($read <= 40000 & $news <= 35)){
echo '3';
}
else if(($read >= 40000 & $news >= 35)
&& ($read <= 70000 & $news <= 50)){
echo '4';
}
else if($read >= 70000 & $news >= 50){
echo '5';
}
I reproduced it in an angular fiddle: http://jsfiddle.net/Lvc0u55v/10941/
Your second statements should also check for <=

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

Why does this return 1 number and then stops the loop? PHP

I'm a beginner at PHP, so my code might not be efficient or good.
Why does this code return 1 number and then stops the loop? It's supposed to stop the loop when "the dice" rolled two of every number (1,2,3,4,5,6). But now it stops after randomly generating 1 number..
<?php
$sixCount = 0;
$fiveCount = 0;
$fourCount = 0;
$threeCount = 0;
$twoCount = 0;
$oneCount = 0;
$rollCount = 0;
do{
$roll = rand(1,6);
$rollCount++;
if($roll == 6){
$sixCount++;
echo "6";
} else if($roll == 5){
$fiveCount++;
echo "5";
} else if($roll == 4){
$fourCount++;
echo "4";
} else if($roll == 3){
$threeCount++;
echo "3";
} else if($roll == 2){
$twoCount++;
echo "2";
} else {
$oneCount++;
echo "1";
}
} while($sixCount < 3 && $sixCount > 1 && $fiveCount < 3 && $fiveCount > 1 && $fourCount < 3 && $fourCount > 1 && $threeCount < 3 && $threeCount > 1 && $twoCount < 3 && $twoCount > 1 && $oneCount < 3 && $oneCount > 1);
echo "<br />It took {$rollCount} rolls!";
?>
This is an exercise from Codecademy.com!
Thanks,
Jesper (New at Stackoverflow!)
After first execution of loop, you cannot have $sixCount > 1 && $fiveCount > 1, among other conditions.
After first roll, suppose it's 3, your variables are:
$sixCount = 0;
$fiveCount = 0;
$fourCount = 0;
$threeCount = 1;
$twoCount = 0;
$oneCount = 0;
It doesn't suit while conditions, cuz, for example, $sixCount > 1 is false and other vars too.
The while expression says:
while ($sixCount < 3 && $sixCount > 1 && $fiveCount < 3 && $fiveCount > 1 ...
If $sixCount is less than 3 and more than 1 that implies $sixCount equals 2. Ditto for the others. So it means "keep looping while $sixCount equals 2 and $fiveCount equals 2 and [all the others equal 2]".
You start with those variables at 0:
$sixCount = 0;
$fiveCount = 0;
...
So the loop condition is not initially met. The loop allows at most one of them to be incremented at most once:
$roll = rand(1, 6);
if ($roll == 6) {
$sixCount++;
echo "6";
} else if ($roll == 5) {
$fiveCount++;
echo "5";
} ...
No matter what number is rolled it is impossible to get any of the counts to 2 by the end of a single roll, and certainly not all of them, so the loop condition will not be met, and the loop will inevitably stop.
It's supposed to stop the loop when "the dice" rolled two of every number (1,2,3,4,5,6)
In that case, the correct condition would be:
while ($sixCount < 2 && $fiveCount < 2 && ...
As others have said, your conditional for the while loop will never be true. Instead, you want to make sure the variables aren't all at 2. Try this instead:
while ($sixCount < 2 || $fiveCount < 2 || $fourCount < 2 || $threeCount < 2 || $twoCount < 2 || $oneCount < 2)
I just adapted your script. It can be a funny game.
It rolls 6 dice times 2 (6 x 2) and then if requirements are not met, it rolls the dice again :
$rollCount = 0;
do{
$sixCount = 0;
$fiveCount = 0;
$fourCount = 0;
$threeCount = 0;
$twoCount = 0;
$oneCount = 0;
$rollCount++;
for ($i= 0; $i< 2 * 6; $i++) {
$roll = rand(1,6);
if($roll == 6){
$sixCount++;
echo "6";
} else if($roll == 5){
$fiveCount++;
echo "5";
} else if($roll == 4){
$fourCount++;
echo "4";
} else if($roll == 3){
$threeCount++;
echo "3";
} else if($roll == 2){
$twoCount++;
echo "2";
} else {
$oneCount++;
echo "1";
}
}
echo "_";
} while(!($sixCount < 3 && $sixCount > 1 && $fiveCount < 3 && $fiveCount > 1 && $fourCount < 3 && $fourCount > 1 && $threeCount < 3 && $threeCount > 1 && $twoCount < 3 && $twoCount > 1 && $oneCount < 3 && $oneCount > 1));
echo "<br />It took {$rollCount} rolls!";
$a = array(
$sixCount,
$fiveCount,
$fourCount,
$threeCount,
$twoCount,
$oneCount);
echo '<pre>';
print_r($a);
echo '</pre>';
Giving this king of output :
262225451666_535451252543_666153663214_652652635413_522615315213_412123422526_113553235335_255616351453_124215216465_112544353243_161145351612_522462262355_114331531645_563664155335_455623424146_233336226515_213136514365_646344361534_445325236533_423153546564_324466143565_422464136444_631511342612_516266141216_613556242333_351541131651_554665566244_261433652145_
It took 28 rolls!
Array
(
[0] => 2
[1] => 2
[2] => 2
[3] => 2
[4] => 2
[5] => 2
)

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 ) { ...

Categories