开发者

PHP Getting a variable by name and concatention

开发者 https://www.devze.com 2023-04-09 08:12 出处:网络
I am having this problem getting a variable by its name. What I am doing is including a path with connections and I want the connections are named in a certain way. I want to iterate through those con

I am having this problem getting a variable by its name. What I am doing is including a path with connections and I want the connections are named in a certain way. I want to iterate through those conenctions and add them to a queue but the problem is, I cannot use '.' to concat a variable.

Code looks like this. Config File:

//Set the host the databse will connect too
    $config_db_hostname='xxx.xxx.xxx';
    //Set the user who is connecting to the database
    $config_db_user='a user';
    //Set the password for user who is connecting to the database
    $config_db_pass='abc123';
    //Set the database type
    $config_db_type='mysql';
    //Set the name of the database
    $config_db_name='phonephare_development';
    //Optional: Set the schema, if any
    $config_db_schema='';
    //Optional: Set the port, otherwise default port will be used
    $config_db_port='';
    //Set the prefix for the table names
    $config_db_prefix='pf_';

    //Set the host the databse will connect too
    $config_db_hostname_1='localhost';
    //Set the user who is connecting to the database
    $config_db_user_1='test';
    //Set the password for user who is connecting to the database
    $config_db_pass_1='test';
    //Set the database type
    $config_db_type_1='mysql';
    //Set the name of the database
    $config_db_name_1='test_development';
    //Optional: Set the schema, if any
    $config_db_schema_1='';
    //Optional: Set the port, otherwise default port will be used
    $config_db_port_1='';
    //Set the prefix for the table names
    $config_db_prefix_1='pv_';

Function for getting values:

public static function init(){
        if(file_exists(PV_DB_CONFIG)){
            include(PV_DB_CONFIG);


            for ($i = 0; $i <= 3; $i++) {
                if($i){
                    $profile_id='_'.$i;
                } else {
                    $profile_id='';
                }
         开发者_开发技巧       // Database variables
                self::$connections['connection'.$profile_id]['dbhost'] = ($config_db_hostname.$profile_id);
                self::$connections['connection'.$profile_id]['dbuser'] = $config_db_user.$profile_id;
                self::$connections['connection'.$profile_id]['dbpass'] = $config_db_pass.$profile_id;
                self::$connections['connection'.$profile_id]['dbtype'] = $config_db_type.$profile_id;
                self::$connections['connection'.$profile_id]['dbname'] = $config_db_name.$profile_id;
                self::$connections['connection'.$profile_id]['dbport'] = $config_db_port.$profile_id;
                self::$connections['connection_'.$profile_id]['dbschema'] = $config_db_schema.$profile_id;
                self::$connections['connection_'.$profile_id]['dbprefix'] = $config_db_prefix.$profile_id;
            }

        }

}//end init

So how can I dynamically read this variables?


You can read 'dynamic' variables like this:

$varname = 'config_db_user';
if ($i)
  $varname .= '_' . $i;

$varvalue = $$varname; // $varvalue contains the value now.

But this will make your code really hard to read and hard to maintain. Rather make your config like this:

$profiles[1]['config_db_user'] = 'whatever';

Then, you can just read $profiles[$profile_id]['configfield] to get what you want. There are probably other ways that may even be better, but stop putting together dynamic variable names, because that's the worst of all.


Check PHP variable variables.

Note: that's a bad config file format to start with, you really should put the separate connections to different arrays. Check the config file of phpmyadmin.


Construct your data in a different way. You want to iterate trough group of variables that share a similar name.. yet, it didn't occur to you to use an array for config options and iterate trough that?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号