I have a php script in which I have a if-else condition.. I want the script to end processing and start executing back from the top if the condition is not met... example :
<?php
$var = 2;
if($var != 1){
stop execution and go to the top and start executing again...
}else{
echo $var;
}
?>
How can I do that?
This can be achieved with the goto statement:
http://php.net/goto
If you want a infinite loop, then most simply put a infinite loop ... then break when you want to break
<?php
while(true){
$var = 2;
if($var != 1){
continue;
}else{
echo $var;
break;
}
}
?>
Related
While running my code on localhost I had a problem with the include command.
Here's my code:
<?php
$res = 2; // this also can be change to any number. it is based on user input, but for simply the problem i make it to be set manually
If ($res =1,){
$open = include ("weekend.php");
}
else{
$open = include ("weekday.php");
}
echo $open;
?>
I expected the output to be weekday.php, but the output is weekend.php.
It works fine if I use $res = 1.
Besides the obvious typos (the capital I in If, the comma at the end of the condition), you're using the wrong operator. = is the assignment operator. In order to check equality, you should use the == operator:
if ($res == 1) {
// ---^
$open = include ("weekend.php");
}
else {
$open = include ("weekday.php");
}
I want to have if the variable = null then he makes it to 1.
if the variable exist do nothing and dont make it again to 1.
i got this code:
if (isset($_POST["register"])) {
if(!$l_NextPage){
$l_NextPage = 1;
echo "helaas" . "</br>";
}
if($l_NextPage == 1){
echo "hoi";
$l_NextPage = 2;
}else if($l_NextPage == 2){
echo "doei";
}
}
only the code dont work i tried empty, isset, $var == FALSE but everytime he makes $l_NextPage to 1. is there any solution i tried this too with session but even it don't work!
What am I doing wrong?
what happen when you refresh page, it assign $l_NextPage = 1 every time, thats why all the time hoi printed
you can use sessions for preserving value of variable after page refresh
try this code
// write this line of code at top of php block
session_start();
if (isset($_POST["register"]))
{
if (!isset($_SESSION["l_NextPage"]))
{
$_SESSION["l_NextPage"] = 1;
echo "helaas" . "</br>";
}
if($_SESSION["l_NextPage"] == 1)
{
echo "hoi";
$_SESSION["l_NextPage"] = 2;
}
else if($_SESSION["l_NextPage"] == 2)
{
echo "doei";
//unset( $_SESSION['l_NextPage'] ); unset varibale
}
}
after reaching at prefixed condition you can unset varible using
unset( $_SESSION['l_NextPage'] );
i have not tested code but this should work
you should try:
if(!isset($l_NextPage)) {
$l_NextPage = 1;
echo "helaas" . "</br>";
}
elseif($l_NextPage == 1) {
(...)
try like this,
if(!empty(trim($l_NextPage)))
Is there something in php that can halt or let the script proceed if things are ok?
In my current scripts I do like this (for this example):
$fetch = false;
if($i == 10){
echo 'I is clear'
$fetch = true;
}
if($fetch){
//Do database work here
}
echo 'This should be seen no matter what the above';
Instead of the $fetch in there, can I do something else? I don't want to stop the entire script after that like what die() or exit does.
Here's an example that should help you:
<?php
function doLoop() {
for($i=0;$i<100;$i++) {
if($i != 50) {
continue; //It's not 50, skip it
}
//Otherwise
printf("Loop: $i");
}
}
function doBreak() {
for($i=0;$i<100;$i++) {
if($i != 49) {
continue; //It's not 49 yet, continue
} //Otherwise, break
printf("Loop: $i");
break;
}
}
doLoop();
doBreak();
?>
break; can be used to end a loop when a condition is met, while continue; can also be used to skip a certain value if a condition is not met. Using die(); would stop your whole script from executing, preventing it to call anything that comes after the die(); statement because that's how the execution of the scripts pretty much go, from the top to the end.
Let's take a look at the following code:
if ($a == 1) {
echo "this is stage 1";
}
else if ($a == 2) {
echo "this is stage 2";
}
else if ($a == 3) {
$a = 1;
// at this point I want something that restarts the if-else construct so
// that the output will be "this is stage 1"
}
I'm working on an if else construct at the moment and let's say that I have three stages and the if-else construct checks which stage I'm in.
Now it happens that some activities in stage 3 lead to a jump back to stage 1. Now I've already passed the code for stage one, which is why I want to somehow restart the if-else construct. Is there a way to do that? And even more important: Is there a better way to do what I want? Because my idea doesn't seem to be good practice.
You're right, it's bad practice.
You're asking for goto.
Example:
<?php
goto a;
echo 'Foo';
a:
echo 'Bar';
The above would never output 'Foo'
It's difficult to suggest the better method without seeing exactly what you're trying to do, but consider a switch.
switch ($a) {
case 3:
// Execute 3 stuff
// No break so it'll continue to 1
case 1:
// Execute 1 stuff
break // Don't go any further
case 2:
// 2 stuff
break;
}
That's probably not what you want either.
You may just want to abstract the code into functions and call them multiple times if necessary.
You can put an endless loop around your if and break out if you're done
while (1) {
if ($a == 1) {
echo "this is stage 1";
break;
}
else if ($a == 2) {
echo "this is stage 2";
break;
}
else if ($a == 3) {
$a = 1;
}
else {
break;
}
}
Maybe you want to look at Wikipedia - Finite-state machine and this question PHP state machine framework
The short answer is yes, there is a way, but the better answer is yes to your second question as well.
Put, at very least, the code that can get called from multiple locations in a function. For example,
function stageOneCode() {
//do stuff;
}
etc.. I would recommend a function for each stage, but it's hard to make recommendations without actually seeing what's being executed in the stages.
In any event, at the end of your stage three function, simply call your stage one function.
A recursive function is helpful for this (but maybe overkill if it will always revert back to 1)
function echo_stage($stage) {
if ($a == 1) {
return "this is stage 1";
}
else if ($a == 2) {
return "this is stage 2";
}
return echo_stage(1);
}
echo echo_stage(5);
Or:
switch ($number)
{
case 2 :
echo "this is stage 2";
break;
case 1:
default:
echo "this is stage 1"
}
use switch(). you can have a "default" case as well as specific cases.
A loop is what you are searching for:
// initialize $a
$a = 1;
// the while loop will return endless
while (true);
// if you want to break for any reason use the
// break statement:
// if ($whatever) {
// break;
// }
if ($a == 1) {
echo "this is stage 1";
}
else if ($a == 2) {
echo "this is stage 2";
}
else if ($a == 3) {
$a = 1;
// continue will go back to the head
// of the loop (step 1) early:
continue;
}
// don't forget to increment $a in every loop
$a++;
}
no idea how to ask this question... the problem is that I need to stop the loop from runing if if ($current[$id][$i]['item'] == '') is true, so whatever the script needs to do or in this case echo, don't get repeated 15 times.
If using exit; the whole page will stop to render, so, any ideas ?
Thanks in advance.
If the question is not clear enough don't hesitate to ask using the comments, I will more than happy to clarify any doubts.
$i=1;
while($i<=15) {
if ($current[$id][$i]['item'] == '') {
echo 'Done!.';
//do something
}
$a++;
}
use break
$i=1;
while($i<=15) {
if ($current[$id][$i]['item'] == '') {
echo 'Done!.';
break;
}
$i++; //<- that should probably be $i instead of $a?
}
How about using the break control structure?
You need to break. Also, your current loop is endless and will therefore time-out. Presumably you meant to increment $i, not $a.
$i=1;
while($i<=15) {
if ($current[$id][$i]['item'] == '') {
echo 'Done!.';
break; //<-- terminate loop
}
$a++; //<-- shouldn't this be $i++?
}
There is only 2 ways to break a loop... using exit; and break;....
break; is what you want it will exit the loop and continue execution, exit will end the script.
$i=1;
while($i<=15) {
if ($current[$id][$i]['item'] == '') {
echo 'Done!.';
break; // or you can make value of $i = 20;
}
$a++;
}