English 中文(简体)
How to include files with die(); function?
原标题:

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?

最佳回答

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.

问题回答

Non-English Speakers:

You can provide your question in your native language as well, and somebody here may be able to translate it for you. Just make your best effort to ask in English, and add your native tongue below.


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

// http://us3.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.

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! );
if (!condition){
   include_once( ./inc/header.inc.php );
   echo "Errormessage";
   include_once( ./inc/footer.inc.php );
   die();
}

I hope this is what you wanted.

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.)

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.

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?





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签