Optimize if-statement to check values - php

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

Related

Display students grades in alphabetical order

I've been able to display the number of times each grade appears on a student's report sheet. However, they are not sorted alphabetically.
grade image
The image above shows that the student got 2Bs, 4B+, 3As, and 1E. My code displays it like this.
But I want the grades to be displayed in alphabetical order like this 3As, 2Bs, 4B+, and 1E.
How do I do that?
Here is my code
<?php $i = 1;
$total = 0;
$count = count($subjectScores);
$grades_count = [];
foreach ($subjectScores as $value) { ?>
<?php
if ($value->tot_score >= 90 && $value->tot_score <= 100) {
$grade = 'A+';
$remark = 'DISTINCTION';
} elseif ($value->tot_score >= 80 && $value->tot_score <= 89.99) {
$grade = 'A';
$remark = 'EXCELLENT';
} elseif ($value->tot_score >= 70 && $value->tot_score <= 79.99) {
$grade = 'B+';
$remark = 'VERY GOOD';
} elseif ($value->tot_score >= 60 && $value->tot_score <= 69.99) {
$grade = 'B';
$remark = 'GOOD';
} elseif ($value->tot_score >= 50 && $value->tot_score <= 59.99) {
$grade = 'C';
$remark = 'ABOVE AVERAGE';
} elseif ($value->tot_score >= 45 && $value->tot_score <= 49.99) {
$grade = 'D';
$remark = 'AVERAGE';
} elseif ($value->tot_score >= 40 && $value->tot_score <= 44.99) {
$grade = 'E';
$remark = 'FAIR';
} elseif ($value->tot_score >= 0 && $value->tot_score <= 39.99) {
$grade = 'F';
$remark = 'NEEDS SERIOUS IMPROVEMENT';
}
// declare count for grade initially with 0
if(isset($grades_count[$grade]) === false) {
$grades_count[$grade] = 0;
}
{
// increment count for given grade
$grades_count[$grade]++;
}
?>
<?php foreach ($grades_count as $grade=>$count) {
echo "$count$grade ";
} ?>

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 ElseIf Statement with Numbers

The user can only enter a number between 1 and 5 - if they enter 0, leave the field blank or enter a number greater than 5 it will be default reset to 5. 1,2,3,4 are accepted otherwise.
$max=mysql_real_escape_string($_POST["max"]);
if ($max=="0" || $max==""){
$max_r="5";
} elseif ($max > "5"){
$max_r="5";
} else {
$max_r=$max;
}
However it always spits out 5.
Well, you're comparing strings and not integers. Try $max = (int) $_POST['max'] and don't wrap the values in quotes. Then, you can always escape $max before writing it to the DB.
$max = (int) $_POST['max'];
if ( ! $max || $max > 5){
$max_r = 5;
} else {
$max_r = $max;
}
Or, you could go one-liner FTW:
$max_r = ( ! $max || $max > 5) ? 5 : $max;
$max = intval($_POST['max']);
if($max < 0 || $max > 5){
$max = 5;
}

PHP: How can I determine if a variable has a value that is between two distinct constant values?

How can I determine using PHP code that, for example, I have a variable that has a value
between 1 and 10, or
between 20 and 40?
if (($value > 1 && $value < 10) || ($value > 20 && $value < 40))
Do you mean like:
$val1 = rand( 1, 10 ); // gives one integer between 1 and 10
$val2 = rand( 20, 40 ) ; // gives one integer between 20 and 40
or perhaps:
$range = range( 1, 10 ); // gives array( 1, 2, ..., 10 );
$range2 = range( 20, 40 ); // gives array( 20, 21, ..., 40 );
or maybe:
$truth1 = $val >= 1 && $val <= 10; // true if 1 <= x <= 10
$truth2 = $val >= 20 && $val <= 40; // true if 20 <= x <= 40
suppose you wanted:
$in_range = ( $val > 1 && $val < 10 ) || ( $val > 20 && $val < 40 ); // true if 1 < x < 10 OR 20 < x < 40
You can do this:
if(in_array($value, range(1, 10)) || in_array($value, range(20, 40))) {
# enter code here
}
if (($value >= 1 && $value <= 10) || ($value >= 20 && $value <= 40)) {
// A value between 1 to 10, or 20 to 40.
}
Sorry for the late answer, but this function allow you to do that.
function int_between($value, $start, $end) {
return in_array($value, range($start, $end));
}
// Example
$value1 = 20;
$value2 = 40;
echo int_between(20, $value1, $value2) ? "true" : "false";
Guessing from the tag 'operand' you want to check a value?
$myValue = 5;
$minValue = 1;
$maxValue = 10;
if ($myValue >= $minValue && $myValue <= $maxValue) {
//do something
}
If you just want to check the value is in Range, use this:
MIN_VALUE = 1;
MAX_VALUE = 100;
$customValue = min(MAX_VALUE,max(MIN_VALUE,$customValue)));
Try This
if (($val >= 1 && $val <= 10) || ($val >= 20 && $val <= 40))
This will return the value between 1 to 10 & 20 to 40.

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