Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

die() or exit() without destructors?

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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…

>Solution :

die and exit are php commands that cause the code to cease execution imediately

<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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading