I have a function that connects to the database and returns a number and a second function, which returns the size of a file.
If I use the echo function to view the results functions: variable1 = 1345064, and variable2 = 135
,but when I use a comparison, go me incorrect result:
if($variable1 < $variable2)
{
echo 'This code is displayed';
}
else
{
echo 'This code should display';
}
what’s wrong?
My source code:
<?php
include 'funkcje.inc';
$login = $_GET['login'];
$fsize = WielkoscPliku2($file);
$tmp = $fsize / 1000000;
$zmienna1 = SprTransfer($login);
$zmienna2 = floor($tmp);
if($zmienna1 < $zmienna2)
{
echo "This code is displayed";
}
else
{
echo "This code should be displayed";
}
?>
SprTransfer function:
<?php
require "connection.php";
connection();
...
function SprTransfer($login)
{
$zapytanie = "SELECT `transfer` FROM `uzytkownicy` WHERE `nick`='$login'";
$idzapytania = mysql_query($zapytanie);
$sprwaznosc = mysql_fetch_row($idzapytania);
return $sprwaznosc[0];
}
?>
Main file:
include 'funkcje.inc';
$login = $_GET['login'];
$fsize = WielkoscPliku2($file);
$tmp = $fsize / 1000000;
$zmienna1 = SprTransfer($login);
$zmienna2 = floor($tmp);
if($zmienna1 < $zmienna2)
{
echo "This code is displayed";
}
else
{
echo "This code should be displayed";
}
function.inc file:
require "connection.php";
connection();
function SprTransfer($login)
{
$zapytanie = "SELECT `transfer` FROM `uzytkownicy` WHERE `nick`='$login'";
$idzapytania = mysql_query($zapytanie);
$sprwaznosc = mysql_fetch_row($idzapytania);
return $sprwaznosc[0];
}
I’m not sure how, but it looks like you are interpreting your numbers as strings.
For example this will display
This code is displayed:exmple
Cast your variables to
intexample
Debugging:
You can check the type of your variables using
gettype(). You should never use the output ofgettype()to test for one particular type, since the output string is liable to change from one version of PHP to another.