Home > front end >  die() or exit() without destructors?
die() or exit() without destructors?

Time:09-23

I would like to stop my php code when a condition is not valid.

Example:

if (strlen($string) < 6){   

 echo "minimum 6"

}   

if (strlen($string) > 32){   

echo "maximum 32"

}   

echo "success"

i would like stop the php code if the first condition is not met that it stops the script (so that it does not display success)

if I use die or exit it deletes the whole page, I just want to stop the script and leave the page as it is...

CodePudding user response:

die and exit are PHP keywords that cause the code to cease execution immediately

<div>
<?php
echo "123";
exit();
echo "456"
?>
</div>

so this code will do the following

add "<div>" to the output buffer 
add "123" to the output buffer
kills the process!

This means that any unsent data in the output buffer will not be sent.

If you instead don't wish the process to end then you need to use conditional logic.

<div>
<?php
if (strlen($string) < 6){   
    echo "minimum 6"
}
else if (strlen($string) > 32){   
    echo "maximum 32"
}   
else {
    echo "success"
}
?>
</div>
  • Related