The following PHP code is working as expected. I need to echo "copy error" message when the code is unable to execute the command in 4 attempts.
for($num_tries = 0 ; $num_tries < 4 ; $num_tries++)
{
$cloudcmd = "cp abc xyz ";
system($cloudcmd,$status);
if($status != 0)
{
sleep(3) ;
continue ;
}
break ;
}
I tried to add echo command after break; but it does not seem to work.
Strictly, you don't need to introduce additional variables and checks to do this.
Keeping it simple, you can inspect the value of the for loop variable $num_tries once outside the for loop:
if($num_tries==4)....
You also have the $status variable available after the for loop.
if($status!=0)....
$success = false;
for($num_tries = 0 ; $num_tries < 4 ; $num_tries++)
{
$cloudcmd = "cp abc xyz ";
system($cloudcmd,$status);
if($status != 0)
{
sleep(3) ;
continue ;
}
$success=true;
break ;
}
if($success)
{
//do your thing
}
Try doing this:
for ($num_tries=0;$num_tries < 4;$num_tries++)
{
system('cp abc xyz',$status);
if ($status === 0)
{
break;
}
sleep(3);
}
if ($status !== 0)
{
echo 'error';
}
This loop will break as soon as $status is 0, meaning the system() call was a success, after the loop, the $status value will be accessible, still (PHP doesn't have block scope), so check it's value and echo if needed. You can easily replace the echo statement using an exit(), to stop the rest of the script.
<?php
for($num_tries = 1 ; $num_tries <=4 ; $num_tries++){
$cloudcmd = "cp abc xyz ";
system($cloudcmd,$status);
if($status != 0) {
if($num_tries!=4){
sleep(3);
continue;
} else {
echo "error";
}
}
break;
}
?>
A break statement causes the loop to break, and any code that occurs after the break, within the loop is skipped over.
Perhaps, you could do something like this:
$success = false;
for($num_tries = 0 ; $num_tries < 4 ; $num_tries++) {
if (success()) {
// do something
$success = true;
}
}
echo ($success) ? 'Success' : 'Failure';
Related
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)))
Let's say I have a simple code:
while(1) {
myend();
}
function myend() {
echo rand(0,10);
echo "<br>";
if(rand(0,10) < 3) break;
}
This will not work with error code 'Fatal error: Cannot break/continue 1 level on line'.
So is there any possibility to terminate the loop during a subfunctin execution?
Make the loop condition depend upon the return value of the function:
$continue = true;
while( $continue) {
$continue = myend();
}
Then, change your function to be something like:
function myend() {
echo rand(0,10);
echo "<br>";
return (rand(0,10) < 3) ? false : true;
}
There isn't. Not should there be; if your function is called somewhere where you're not in a loop, your code will stop dead. In the example above, your calling code should check the return of the function and then decide whether to stop looping itself. For example:
while(1) {
if (myend())
break;
}
function myend() {
echo rand(0,10);
echo "<br>";
return rand(0,10) < 3;
}
Use:
$cond = true;
while($cond) {
$cond = myend();
}
function myend() {
echo rand(0,10);
echo "<br>";
if(rand(0,10) < 3) return false;
}
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.
Following is my code in which i am facing only one difficulty that when i run the following script then due to comparison failure the $flag doesnot echo kindly let me know how to fix this?
$s = "iph4on comes";
$se = "4gb comes in iphone";
$f = 0;
$tf = explode(" ",$searching);
$ms= explode(" ",$search_in);
foreach($tf as $word)
{
if (!preg_match("/$word/i", $search_in))
//if (!strpos($search_in, $word));
return false;
}
{
$f = 1;
}
echo $f;
//Due to return flase above i am not echoing
echo "Comparison Failed";
return terminates the currently executing code block and "returns" to whatever called that code. if you execute a return in the top level of the code, it's essentially an exit() call and your echo will never be reached.
Why not just put an echo $flag before the return statement?
if (!preg_match("/$word/i", $search_in)){
echo $flag
return false;
}
You need to put the echo before the return. return gets the execution back to the calling function, hence any code after that won't be executed.
if (!preg_match("/$word/i", $search_in)) {
$flag = 1;
echo $flag;
echo "Comparison Failed";
return false;
}
I have a rather big if statement:
if (!$result_spam)
{
$confrim_spam = "FAILED";
}
else if ($result_spam)
{
$confrim_spam = "PASSED";
}
if (!$result_email_manage)
{
$confrim_email_manage = "FAILED";
}
else if ($result_email_manage)
{
$confrim_email_manage = "PASSED";
}
if (!$result_analyt)
{
$confrim_analytics = "FAILED";
}
else if ($result_analyt)
{
$confrim_analytics = "PASSED";
}
Now I want to do another if statement to check if all have PASSED or if all have FAILED or is some have PASSED and some have FAILED and then echo (do something with) the failed ones.
I know how to check if all have passed or failed:
if ($confirm_spam == "PASSED" AND $confirm_analytics == "PASSED"
but to check if some have passed and some haven't and then find the ones that failed will take too long, right?
I was just wondering, would there be an easier/quicker way to do this?
Since they are all bools anyway:
if($result_spam && $result_email_manage && $result_analyt){
//do all passed
}
elseif($result_spam || $result_email_manage || $result_analyt){
//at least one passed
if(!$result_spam){ echo '$result_spam failed';}
if(!$result_email_manage){ echo '$result_email_manage failed';}
if(!$result_analyt){ echo '$result_analyt failed';}
}
else {
//do all failed
}
You can change validation logic to something like
$passed = array();
$failed = array();
if (!$result_spam)
{
array_push($failed, "confirm_spam");
}
else
{
array_push($passed, "confirm_spam");
}
...
Then you have an easy and clear way to check whether all passed/failed and which tests are failed.
What if you try this way:
$passed = $failed = "";
$all = array("confrim_spam" => $result_spam,
"confrim_email_manage" => $result_email_manage,
"confrim_analytics" => $result_analyt);
foreach($all as $a => $b)
{
if (!$b)
$failed.= $a . ", ";
else
$passed.= $a . ", ";
}
Then if var $passed is empty, none passed else if $failed is not empty, at last one have not passed.. so do you got what passed and what failed and do something with them. And you can store results both in a string or an array whatever you want...