Determine a value with switch and range - php

I wrote the following piece of code to perform a custom rating of some movies in accordance to their IMDb rating (I'm scraping the rating field off of IMDb using a class I found on github).
Anyway, this worked fine for 643 movies in my DB, but for 12 specific movies it fails to recognize the correct range the IMDb rating corresponds to... It's not that those 12 have any special number instead of a rating... I wrote down the values of those 12 movies, and they are normal floats (OK, except for one that is a very old, foreign movie and it returns n/A - but even that when floatval()'ed turns to 0)... For statistical reasons, the values that can't be recognized along with the times they appear are these:
1x time 0 [it's the unrated one]
2x times a rating of 3.4
2x times a rating of 3.8
4x times a rating of 3.9
1x time a rating of 4.1
2x times a rating of 4.6
The code that performs this custom rating is this (very simple and straight-forward):
$rating = floatval($imdb->getRating());
switch (true) {
case in_array($rating, range(1, 4.9, 0.1)):
$return['movie_rating'] = 1;
break;
case in_array($rating, range(5, 5.9, 0.1)):
$return['movie_rating'] = 2;
break;
case in_array($rating, range(6, 6.9, 0.1)):
$return['movie_rating'] = 3;
break;
case in_array($rating, range(7, 7.9, 0.1)):
$return['movie_rating'] = 4;
break;
case in_array($rating, range(8, 10, 0.1)):
$return['movie_rating'] = 5;
break;
default:
$return['movie_rating'] = 'n/A';
}
Guys, honestly, this is driving me crazy. Any suggestion will be very much appreciated! TIA.

problem happens because of floating point math is not precise
to avoid it I suggest to rewrite this code this way:
if ($rating >= 8) $return['movie_rating'] = 5;
elseif ($rating >= 7) $return['movie_rating'] = 4;
elseif ($rating >= 6) $return['movie_rating'] = 3;
elseif ($rating >= 5) $return['movie_rating'] = 2;
elseif ($rating >= 1) $return['movie_rating'] = 1;
else $return['movie_rating'] = 'n/A';

Related

PHP Calculate fee by price range

Is anyone who can help me to create PHP and mysql Code. Here is the condition.
If price range is 1 to 20 USD, it will be show 2 USD.
If price range is 21 to 50 USD, it will be show 5 USD.
If price range is 51 to 100 USD, it will be show 7 USD.
how to do it with PHP or WordPress php coding.
I don't get the MySQL part here did you want to do this on the database level? or are you looking for both? anyway, in PHP it should be like this
$price = 5;
if($price >= 1 && $price <= 20){
$price = 2;
}else if($price > 20 && $price <= 50){
$price = 5;
}else if($price > 50 && $price <= 100){
$price = 7;
}
Note: you should be careful with the ranges 20.1 is greater than 20 but it is less than 21
I hope it's helpful
If you're using PHP 8, this would be elegantly done with match:
$fee = match(true) {
$price <= 20 => 2,
$price <= 50 => 5,
$price <= 100 => 7,
default => 0 // free shipping!
};
Since the evaluation ends when a "match arm" matches, we don't need to define the lower range -- as long as our ranges are defined in ascending order. I haven't used $price >= 1 && $price <= 20 => 2 for the first condition, assuming that $0.50 still costs something.
Since we're evaluating for the truth of expressions (true|false), a simple match($fee) wouldn't work. We need match(true) instead, where, when an expression evaluates as true, the corresponding value will be returned. Note that match must be exhaustive, ie. it must have a default condition if none of the conditions match (or you get an error).
On older PHP versions, the more long-winded switch can be used:
switch(true) {
case ($price >= 1 && $price <= 20):
$fee = 2;
break;
case $price <= 50:
$fee = 5;
break;
case $price <= 100:
$fee = 7;
break;
default:
$fee = 0;
break;
};
While switch doesn't require a default statement, it's a good idea to have a default value for transactions that exceed your ranges. Being nice, I give free shipping for orders > 100.
You can also of course use a series of if/elseif statements, but they become quite clunky if you have more than a small handful of conditions to check. match and switch exist to make cases like this more readable and easier to maintain.
Per #AhmedHassan's notes, your logic has a gap between 20/21, and 50/51. The next step from "less than or equal to 20" has to be "more than 20, less than or equal with 50". Or, if $20 flat already incurs the higher fee, then you should use >= 20 and < 50 for your price range boundaries.

A way to summarize many commands like if... and if... do

I've created a question and answer page to determine the level of interest of each user for different products
like this:
how much u like x (betwin 1-10)
These questions are more than 30, and if I want to write a single command line for each possibilities, it's almost impossible.
the commands do like this:
if $a <=5 and $b <=6 and $c <=7 and... do ...
if $a<= 8 and $b <=7 and $c >= 5 and $d <=8 do...
I want the commands to work this way
Is there a better way to do this?
thanks
For this you could use a switch statement. Documentation: http://php.net/manual/en/control-structures.switch.php
Example code:
$i = 10;
switch($i):
case ($i <= 0): // or for example: case (0)
return 'It\'s 0 or lower than 0';
break;
case ($i > 0 && $i < 10):
return 'Number is between 0 and 10';
break;
case ($i >= 10):
return 'Number is 10 or higher';
break;
default:
return false;
endswitch;
// You can use echo instead of return, but i prefer to use these statements in a function instead of the html page.
More information about the differences between if and switch is provided by Masivuye Cokile as a comment in your question: Which is Faster and better, Switch Case or if else if?
I hope this helped. Let me know.

Using Switch Statements to determine div background-color

I am building a percentage bar for my website based on pre-defined numbers (not server load percentage). When I use my switch statements to determine the color (based on "percentage range") it has the correct percentage, but the wrong color.
My php
$max = 250000;
$heat = 200000; //$_SESSION['warrents'];
$percent[0] = ($heat / $max) * 100;
if ($percent[0] > 100) { $percent[0] = 100; }
switch ($percent[1]) {
case ($percent[0] < 25):
$percent[1] = "green";
break;
case ($percent[0] >= 25 && $percent[0] <50):
$percent[1] = "yellow";
break;
case ($percent[0] >= 50 && $percent[0] <75):
$percent[1] = "orange";
break;
case ($percent[0] >= 75):
$percent[1] = "red";
break;
}
echo $percent[0]." ".$percent[1];
The echo says 80 green
80 is correct; however, it should be red, not green. Any ideas what I'm doing wrong here?
Thanks!
Of course as soon as I posted this I figured out my problem:
switch ($percent[1]) {
should be
switch ($percent[0]) {
Thanks for those who looked!
I needed a percentage bar on the site I am working on at the moment; It is currently on my development domain, so you can check it out and see yourself (Yours can look however you want).
The following code will take a percentage value and return a color value from red-amber-green:
function getColorFromPercent(percent) {
colors = [
'FF0000',
'FF1000',
'FF2000',
'FF3000',
'FF4000',
'FF5000',
'FF6000',
'FF7000',
'FF8000',
'FF9000',
'FFA000',
'FFB000',
'FFC000',
'FFD000',
'FFE000',
'FFF000',
'FFFF00',
'F0FF00',
'E0FF00',
'D0FF00',
'C0FF00',
'B0FF00',
'A0FF00',
'90FF00',
'80FF00',
'70FF00',
'60FF00',
'50FF00',
'40FF00',
'30FF00',
'20FF00',
'10FF00'
];
return '#' + colors.reverse()[Math.floor(parseFloat(percent) / (100 / colors.length))];
}
document.getElementById('test').style.backgroundColor = getColorFromPercent('23.8%');
<div id="test">23.8%</div>
Good Luck, and If you have any issues - Let me know!

Evaluate a value in a switch block using comparisons in each case

Suppose I have a variable integer and wish to do different things if the value is greater then one value and less then another value. My objective for this switch, is basically to send different results based on the value of $chance, which I will have many cases in the end as this is for a game.
switch ($chance)
{
case ($chance < 15):
echo"strike<br>";
case ($chance < 15 && $chance > 50):
echo"Ball<br>";
break;
case ($chance < 50 && $chance > 100):
echo"Single<br>";
break;
case ($chance <= 150 && $chance >= 100):
echo"double<br>";
break;
case ($chance <= 175 && $chance >= 151):
echo"triple<br>";
break;
case ($chance > 200 && $chance > 175):
echo"Ground Rule Double<br>";
break;
case ($chance < 200):
echo"Home Run<br>";
break;
}
Now, I've been told that I can use conditionals in switch statements, and I've also been told that I should not use them. I really don't know who to believe.
What I do know, is that currently, this switch statement does not work as intended. It doesn't generate syntax errors, but I will get random echos back. This happens when sometimes the chance may be 100 and I will get a home run echo. I'm not sure.
I know I could do the same with a series of if but it would amount to a huge difference in length of code if I can achieve the same results.
I imagine I can do something like
case 1:
echo this
case 2:
echo that
etc etc
Until I hit 2 or 300 but I would like to avoid that if possible.
This is not how you use the switch statement. This is an example of a correct way:
switch ($a) {
case 1:
echo 1;
break;
case 2:
echo 2;
break;
default:
echo 0;
}
For what you want to accomplish you need to use the old if-else statements.
if ($chance < 15)
echo"strike<br>";
else if ($chance >= 15 && $chance < 50)
echo"Ball<br>";
else if ($chance >= 50 && $chance < 100)
echo"Single<br>";
else if ($chance <= 150 && $chance >= 100)
echo"double<br>";
else if ($chance <= 175 && $chance >= 151)
echo"triple<br>";
else if ($chance < 200 && $chance > 175)
echo"Ground Rule Double<br>";
else if ($chance <= 200)
echo"Home Run<br>";
Switch statement syntax:
http://php.net/manual/en/control-structures.switch.php
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
To do what you are wanting to do, you should use an if-else. The value used for the case expression must be an integer, floating-point decimal, or string.
You need just set true in you switch. Use switch (true) instead of switch ($chance).
now your code will be:
switch (true)
{
case ($chance < 15):
echo"strike<br>";
case ($chance < 15 && $chance > 50):
echo"Ball<br>";
break;
case ($chance < 50 && $chance > 100):
echo"Single<br>";
break;
case ($chance <= 150 && $chance >= 100):
echo"double<br>";
break;
case ($chance <= 175 && $chance >= 151):
echo"triple<br>";
break;
case ($chance > 200 && $chance > 175):
echo"Ground Rule Double<br>";
break;
case ($chance < 200):
echo"Home Run<br>";
break;
}
Despite some mathematical/comparison issues in your code, it seems that you intend to set numeric ranges that correspond to specific outcomes. It also seems inappropriate to reach multiple outcomes in a single script execution.
Given these truths, you should use an if-elseif-else block. It will be more appropriate and less verbose than a switch block.
The ONLY advantage to using a switch block is when you need to make 1 evaluation and take different actions based on that evaluation. Because you are making multiple evaluations, there is no advantage in using a switch -- in fact, the computational complexity will be the same and the syntax will either be of equal or greater length versus an if block. I personally have such a distaste for switch blocks that I endeavor to use any other viable technique instead. Switch blocks only make very rare appearances in my professional projects.
By progressing in an ASC or DESC fashion, you will only need one expression in each condition. Using consistent operators in each condition will make your script easier to maintain/extend.
Finally, in PHP, elseif is one word not two.
Code:
if ($chance < 0) {
$result = 'Balk'; // ;)
} elseif ($chance < 15) {
$result = 'Strike';
} elseif ($chance < 50) {
$result = 'Ball';
} elseif ($chance < 100) {
$result = 'Single';
} elseif ($chance < 150) {
$result = 'Double';
} elseif ($chance < 175) {
$result = 'Triple';
} elseif ($chance < 200) {
$result = 'Ground Rule Double';
} else {
$result = 'Home Run';
}
echo "$result<br>";

Redefine a function after it is created in Javascript

I'm creating through a PHP loop (the reason of the loop is because I want more or less cases) a switch Javascript function, that outputs this:
function my_function(array_name) {
switch (true) {
case (array_name[1] < x < array_name[2]):
console.log("between: 1 & 2");
break;
case (array_name[2] < x < array_name[3]):
console.log("between: 2 & 3");
break;
default:
console.log("none");
break;
}
}
Now when the document is loaded I create a Javascript array which contains the values I want to pass to the function.
var array_to_execute = [0,1000,2000,3000];
How can I call the function after this array is created passing these values? Something like:
my_function(array_to_execute);
That would result in executing:
switch (true) {
case (1000 < x < 2000):
console.log("between: 1 & 2");
break;
case (2000 < x < 3000):
console.log("between: 2 & 3");
break;
default:
console.log("none");
break;
}
The function call that you've got in mind (my_function(array_to_execute)) should be just fine!
The problems you're probably having are because:
case (array_name[1] < x < array_name[2]):
does not work the way you think it does. (It ends up comparing a truth value to a number.) Use this instead and you should be good to go:
case (array_name[1] < x && x < array_name[2]):

Categories