Php Help, quizes

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

apollovega
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Jul 13, 2003 11:55 am

Php Help, quizes

Postby apollovega » Sun Jul 13, 2003 12:17 pm

Hello, I have a question. It's been bugging me for a bit and I can't seem to figure it out. What I'm trying to do is make a quiz like that off of Emode.com except with php. I pratically have it already except for one little error.... I"ll use an example.

Let's say that I wanted to find out if the Variable $Cow is the highest out of $Dog $Cat $Pig . So I do that, with.. :
if ($Cow > $Dog && $Cat && $Pig) { echo "Your a Cow!"; }

That would find out if the $Cow variable has the biggest value, right? Aiite, so then I want to put all 4 of them down to find out the highest out of all of them, and execute the Echo command on ONLY the highest variable value without having the others follow.. I could do this with the exit; command inside the If { } But see here's my problem.....

I want to make it so that it will execute the highest one out of all 4 not just if the first If command is true, execute that..and if the second If command is true, execute that. Do you know what I mean? Here, I'll try to explain it easier.. ::

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


Now let's say that $Cow is the highest, and $Dog is the second highest etc.. It would normally execute the first "If" command and then exit. Now, let's say that $Cow isn't the highest but the second highest and $Dog is the highest. It would still execute the $Cow first because it was the first If command.. What I'm trying to do is if it ISN'T the highest command, don't execute it.. Ya know?
Well this is getting pretty long.. Any help would be appreciated ^_^ if you don't understand what I'm talking about then please post an "I don't have an idea what the hell your talking about geek" message and I'll reply pronto ;)

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

Postby 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.

apollovega
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Jul 13, 2003 11:55 am

Postby apollovega » Sun Jul 13, 2003 12:56 pm

well see, that's what I have it as already and it still doesn't seem to do what I want it to do...

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

Postby 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.

apollovega
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Jul 13, 2003 11:55 am

Postby apollovega » Mon Jul 14, 2003 10:37 am

Thanks a bunch.


Return to “PHP coding => General”

Who is online

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

cron