Check if int is between negative and positive values

Hi,

Why does this not work?
($connection_port_top_angle = -370)

if(($_GET["connection_port_top_angle"] >= -360) && ($_GET["connection_port_top_angle"] <= 360)) {

 - This return true..
 - Only allow numbers between -360 and +360
kgp43Asked:
Who is Participating?
 
Ray PaseurCommented:
Please see: http://iconoun.com/demo/temp_kgp43.php

<?php // demo/temp_kgp43.php
/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28629031.html
 *
 * Limit Integer Values in PHP
 *
 */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);

// Receive the request input, cast it to integer or PHP FALSE
if (!empty($_GET['q']))
{
    if (!is_numeric($_GET['q']))
    {
        echo PHP_EOL . "PLEASE ENTER AN INTEGER";
    }
    else
    {
        $val = !empty($_GET['q']) ? (int)$_GET['q'] : FALSE;

        // If there is non-FALSE, we have usable input!
        if ($val !== FALSE)
        {
            if ($val < -360)
            {
                echo PHP_EOL . "$val IS BELOW THE ALLOWABLE MINIMUM";
            }
            elseif ($val > +360)
            {
                echo PHP_EOL . "$val IS ABOVE THE ALLOWABLE MAXIMUM";
            }
            else
            {
                echo PHP_EOL . "$val IS ACCEPTABLE";
            }
        }
    }
}
// CREATE THE ORM TO RECEIVE INPUT
$form = <<<EOD
<form>
Enter an integer between -360 and +360:
<input name="q" />
<input type="submit" />
</form>
EOD;

echo $form;

Open in new window

0
 
Rgonzo1971Commented:
HI,

does that work

if($connection_port_top_angle >= -360 && $connection_port_top_angle <= 360)

Regards
0
 
Brian TaoSenior Business Solutions ConsultantCommented:
You said $connection_port_top_angle = -370, but you're evaluating $_GET["connection_port_top_angle"].
One is a normal variable and the other is an array element.  They are not referring to the same thing.

So why not provide more detail and/or context?
0
 
kgp43Author Commented:
Perfect, thanks a lot.
0
 
Ray PaseurCommented:
Thanks for the points - great question!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.