Arrays and Config

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
Mile
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Oct 28, 2020 7:35 am

Wed Oct 28, 2020 8:01 am

As a way of (re)learning PHP and web design (haven't looked at some of this stuff for almost 15 years, and the world has moved on!) I thought I'd get a little project on the go, and build something from scratch, to up my knowledge.

I used this little tutorial (admittedly it's several years old, but I have no reason to believe it doesn't work still), as the framework provided definitely works, and I quite liked the abstraction.

However, what I'm having trouble with is referencing the $config arrays from another page.

Code: Select all

$config = array(
    "db" => array(
        "db1" => array(
            "dbname" => "myDatabase",
            "username" => "dbUser",
            "password" => "myPassword",
            "host" => "localhost"
        ),
    ),
    "urls" => array(
        "baseUrl" => "http://example.com"
    ),
    "emails => array(
        "hello" => "hello@mywebsite.com",
        "admin" => "admin@mywebsite"
    ),
    "paths" => array(
        "resources" => "/path/to/resources",
        "images" => array(
            "content" => $_SERVER["DOCUMENT_ROOT"] . "/images/content",
            "layout" => $_SERVER["DOCUMENT_ROOT"] . "/images/layout"
        )
    )
);
So, what I'm trying to get to is the email address "hello" in the config.php file's multidimensional array. Had a look at things like array_values() and return $config->emails["hello"], and my error log is long and varied. :(

Solutions/wider issues/things to try?

EDIT: The general error is that the $config variable doesn't exist, but it seems to be getting called via the index.php in the example, with the line require_once(realpath(dirname(__FILE__) . "/../../resources/config.php"));.
simonbrahan
php-forum Fan User
php-forum Fan User
Posts: 90
Joined: Mon Jun 08, 2020 2:00 am
Contact:

Wed Oct 28, 2020 11:23 am

First thing you need to do is check that $config is available wherever you're trying to access it. You can do that using:

Code: Select all

var_dump($config);
If that shows your config array, great. If not, your require_once isn't working as you expect. require_once should cause a fatal error if it can't include the file, but you might be requiring another, similarly named file by accident.

If you do have access to $config, you use PHP's array access syntax to get values from it. For the value you're after you'd use:

Code: Select all

$config['emails']['hello'];
$config is the name of the variable, ['emails'] and ['hello'] is how you describe the key you're looking for at each level of the structure.

If that threw you, it's worth running through some tutorials to get your head back around the basic syntax and structure of the language before trying a large project. Codecademy seems to have a decent one. Some of it will be old hat, but it will be helpful to see what's changed in the language if nothing else - PHP has had a very eventful fifteen years.
Mile
New php-forum User
New php-forum User
Posts: 2
Joined: Wed Oct 28, 2020 7:35 am

Fri Oct 30, 2020 2:21 am

Thank you for replying.

The var_dump() was helpful, and I finally got there.

Ultimately my issue was twofold:

1. Scope. Was looking at the screen too long I missed a function's role in the overall logic; and
2. That the array element needs passed earlier, rather than passing the whole array, with all its dimensions, and trying to read it later.
Post Reply