Page 1 of 1

newbie question using if and or

Posted: Wed Jul 12, 2006 6:42 pm
by kingdbag
I need some help... thanks!

$freqa= 24.9;

if($freqa != 24.9 || $freqa != 36.4)
{
echo ("<h1>Not running at 24.9 or 36.4 RUNNING AT $freqa</h1>");
}
else
{
echo ("<h1>Is running normal</h1>");
}

thing is no matter what i set freqa to it still says its not running at 24.9 or 36.4 even though it is. :evil:

Posted: Wed Jul 12, 2006 6:51 pm
by RobertGonzalez
First, please wrap PHP code in the appropriate bbCode tags.

Next, here is what it should look like, with comments to answer your question.

Code: Select all

<?php
// Give $freqa a value
$freqa= 24.9;

if ( $freqa != 24.9 )
{
    if ( $freqa != 36.4 )
    {
        // If we are here then $freqa either does not equal 24.9 OR it does not equal 36.4)
        echo ("<h1>Not running at 24.9 or 36.4 RUNNING AT $freqa</h1>");
    }
    else
    {
        // If we are here then $freqa does not equal 24.9 but it DOES equal 36.4)
        echo ("<h1>Running at 36.4</h1>");
    }
}
else
{
    // We are 24.9
    echo ("<h1>Is running normal</h1>");
} 
?>

Posted: Wed Jul 12, 2006 6:54 pm
by kingdbag
Sorry about that, there is no way in php to OR both statements into one line?

Posted: Wed Jul 12, 2006 7:06 pm
by Benjamin
Untested...

Code: Select all

$freqa= '24.9';

switch ($freqa) {
  case '24.9':
  case '36.4':
    echo '<h1>Is Running Normal</h1>';
    break;
  default:
    echo ("<h1>Not running at 24.9 or 36.4 RUNNING AT $freqa</h1>");
}
In your code..

Code: Select all

if($freqa != 24.9 || $freqa != 36.4)
This will always return true, as $freqa is not going to be equal to one of the two.

Posted: Wed Jul 12, 2006 7:11 pm
by dull1554
you can also do this:

Code: Select all

$freqa= 24.9;

if(($freqa != 24.9) || ($freqa != 36.4)) //this should work
{
echo "<h1>Not running at 24.9 or 36.4 RUNNING AT $freqa</h1>";
}
else
{
echo "<h1>Is running normal</h1>";
}

Posted: Wed Jul 12, 2006 7:12 pm
by Benjamin
dull1554 wrote:you can also do this:
You might want to take another look at that.

Posted: Wed Jul 12, 2006 7:26 pm
by dull1554
astions wrote:
dull1554 wrote:you can also do this:
You might want to take another look at that.
ok that dosnt work, this does

Code: Select all

<?php

$freqa= 24.7;

if(($freqa == 24.9) || ($freqa == 36.4)) //this should work
{
echo "<h1>Is running normal</h1>";
}

else
{
echo "<h1>Not running at 24.9 or 36.4 RUNNING AT $freqa</h1>";
} 

?>

Posted: Thu Jul 13, 2006 12:47 am
by kingdbag
thanks guys... i really appreciate the responese!