Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 01-18-2005, 05:03 PM help please
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
i'm new to php and i need some help, i made this for a site i'm making, but i get this error: Fatal error: Maximum execution time of 30 seconds exceeded in /home/b2l1366/public_html/t/fight.php on line 15 i know its probabaly something stupid but becasue i don't know much about php i'm not sure whats wrong

PHP Code:
<?php
session_start
();
include 
"header.php";

//monster stats
$monsterhealth=10;
$monsterstrengh=5;
//player stats
$playerhealth=$get3[health];
$playerstrengh=$get3[strengh];

$set=1;
//fight time
while ($set>0){
while (
$set==$playerhealth>0){
$playerattack=rand(1,$playerstrengh);
$monsterhealth=$monsterhealth-$playerattack;
echo 
"You attack the $action, and do $playerattack damage, the $action has $monsterhealth  health left<br>";
if (
$playerhealth<=0){
$set=0;
} else{
$set=2;
}
} while (
$set==$monsterhealth>0){
$monsterattack=rand(1,$monsterstrengh);
$playerhealth=$playerhealth-$monsterattack;
echo 
"The $action attacks you and does $monsterattack, you have $playerhealth health left";
if (
$playerhealth<=0){
$set=0;
} else{
$set=1;
}
}
}

include 
"footer.php";
?>
timsquash5 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 01-18-2005, 05:42 PM
foobar's Avatar
Extreme Talker

Posts: 225
Trades: 0
Basically, one of your loops is never ending. You've made an infinite loop.

A few things you should fix, and that may (or may not) fix your problem.

1) Use && as the AND operator. & is the bitwise AND operator, and doesn't function the way you want it to (in your case).

2) This line:

PHP Code:
} while ($set==$monsterhealth>0){ 
Should be split into two, like so:

PHP Code:

while (
$set==$monsterhealth>0){ 
Don't close a loop and start one on the same line. The way you have it is the way you write the condition for a DO WHILE loop, when you're using a regular WHILE loop.

3) Indent your code! I don' know if it's just because you copy/pasted it or whatever, but if you don'T indent your code, make sure you do. Especially when using loops. Makes it a LOT easier to see what's going on.

4) When getting wariables using the $_GET superglobal, put the index name in '' (so $_GET['foo']), and capitalize the GET. It's just a standard practive to capitalize constants and system vars in this case.

5) You're starting a session at the top, but aren't setting any variables. Why's this?

Let me know if any of that helped (or didn't).


------------------------ FOOBAR EDIT -------------------------

I found the problem. In your first loop, you have the player attacking the monster. Thus, you should be checking the monsters health. Instead, you're checking the player health in your if statement.


------------------------ /FOOBAR EDIT -------------------------

Last edited by foobar; 01-18-2005 at 05:48 PM.. Reason: Found the problem.
foobar is offline
Reply With Quote
View Public Profile Visit foobar's homepage!
 
Old 01-18-2005, 05:51 PM
Extreme Talker

Posts: 219
Location: UK, East Anglia
Trades: 0
Quote:
3) Indent your code! I don' know if it's just because you copy/pasted it or whatever, but if you don'T indent your code, make sure you do. Especially when using loops. Makes it a LOT easier to see what's going on.
thanks for replying, i don't understand what you said above.
il see if your solution works tomorrow, i'm going to bed now
timsquash5 is offline
Reply With Quote
View Public Profile
 
Old 01-18-2005, 06:26 PM
Phaedrus's Avatar
Ultra Talker

Posts: 271
Location: CA
Trades: 0
Quote:
Originally Posted by timsquash5
thanks for replying, i don't understand what you said above.
il see if your solution works tomorrow, i'm going to bed now
He means make your code look like this:

PHP Code:
<?php
session_start
();
include 
"header.php";

//monster stats
$monsterhealth=10;
$monsterstrengh=5;

//player stats
$playerhealth=$get3[health];
$playerstrengh=$get3[strengh];

$set=1;

//fight time
while ($set>0)
{
   while (
$set==$playerhealth>0)
   {
        
$playerattack=rand(1,$playerstrengh);
        
$monsterhealth=$monsterhealth-$playerattack;
        echo 
"You attack the $action, and do $playerattack damage, the $action has $monsterhealth  health left<br>";
      
        if (
$playerhealth<=0)
       {
          
$set=0;
       }
       else
       {
          
$set=2;
       }
   } 

   while (
$set==$monsterhealth>0)
   {
        
$monsterattack=rand(1,$monsterstrengh);
        
$playerhealth=$playerhealth-$monsterattack;
        echo 
"The $action attacks you and does $monsterattack, you have $playerhealth health left";
        
        if (
$playerhealth<=0)
       {
           
$set=0;
       }
        else
       {
           
$set=1;
       }
   }
}

include 
"footer.php";
?>
That way it's easy to see what's going on in a loop or if statement and also whether or not they are closed. Each coder has their own style, but the point is to make it human-readable. After all, if the syntax is correct, the parser can read it no matter how messy it is. But if there's a bug, YOU're the one who has to fix it, so YOU'd better be able to read it.
__________________

Please login or register to view this content. Registration is FREE

Last edited by Phaedrus; 01-18-2005 at 06:30 PM.. Reason: use [php] tags
Phaedrus is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to help please
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.49639 seconds with 12 queries