php float comparison not working - php

I want to change the students marks to GP so I have code like this
$mark=80;
if($mark>=84.50 && $mark<=100){$GP = 4;}
if($mark>=79.50 && $mark<=84.49){$GP = 3.67;}
if($mark>=74.50 && $mark<=79.49){$GP = 3.33;}
if($mark>=69.50 && $mark<=74.49){$GP = 3;}
if($mark>=64.50 && $mark<=69.49){$GP = 2.50;}
if($mark>=59.50 && $mark<=64.49){$GP = 2;}
if($mark>=54.50 && $mark<=59.49){$GP = 1.50;}
if($mark>=49.50 && $mark<=54.49){$GP = 1;}
else { $GP = 0; }
echo $GP;
Expected output here is 3.67 but here I am getting 0. Can anyone explain why I am getting 0?

Each if is evaluated independently. Regardless of what happens with the first couple of ifs, you reach the last one, and since $mark isn't between 49.50 and 54.49, the code enters the else block, and sets $GP to 0. If you replace all the ifs except the first one with else ifs you'll get the intended logic:
$mark=80;
if($mark>=84.50 && $mark<=100){$GP = 4;}
else if($mark>=79.50 && $mark<=84.49){$GP = 3.67;}
else if($mark>=74.50 && $mark<=79.49){$GP = 3.33;}
else if($mark>=69.50 && $mark<=74.49){$GP = 3;}
else if($mark>=64.50 && $mark<=69.49){$GP = 2.50;}
else if($mark>=59.50 && $mark<=64.49){$GP = 2;}
else if($mark>=54.50 && $mark<=59.49){$GP = 1.50;}
else if($mark>=49.50 && $mark<=54.49){$GP = 1;}
else { $GP = 0; }
echo $GP;

