Hi, I have a little difficulty getting this condition together...

I have a variable $var that will assume any value (characters and numbers) but that has to have a value (required) and a variable $n that also will assume any value and has to have a value (required). I need to build a condition that will do a certain thing ONLY if $var does NOT have and $n has any value that is not a number (in particular between 1 and 50). In other words if $var has any value and $n has any NUMERICAL value then it's ok but if either $var has no value or $n has a value that is NOT numberical then it is not ok.

I tried this:

if ( !$var && $n >= "1" && $n <= "50" ) {
not ok
} else {
it's ok
}

but it doesn't work.

Can somebody help me?

    You can use gettype() to test the type of any variable.

    <?php
    
    $var; // declaring w/o assigning
    echo gettype( $var ); // returns null
    echo "<br />";
    
    $var = 5;
    echo gettype( $var ); // integer
    echo "<br />";
    
    $var = "five";
    echo gettype( $var ); // string
    echo( "<br />");
    
    $var = 5.0;
    echo gettype( $var ); // double
    echo( "<br />"); 
    
    $var = true;
    echo gettype( $var ); // boolean
    echo "<br />";
    
    ?>
    

    you can use settype() to make sure it's the type of var you want it to be.

    $var = 3.14; // pi ;p
    echo gettype( $var ); // returns double
    echo " is $var<br />"; // 3.14
    settype( $var, 'integer' );
    echo gettype( $var ); // now an integer. 
    

    I don't know if thats what you were looking for or not. Either way, i hope it helps ya out!

      First of all, see [man]is_numeric[/man].

      Second, I'll just paraphrase your condition:

      In yet other words; if(!empty($var) && is_numeric($n)){
      ok
      }else
      not ok
      }

        Thanks to the both of you... Don't know grettype() but it seems like is_numeric() is exactly what I am looking for because I need to make sure that the value is numeric...

        I tested it like this:

        if (!$var || !is_numeric($n))

        and it worked perfectly...

        Now I only have one doubt/question/curiosity... is there a difference between my syntax (!$var) and yours (!empty($var))? Is mine wrong? Are they the same thing or are there differences?

          The only significant difference is that empty($var) won't cause a warning about an "uninitialised variable" if $var happens to not be set. It won't make a difference if error reporting is set leniently enough.

            Hi ,

            The function below returns a string "0" if ur string is not a valid
            number .

            It also works for decimal numbers.

            Hope this is what you were looking for

            regds

            <?
            function snum($data)
            {
            $str = trim($data);
            $length = strlen($str);
            $isdot = false;
            $flag = true ;
            if ($length == 0) $flag = false;

            for ($i = 0 ; $i < $length && $flag ; $i++ )
            {

              if (  ( substr($str,$i,1) <= "9" && substr($str,$i,1) >= "0" ) ||  ( substr($str,$i,1) == "." && !$isdot ) )
              {
                 if ( substr($str,$i,1) == "." ) $isdot = true;
              }
              else
              {
                 $flag = false;
              };
            
            };

            if ($flag)
            {
            return($str);
            }
            else
            {
            return("0");
            };

            };

            ?>

              krylor: how is your function snum($data) different from is_numeric($data) ? $data : "0"?

                well not much different .
                Its actually a port of something i wrote in javascript
                and i also read somewhere that isnumeric in php has a limitation
                of some no of digits

                  Originally posted by krylor
                  well not much different .
                  Its actually a port of something i wrote in javascript
                  and i also read somewhere that isnumeric in php has a limitation
                  of some no of digits

                  Ah, probably limited to numbers that can actually be stored in a numeric variable type.

                  preg_match("/^[\d.]+$/",$data) && substr_count($data,'.')<=1;
                  
                  

                  ...could probably be ported to Javascript...

                    Write a Reply...