Reputation: 132
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];
}
Upvotes: 0
Views: 164
Reputation: 57685
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
:
<?php
$variable1 = "1345064a";
$variable2 = "135a";
if ($variable1 < $variable2) {
echo "This code is displayed";
}
else {
echo "This code should be displayed";
}
// Output: This code is displayed
?>
Cast your variables to int
<?php
$variable1 = "1345064a";
$variable2 = "135a";
if ((int) $variable1 < (int) $variable2) {
echo "This code is displayed";
}
else {
echo "This code should be displayed";
}
// Output: This code should be displayed
?>
You can check the type of your variables using gettype()
. You should never use the output of gettype()
to test for one particular type, since the output string is liable to change from one version of PHP to another.
Upvotes: 1
Reputation: 6248
assuming that missing the $ is just a typo ...
when you run into this type of problem, the best thing is to post a full working code sample. for example, the code below works as it should. it displays "This code should be displayed". what happens when you run it? what is the difference between this code and yours? answer those two questions and you'll be at least pointed to your answer:
<?php
$variable1 = 1345064;
$variable2 = 135;
if ($variable1 < $variable2) {
echo "This code is displayed";
}
else {
echo "This code should be displayed";
}
?>
Upvotes: 1
Reputation: 18853
You are using variable names wrong. A variable needs to be preceded by a $
.
$variable1 = 4;
$variable2 = 5;
if ($variable1 < $variable2) {
echo 'Yep';
}else {
echo 'Nope';
}
You should look into the basic syntax of PHP namely the Variables Section.
Upvotes: 4