March 9, 2013

Stop code after if statment from excuting when error occurs

Question by shnisaka

This is what my code looks like:

if($allthevaluesarenotempty) {
    if($pictureexist) {
        // upload picture code
    }
    else
    {
        $message = "error";
    }

    // sql query here
}

How can I stop the SQL query from executing when $message is set?

Answer by Starx

  • If in case of loops like while, for and foreach, you can use continue; to escape the iteration and break; to end the loop.

  • In function return false; will stop the execution.

  • In a PHP document, exit() will terminate further execution of the script.

So, use which is suitable.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!