newbie question using if and or

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kingdbag
Forum Newbie
Posts: 22
Joined: Wed Jul 12, 2006 6:22 pm

newbie question using if and or

Post 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:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>");
} 
?>
kingdbag
Forum Newbie
Posts: 22
Joined: Wed Jul 12, 2006 6:22 pm

Post by kingdbag »

Sorry about that, there is no way in php to OR both statements into one line?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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>";
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

dull1554 wrote:you can also do this:
You might want to take another look at that.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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>";
} 

?>
kingdbag
Forum Newbie
Posts: 22
Joined: Wed Jul 12, 2006 6:22 pm

Post by kingdbag »

thanks guys... i really appreciate the responese!
Post Reply