skip to Main Content

I have one file dbConfig.php which contains simple array data.

<?php 
return [
    'host'                  => 'localhost',
    'database_name'         => 'mitesh_practice',
    'database_user_name'    => 'root',
    'database_password'     => 'Kadiya@123'
]
?>

I have another file which is Model.php

So in Model.php I have created one variable $dbConfig;

So in $dbConfig variable I want to store array of dbConfig.php file

$filename = 'path/to/your/array_data.txt';

// Read the contents of the file
$serializedArray = file_get_contents($filename);

// Unserialize the contents to get the array back
$array = unserialize($serializedArray);

print_r($array);

I tried this but I failed because it reads .txt file

2

Answers


  1. If your file returns a variable, you can retrieve it in a simple require: https://www.php.net/manual/en/function.require.php#Hcom126279

    $dbConfig = require 'path/to/dbConfig.php';
    
    Login or Signup to reply.
  2. You Include your file using require_once and then assign using $dbConfig variable of Model.php
    Code:

    <?php    
    $config = require_once 'path/dbConfig.php'; //it will take the array value from the config.php file
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search