简体   繁体   中英

PHP Include once to multiple PHP File

I know this sounds like a dumb question, but i want to know if there is a way to include php file once and use it for the rest of the project?

For example

i have a php file which contain my database connection.

<?php 
$username = "root";
$db_name = "myDB";
$password = ""; ?>

I only want to include it once in my index.php like

<?php 
include 'db_con.php';

echo "Content Goes here";

?>

then use it for the rest of my project. so in my future PHP file when i

echo $db_name;

I can just access the variable like a global one.

Is this even possible? or is there any approach or guide I can use to accomplish this trick? Thank you

yes. you can use include_once('db_con.php') or require_once('db_con.php')

http://php.net/manual/en/function.include-once.php

If you have access to the configuration, you can transparently add a file for all php requests, with the auto_prepend_file directive.

Here is an example with an apache .htaccess:

# Prepend the file
php_value auto_prepend_file "/path/to/utilities.php"

This will be similar to adding require '/path/to/utilities.php'; at the start of each main file.

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