Atish
Atish

Reputation: 174

How to read PHP file which contains array

I have a php file, config.php and it contains following code.

$_config = array(

    'db' => array(
        'mysolidworks' => array(
        'host'      => 'localhost',
        'username'  => 'netvibes',
        'password'  => 'frakcovsea',
        'dbname'    => 'sw_mysolidworks',
        'driver_options' => array(
            PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
        )
    )
  )
);

I want to read this file into a variable and then read the variable like an array. Could anyone help?

Upvotes: 0

Views: 52

Answers (2)

ksealey
ksealey

Reputation: 1738

you can use any of the following

  <?php
        $file_loc = 'SomeDir/config.php';
        require $file_loc; //Throw a fatal if it's not found
        require_once $file_loc; //If this might be called somewhere else in your script, only include it once
        include $file_loc; //Include but just show a warning if not found
        include_once $file_loc; //Include if not already included and throw warning if not
  ?>

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 78994

Simply using PHP the way it's meant to work:

include('config.php');
echo $_config['db']['mysolidworks']['host'];

Upvotes: 1

Related Questions