Pregunta

I'm beginner in php and I have a config.php file like this:

<?php   

 $config['dbUser'] = 'usertest';
 $conifg['dbPassword'] = 'passtest';

I need to load this variables in another php, I try with 'include_once' and don't work please could someone help me?

¿Fue útil?

Solución

Do you know that you could return values from included files? So you could do this:

<?php   

 $config['dbUser'] = 'usertest';
 $config['dbPassword'] = 'passtest'; 
 //   ^typo
 return $config;

And usage is

 $config = include("config.php");

Otros consejos

A simple include should work.

<?php
include("config.php");
echo $config['dbUser'];
?>

Use:

<?php 
include("config.php");

at the top of the PHP file you want to access it in. You will then be able to access the config variables form that PHP file it is declared in. Handy for storing database connection string and other configuration.

You have also spelled config wrong at:

$conifg['dbPassword'] = 'passtest';

try require instead of include. this will give an error if the config file is not found (ie wrong path).

require("config.php");

also, config entry for password is misspelled - $conifg.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top