How to Stop PHP Code Execution

How to stop execution of PHP script when a certain condition is met in IF statement?

You might also want to use try catch syntax, which avoid pyramidal codes of conditionnal statements :


try {
if (empty($firstname)){
throw new Exception("Please enter first name");

if (empty($password)){
throw new Exception("Please enter password");

if (empty($confirm_password)){
throw new Exception("Please confirm password");

// Everything is fine, logic continues here...
catch( Exception $e ) {
$message = $e->getMessage();

die( $message );


At the first throw statement, the program automatically jump to the next catch statement. In this case, you throw a Exception, and as we catch them, the first error will be displayed (instead of accumulating all the errors).

As every folks put die for displaying the message I put it too, but you can return a nice HTML error if you would like.

When you read the code again, exceptions let the developper better understand that these conditionnal failures are really exceptions and that the code should stops. It is less easy to undertsand it when we only set up if else statements. That one of the advantages of exceptions.

But exceptions are far more powerful, they make it easy to know which lines thrown the exception, in which file, you can also provide an error code to make it more reliable, ...

How to stop PHP Script?

If you are on UNIX system do this:

ps aux | grep php

it will list all running processes which are instances of php. If you're script is called myscript.php, then you should see php /path/to/myscript.php in this list when it's running.

Now you can kill it by command kill -9 PID

If you're running on windows you can't (unless you can pull up task manager manually) and if you only have web browser access to the server and you're running the script via the web server again you can't kill the process. Either way running an infinite loop script without the ability to kill it on command is a bad idea.

Reference how detect stop or running a php script from out and background

Stop PHP Execution But Display HTML

You could put your validation logic inside a function at the top of your page, and change all your echo to return.

function validate() {

if (isset($_POST["submitted"])) {
if (!isValidEmail($_POST["email"])) {
return "<p>Please enter a valid email address.</p>";
if (!isValidPhoneNumber($_POST["phoneNumber"])) {
return "<p>Please enter a valid phone number.</p>";


if (updateUser($id, $email, $phoneNumber, $name)) {
return "<meta http-equiv='refresh' content='0'>";
} else {
return "<p>An error occurred! Could not update your profile information!</p>";


Then simply echo the string returned from the function above the footer.

<?php echo validate(); ?>

Note that the above will work because $_POST is a superglobal. However, you may consider changing your function to pass email, phoneNumber, name and id as parameters instead.

How to stop php to execute after getting a false as return in js

Working Fiddle.

Add return in onclick before function call :

<button type="submit" onclick="return phonenumber(" class="quote">Get Quote</button>

Hope this helps.

how to stop the current included execution in PHP?

You can use a return statement in the included file to return to the file that includes it.

From the documentation of include:

It is possible to execute a return statement inside an included file in order to terminate processing in that file and return to the script which called it.

Javascript stop PHP script

Separate front- and backend, send messages between two.

Javascript asks for confirmation and if gets it - sends a request to php script.

<script src=""></script>
<script src=""></script>

<div id="dialog-confirm" title="Do you wanna continue ?">
<p>Are you sure to delete this file ?</p>

<script language="javascript">
$( document ).ready(function() {
resizable: false,
height: "auto",
width: 400,
modal: true,
buttons: {
"Do it": function() {
$.post( "post.php", {filename: "error"})
.done(function( data ) {
alert('Success: '+JSON.stringify(data));
.fail(function(jqXHR, textStatus, errorThrown) {
alert('ERROR: '+JSON.stringify(errorThrown));
Cancel: function() {

php script waits for a filename, deals with file manipulation (e.g. deleting) and sends some feedback.

$fn = $_POST['filename'];
//delete file with name $fn
//send result
if($result=='error') //something went wrong
header('HTTP/1.1 500 Ended up with bad result');
header('Content-Type: application/json');
$response_array['status'] = "error";
$response_array['data'] = $data;
}else{ //everything ok
header('Content-type: application/json');
$response_array['status'] = 'success';
$response_array['data'] = $data;
echo json_encode($response_array);

Related Topics

Leave a reply