tags:

views:

67

answers:

6

Hi, 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?

+2  A: 

maybe the $ before variables:

if($variable1 < $variable2)
pedrorezende
In this code, I made a mistake, but in real code, I have a "$".Sorry.
oski225
@oski255: use copy-paste!
greg0ire
My real code is:$zmienna1 = SprTransfer($login);$zmienna2 = floor($tmp); if($zmienna1 < $zmienna2) { echo 'pff'; } else { echo 'OK'; }
oski225
+4  A: 

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.

Brad F Jacobs
My real code is:$zmienna1 = SprTransfer($login);$zmienna2 = floor($tmp); if($zmienna1 < $zmienna2) { echo 'pff'; } else { echo 'OK'; }
oski225
Next time post the real code?
Brad F Jacobs
+1  A: 

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";
    }
?>    
Don Dickinson
[Working example of code above](http://codepad.viper-7.com/VANyh9)
Peter Ajtai
A: 

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];
}
?>
oski225
Damn, my code id wrong pase :(
oski225
Welcome to Stack Overflow. In the future, unless you're answering your own question, you should edit your original post by using the Edit link underneath it (this site is structured a bit differently than a forum. Look at some questions and you'll catch onto it).
yuval
+1  A: 

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
?> 

exmple


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
?>  

example


Debugging:

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.

Peter Ajtai
how to check whether a variable is a string? And how can I possibly compare those variables?
oski225
@oski225 - I edited it into my answer - `gettype()`. When strings are compared, the one that comes first alphabetically is "less than" the other.
Peter Ajtai
It's strange, but The result cast: (int)$variable1 is 0 ...
oski225
@oski225 - That's formatted badly, it should be `(int) $variable` - Note the dollar sign ( `$` ). Click on the example links and play with those.
Peter Ajtai
My mistake again, but (int)$variable1 is 0 too.I checked the function gettype:variable1 is stringvariable2 is double
oski225
You can try use intval($var) .
pedrorezende
I try do: if((double) $zmienna1 < (double) $zmienna2), but result is the same. intval($var) doesn't work too.
oski225
Well, somebody know what's wrong? :(
oski225
@oski225 - try `echo $zmienna1 . "\n"; echo $zmienna2 . "\n"; if((float) $zmienna1 < (float) $zmienna2){ ... } else { ... }` The `echo` will allow you to see the values you are working with and go from there...... What are the 2 strings, exactly?
Peter Ajtai
A: 

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];


}
oski225
This is not an answer. Also 4 spaces before a line gets it to format as code (select block and press ctr+k )
Peter Ajtai