Php Help, quizes

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Post Reply
liquedus
php-forum Active User
php-forum Active User
Posts: 266
Joined: Tue Apr 08, 2003 5:18 am
Location: Ottawa, Canada
Contact:

Post by liquedus » Sun Jul 13, 2003 12:45 pm

is this what you are looking for??

if($car2 > $car1 && $car2 < $car3) {...}
if......

also try looking at switch statements, maybe those will suit you more.

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Re: Php Help, quizes

Post by swirlee » Sun Jul 13, 2003 6:24 pm

apollovega wrote:if ($Cow > $Dog && $Cat && $Pig) { echo "Your a Cow!"; Exit; }
if ($Dog > $Cow && $Cat && $Pig) { echo "Your a Dog!"; Exit; }
if ($Cat > $Dog && $Cow && $Pig) { echo "Your a Cat!"; Exit; }
if ($Pig > $Dog && $Cat && $cow) { echo "Your a Pig!"; Exit; }


This is perhaps one of the most common programming mistakes in all of programming history. Well, at least since the 'sixties. Anyway, conditionals don't work that way. Even though it sounds natural enough to say, "dog is greater than cow and cat and pig", it doesn't make any sense to PHP. You have to say "dog is greater than cow and dog is greater than cat and dog is greater than pig". It's a lot more to say, but it's what you have to do. So, like this:

Code: Select all

<?
   if($cow > $dog &&
      $cow > $cat &&
      $cow > $pig
   ) {
      echo "You're a cow!";
      exit;
   }
   elseif(  $dog > $cow &&
            $dog > $cat &&
            $dog > $pig
   ) {
      echo "You're a dog!";
      exit;
   }
   elseif(  $cat > $dog &&
            $cat > $cow &&
            $cat > $pig
   ) {
      echo "You're a cat!";
      exit;
   }
   elseif(  $pig > $dog &&
            $pig > $cat &&
            $pig > $cow
   ) {
      echo "You're a pig!";
      exit;
   }
?>


It's also important to note that "Exit;" is not a valid statement in PHP. The statement you're looking for is "exit;". I also cleaned up your code and your grammar a bit, because I'm compulsive like that.

There are, of course, far easier, or at least cleaner, ways to do this. For example, you could create a greatest() function that would just return whichever is greatest so you don't have to repeat all that code. But I'll leave that as an exercise for you.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest