简体   繁体   中英

PHP Include file but only first few lines

I am using PHP Include:

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

i want to only include the first few lines of file1.php - is this possible?

If you really want to include (run as PHP), then just pull those lines out into a new file:

new.php :

<?php

// line 1
// line 2

And include it in both files:

existing.php and other.php :

<?php
include('new.php');

...
<?php
  $return_from_inc = include('file1.php');
?>

file1.php

<?php
  if ($x === 1) { return 'A'; }
  else { return 'B'; }
  //... return ("break") running script wherever you want to
?>

Depending on the content of those first lines, why don't you use PHP Functions?

file1.php

<?php
    function what_i_want_to_include(){

      //"First lines" content

    }
}

existing.php

<?php
include('file1.php');

what_i_want_to_include();

?>

Using functions it's the simplest way to do it.

You can simply use return on the line of your choice and control will be sent back to the calling file.

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return will also end the execution of an eval() statement or script file.

If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call . If return is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.

Source: PHP Manual

There are a few options to achieve this, but let me stress out that if this is necessary for your application to work you should really consider reviewing the app design.

If you want it programatically you can either grab the first x lines and use eval() to parse them. Example:

$file_location = '/path/to/file.php';
$number_of_lines = 5; //

$file_array = file($file_location);
if(!$file) {
    return false; // file could not be read for some reason
}
$first_lines = array_slice($file_array, 0, $number_of_lines);
$to_be_evaluated = implode('', $first_lines);
eval($to_be_evaluated);

But you should take not that eval expects a string without the php opening tag ( <?php ), at least, not at the start. So you should search for it and delete it in the first line (if present):

if(strpos($first_lines[0], '<?php') !== false) {
    $first_lines[0] = substr(strpos($first_lines[0], '<?php') + 5);
}

Another, and better option, and as suggested above, just pull out the required lines, save them to another file, and include them in both. You could also do this programatically, you could even extract the needed lines and save them to a temporary file on the fly.

Edit it is a 'weird' question, in the sense that it should not be necessary. Could you explain what exactly you are trying to do? Most probably we can come up with a nice alternative.

Edit

As I understand it correctly you have in the file-to-be-included a lot of stuff, but only the database settings are needed. In that case, put them elsewhere! Example:

settings.php

$connection = new mysqli($host, $user, $pass, $db);
if($connection->connect_error) {
     die('This failed...');
}

header.php

<?php require_once('settings.php'); ?>
<html>
    <head>
        <title>My awesome website</title>
        ... other stuff
    </head>

other_file.php

<?php
require_once('settings.php');
$r = $connection->query('SELECT * FROM `my_table` WHERE `random_field`=`random_value`');

etc. etc.

In settings.php you could also put everything in functions to ensure pieces are only executed when needed. You could in example create a get_connection() function, which checks if a database connection exists, otherwise creates it and returns it for usage.

No need for fancy eval() functions at all!

Please bear in mind that it isn't a crime to divide your application in a thousand files. It really isn't!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM