How to include files with die(); function?
Asked Answered
E

7

9

file1.php and file2.php with die(); function.

include.php:

<? include 'file1.php';
include 'file2.php' ?>

file1.php

<? echo 'included'; die(); ?>

file2.php

<? echo 'not included'; die(); ?>

How can I include both files with die(); function?

Elizbeth answered 24/11, 2009 at 16:59 Comment(1)
What is the purpose of die() at the end of file1.php and file2.php? If you want to stop executing the included file you can use return instead.Subbasement
S
9

If I understand correctly what you are trying to do then unfortunately it isn't possible.

die(); will stop the script from executing at the point from where it is called.

Symploce answered 24/11, 2009 at 17:2 Comment(0)
D
10

If you would like to test whether the include line happened successfully, you can test the return value of the include function itself:

// https://www.php.net/manual/en/function.include.php Example #4
if ((include 'file1.php') != 'OK') {
    die();
}

You may also consider require() instead of include() depending on your needs:

require() is identical to include() except upon failure it will also produce a fatal E_ERROR level error. In other words, it will halt the script whereas include() only emits a warning (E_WARNING) which allows the script to continue.

Dolliedolloff answered 24/11, 2009 at 17:4 Comment(0)
S
9

If I understand correctly what you are trying to do then unfortunately it isn't possible.

die(); will stop the script from executing at the point from where it is called.

Symploce answered 24/11, 2009 at 17:2 Comment(0)
C
2
if (!condition){
   include_once './inc/header.inc.php';
   echo "Errormessage";
   include_once './inc/footer.inc.php';
   die();
}

I hope this is what you wanted.

Carline answered 10/3, 2012 at 18:9 Comment(0)
T
2

Here is how to include a file, or die with a message if the include fails code sample.

(include 'file.php') || die('Failed to include file!');
Turgescent answered 12/10, 2014 at 7:53 Comment(1)
Why not just use require?Integration
J
1

If the execution of your included file is not dependent on the current file (no shared variables, functions, etc.), then use

file_get_contents('link_to_file.php');

instead of an include method. When you force the file to execute independently it will not make a effect in the script execution.

Jett answered 5/9, 2010 at 15:43 Comment(0)
P
1

Just a minor improvement to LorenzoP's method:


(@include "file.php") or die("Failed to include!");
// notice the @ sign!

This way, you save yourself 2 ugly lines of php warning when inclusion fails. Otherwise I think this is truly the best way to handle failed includes. (Also, his answer should be the accepted one.)

Pogey answered 28/10, 2020 at 22:34 Comment(0)
S
0

die() is just an exit with an error, you can't include files with it and I don't really understand why you want to. Could you provide more details as to what you're trying to accomplish?

Stillborn answered 24/11, 2009 at 17:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.