Your code fails because your else only applies to the last condition. That means all the previous if statements have come to nothing.
You should use elseif to make sure that the conditions are truly distinct:
$mark=80;
if($mark>=84.50 $GP = 4;
elseif($mark>=79.50) $GP = 3.67;
elseif($mark>=74.50) $GP = 3.33;
elseif($mark>=69.50) $GP = 3;
elseif($mark>=64.50) $GP = 2.50;
elseif($mark>=59.50) $GP = 2;
elseif($mark>=54.50) $GP = 1.50;
elseif($mark>=49.50) $GP = 1;
else $GP = 0;
echo $GP;
As you see, you can also dispense with the && additional condition. Finally, you don’t need braces ({}) around a simple statement.

<?php
$mark=80;
if($mark>=84.50 && $mark<=100){$GP = 4;}
elseif(abs($mark>=79.50 && $mark<=84.49)){$GP = 3.67;}
elseif($mark>=74.50 && $mark<=79.49){$GP = 3.33;}
elseif($mark>=69.50 && $mark<=74.49){$GP = 3;}
elseif($mark>=64.50 && $mark<=69.49){$GP = 2.50;}
elseif($mark>=59.50 && $mark<=64.49){$GP = 2;}
elseif($mark>=54.50 && $mark<=59.49){$GP = 1.50;}
elseif($mark>=49.50 && $mark<=54.49){$GP = 1;}
else { $GP = 0; }
echo $GP;
?>
Your comparision not in proper way check above code.

Related

Can If Statment created based on loop?

For example
$InitArray[1] = 4;
$InitArray[2] = 5;
$InitArray[3] = 6;
I want to make if statment with that syntax
if($Type = /* key */){
$Position = /* value */
}
which means
if($Type = 1){
$Position = 4;
}
if($Type = 2){
$Position = 5;
}
if($Type = 3){
$Position = 6;
}
How to make that if statment through array loop?
Edit :
Array isn't static , User put his own array
,It Could be like that for example
$InitArray[4] = 34;
$InitArray[5] = 13;
or
$InitArray[6] = 12;
$InitArray[13] = 84;
$InitArray[52] = 23;
$InitArray[78] = 10;
what I mean is something like this
foreach($InitArray as $TypeWritten => $PositionWritten){
if($Type = $TypeWritten){
$Position = $PositionWritten;
}
}
The Equal comparison operator in php is double equal sign ==. So you need to at least change all if statement to use == instead of =.
foreach($InitArray as $TypeWritten => $PositionWritten){
if($Type == $TypeWritten){
$Position = $PositionWritten;
// some code to use the $Position variable ...
// ...
}
}
You're just missing the correct comparison operator. Just change the = into == (or === if the variables you are comparing are of the same type),
foreach($InitArray as $TypeWritten => $PositionWritten){
if($Type == $TypeWritten){
$Position = $PositionWritten;
}
}
Hope this helps,

How to do addition between two dropdown menus containing 1-5?

This is the code I have so far. I'm trying to use two dropdown menus in my HTML form that contains a 1-5 each and simply add them using POST.
//action for dropdown menu addition
$dropdownValueA = $_POST["dropdown1"];
$dropdownValueB = $_POST["dropdown2"];
$valueone = 0;
$valuetwo = 0;
if ($dropdownValueA == "1a"){
$valueone = 1;
}
if ($dropdownValueA == "2a"){
$valueone = 2;
}
if ($dropdownValueA == "3a"){
$valueone = 3;
}
if ($dropdownValueA == "4a"){
$valueone = 4;
}
if ($dropdownValueA == "5a"){
$valueone = 5;
}
if ($dropdownValueB == "1b"){
$valuetwo = 1;
}
if ($dropdownValueB == "2b"){
$valuetwo = 2;
}
if ($dropdownValueB == "3b"){
$valuetwo = 3;
}
if ($dropdownValueB == "4b"){
$valuetwo = 4;
}
if ($dropdownValueB == "5b"){
$valuetwo = 5;
}
echo $valueone + $valuetwo;
It's totally not clear what is your problem and what is not works as expected. But your code is... not good :) Maybe try something like
if( preg_match('/^([1-5])a$/', $dropdownValueA, $m) ) {
$valueone = $m[1];
}
if( preg_match('/^([1-5])b$/', $dropdownValueA, $m) ) {
$valuetwo = $m[1];
}
echo (int)$valueone + (int)$valuetwo;
If the numbers will always come first, this will be your best option.
$dropdownValueA = $_POST["dropdown1"];
$dropdownValueB = $_POST["dropdown2"];
$valueone = intval($dropdownValueA);
$valuetwo = intval($dropdownValueB);
echo $valueone + $valuetwo;
3v4l HERE

PHP function inside query wont return value

So, I have a PHP Script that would insert an attendance of the student. First it should check if the attendance with the same student ID and date already exist. If not, it inserts the attendance. I want to insert in the attendance status whether the student is present or late based on my php function compareTime's return value. But the problem is, the function wont return anything. That's why in the attendance data the status column is blank.
function checkAttendance($conn, $stud_id) {
$check = mysqli_query($conn, "SELECT * FROM tbl_attendance WHERE student_id = '$stud_id' AND date = CURDATE()") or die (mysqli_error());
if(mysqli_num_rows($check) > 0) {
return true;
}
else {
return false;
}
}
function compareTime($time,$grade) {
$ctime = strtotime($time);
if($grade == "H1" || $grade == "H2" || $grade == "H3" || $grade == "H4" || $grade == "S1" || $grade == "S2") {
if ($ctime > strtotime('05:00:00') && $ctime < strtotime('07:30:00')) return "P";
else if ($ctime > strtotime('07:30:00') && $ctime < strtotime('8:30:00')) return "L";
}
else {
if($ctime > strtotime('05:00:00') && $ctime < strtotime('07:15:00'))
return "P";
else if ($ctime > strtotime('07:15:00') && $ctime < strtotime('8:30:00'))
return "L";
}
}
function recordAttendance($conn, $sid, $glid, $scid) {
$sql = mysqli_query($conn, "INSERT INTO tbl_attendance(student_id, gradeLevel_id, section_id, date, arrival_time, status) VALUES ('".$sid."', '".$glid."', '".$scid."', CURDATE(), CURTIME(), '".compareTime(date('H:i:s'),$glid)."')") or die(mysqli_error());
}
$res = mysqli_query($connect, "SELECT * FROM tbl_student WHERE card_id = '$cardID'")or die(mysqli_error());
while($row = mysqli_fetch_array($res,MYSQLI_ASSOC)) {
if (checkAttendance($connect, $row['student_id']))
echo "Already Exist!";
else
recordAttendance($connect, $row['student_id'], $row['gradeLevel_id'], $row['section_id']);
}
It could have several reasons actually, but you'll need to debug to see which is the case.
First start debugging by outputting the values of compareTime to see if they actually work at all. If it doesn't, fix that first.
If that works you could try adding a 'hardcoded' value to your query to see if that gets inserted into your (test) data/table. If that doesn't produce a good result either it might be a case of wrong type in the database. Maybe it's expecting an integer or something else?
You could also extract the function call from the query and put it in a variable, it will provide a bit cleaner and more readable code, as well as it being easier to debug.
p.s.
Did you realise that, in the unlikely event of a time being exactly 7:30:00 or 7:15:00, a person falls between the cracks because you only check for greater or smaller than. Some should also be >= or <=
Might be better to rewrite it to something like this:
function compareTime($grade,$time = ''){
$time = (empty($time))?strtotime(date('H:i:s')):strtotime($time);
$time_5 = strtotime('05:00:00');
$time_715 = strtotime('07:15:00');
$time_730 = strtotime('07:30:00');
$time_830 = strtotime('08:30:00');
if ($grade == "H1" || $grade == "H2" || $grade == "H3" || $grade == "H4" || $grade == "S1" || $grade == "S2") {
if (
$time > $time_730
&& $time < $time_830
){
return "L";
} elseif (
$time > $time_5
&& $time <= $time_730
){
return "P";
}
} else {
if (
$time > $time_715
&& $time < $time_830
){
return "L";
} elseif (
$time > $time_5
&& $time <= $time_715
){
return "P";
}
}
}

Choose a string based on random number

I am working on a bit of PHP and I've come upon a bit of issues.
I am using PHP to randomly choose a number from 1-360. I am trying to compare the answer to a list of value determined by range.
$NumberA = rand(0,180);
$NumberB = rand(0,180);
$NumberC = $NumberA + $NumberB;
if ($NumberC = range(0,21) {
$result = "Orange";
}
elseif ($NumberC = range(22,42) {
$result = "Red";
}
elseif ($NumberC = range(43,63) {
$result = "Blue";
}
//This goes on for a while ...
else {
$result = "Green";
}
echo = $result;
Anytime i do this, the result always assigns the value of "Orange" to $result .
Im sure im doing something wrong here, please help!
First of all, you used just one '=' to compare while it should have been '=='. Second range() generates an array and you cannot compare an integer to an array. Third why generating the range every single time when you can check that $NumberC lies between the minimum and the maximum numbers of the range?
Change your code to:
$NumberA = rand(0,180);
$NumberB = rand(0,180);
$NumberC = $NumberA + $NumberB;
if ($NumberC >= 0 && $NumberC <= 21) {
$result = "Orange";
} elseif ($NumberC >= 22 && $NumberC <= 42) {
$result = "Red";
} elseif ($NumberC >= 43 && $NumberC <= 63) {
$result = "Blue";
} else {
$result = "Green";
}
echo $result;
Shall work. Hope this helps.

Best way to check 3 variable values equals to zero inside if condition - PHP

I am checking 3 variables are equals to zero inside if condition , currently i am doing some thing like this
if($diff_colour_code==0 && $diff_upholstery_code==0 && $big_diff==0 )
is there any better way to do this
I am thinking a way like
if($diff_colour_code==$diff_upholstery_code==$big_diff==0 )
Please help , Thanks in advance .
This should work for you:
You can give the function as many arguments as you need!
<?php
$diff_colour_code = 0;
$big_diff = 0;
$diff_upholstery_code = 0;
function zeroCheck() {
foreach(func_get_args() as $arg)
if($arg == 0)
continue;
else
return false;
return true;
}
if (zeroCheck($diff_colour_code, $big_diff, $diff_upholstery_code))
echo "all are 0!";
?>
you can use ! as :
if(!$diff_colour_code && !$diff_upholstery_code && !$big_diff )
You could do something like this:
$var1 = 0;
$var2 = 0;
$var3 = 0;
$array = compact("var1", "var2", "var3");
$countValues = array_count_values($array);
if($countValues[0] == count($array)){
echo "yes";
}else{
echo "no";
}
or this
if(($var1 == 0 && $var1 == $var2 && $var2 == $var3)){
echo "yes";
}else{
echo "no";
}

Categories