Home     Articles & Projects     Products & Web Services     Forum

Go back to start of PHP

Hi i have a hard question, SO thought I would ask it here.

Is there anyway to go back to the start of a PHP code, if the result is not accepted by an if statement?

Hi Russell, PHP supports

Hi Russell,

PHP supports "goto"...

http://php.net/manual/en/control-structures.goto.php

However; it is not considered good programming practice to make use of "goto", as there would normally be much better ways to structure your code. For example; where you might want to go back to the top to do something again when an IF statement is false; why not use do { } while(); - for example:

<?php
 
do {
   
// do something that sets $result = TRUE on success
 
} while (!$result);
?>

One thing you must be careful of, and this would apply if you used "goto" as well; is that your script doesn't get stuck in a never ending loop; if, for some reason; "do something" never works. One way around this is to use a counter variable, decrement it in each iteration of the loop and add it to the test. For example:

<?php
  $max
= 5;
  do {
   
// do something that sets $result = TRUE on success
  
$max--;
  } while (!
$result && $max);
?>

Hope this helps!
Cheers,
David.

So would this work if i want

So would this work if i want to make sure that by playing the ad the account does not go into the red.

in other words say an account only has 0.50 in it, but the ad cost 0.55 i want to it rerun the mysql php code and get another ad.

Any chance of making it only

Any chance of making it only run 3 times, and if it still does not get a result to go to another do function.

Hi Russell, Of course I

Hi Russell,

Of course I don't know the details of your application; but I would have thought it might be more practical to SELECT and with a cost that was within the current balance of an account; that way there's no way for loop in PHP...so that might be worth looking at...!

Adds: re your previous question - sure; just set $max = 3 and then after 3 attempts when it comes out of the bottom of the loop $max will be 0; so you can check that to decide whether to do another function; for example:

<?php
  $max
= 5;
  do {
   
// do something that sets $result = TRUE on success
  
$max--;
  } while (!
$result && $max);

  if (!
$result && ($max == 0))
  {
    
// do something else
 
}
?>

Cheers,
David.

I have no idea if the

I have no idea if the following code will make sense to you. but this is the code before I place in your code above

(if possible please hide the code after you read)

{code saved}

Hey david I found an error

Hey david I found an error but cant fix it I need to do a if statment on the do statement. If the xamountspend is less the 0 then it needs to show the word Sorry.

But in this PHP code it for some reason goes to IT WORKS! no matter what result.

<?php
do {
$xamountspend = rand(-100, 100);


} while (
$xamountspent);
 
  if(
$xamountspent < 0)
  {
   print
"Sorry NO WORK!";
  }
  else
  {
  print
"IT WORKS!<br/>";
  print
$xamountspend;
  }
?>

Hi Russell, You have

Hi Russell,

You have different variable names - $xamountspend and $xamountspent - that should be all it is! Try:

<?php
do {
$xamountspent = rand(-100, 100);
} while (
$xamountspent);

  if(
$xamountspent < 0)
  {
   print
"Sorry NO WORK!";
  }
  else
  {
  print
"IT WORKS!<br/>";
  print
$xamountspend;
  }
?>