Reputation: 79
The Following PHP if condition statement is processing wrong and is not working.
File $fname has text values: "990,1000" with a comma in between:
$myfile = fopen($fname, "r") or die("<script
type='text/javascript'>window.location.href =
'http://www.autorekrooter.com/activation.html';</script>");
$dt=fread($myfile,filesize($fname));
//File fname has values: "990, 1000"
fclose($myfile);
$myArray = explode(',', $dt);
//Now checking if Limit expired or not?
echo "<br>myArray[0] values is: " . $myArray[0] . " myArray[1] value is: " .
$myArray[1];
if ($myArray[0]>=$myArray[1]){
echo "<script>alert('You can't proceed further');</script>";
//echo "<script type='text/javascript'>window.location.href = 'register.html';</script>";
exit();
}
else{
echo "<script type='text/javascript'>window.location.href =
'gui.php';</script>";
echo "You can proceed";
exit();
}
When I run the above in a php file the if statement always takes value of myArray[0] >= myArray[1]
as true and show the reigster.html whereas in the $fname
file it is 990,1000 myArray[0]=990
and myArray[1]=1000
so myArray[0]
should not be greater than myArray[1]
and the else statement should run but the opposite is happening.
Any suggestions as to why this is not working?
Many Thanks in advance for your answers.
Upvotes: 1
Views: 77
Reputation: 4894
You can use intval to get the integer value only.
Try this
if (intval($myArray[0])>=intval($myArray[1])){
echo "<script>alert('You can't proceed further');</script>";
//echo "<script type='text/javascript'>window.location.href = 'register.html';</script>";
exit();
}
else{
echo "<script type='text/javascript'>window.location.href =
'gui.php';</script>";
echo "You can proceed";
exit();
}
I thing it will work for you.
Upvotes: 1
Reputation: 3240
Try this one
$myArray = array_map('intval', explode(',', $dt));
Upvotes: 1
Reputation: 13004
You probably have a newline (\n
or \r\n
) at the end of your file ($myfile
). So myArray[1]
actually looks like "1000\n"
.
var_dump("990" >= "1000\n");
returns bool(true)
.
You're comparing strings that PHP can't convert to numbers so you get an unexpected result.
Upvotes: 2