Best way to pass variables - Post ID...

User 10077 Photo


Senior Advisor
1,096 posts

I'm wondering if this is the best solution. I'd like your opinion. One clarification: This is not about passing variables via cookies or in URLs. It is about passing variables to functions in PHP.

I have a project where several sites pull their content from the same database. It allows me to govern the site content for all the sites in one place no matter where I am.

The database has a list of the sites and the settings for each site. Each site has several templates unique for that site but all the sites share the same set of variables (code, name, description, search box settings, which ads to show, etc.). While the values may be the same, the variable set is the same for all the sites. There are probably around 40 variables and a large number of them are used on most pages of each site.

When a user visits a page, the code queries the database for the correct information to display on the page. It first gets the site settings, then it gets the settings for the page (including which template is used for that page), and then it runs a series of functions to retrieve the data that is displayed in the template.

Because the site settings are used repeatedly, I created one array that holds all the variables.

$arMyVars['var1name'] = 'sitecode';
$arMyVars['var2name'] = 'ad type to show';
$arMyVars['var3name'] = 'header picture';
$arMyVars['var4name'] = 'template to use';
... and so on.

For the functions, I simply pass the one array and any of the variables that are needed are available to the function.

Is there a better or more efficient way of doing this?
ASK ME ANYTHING
I provide personalized help for Coffeecup Users including personal or group training for Site Designer, Web Form Builder and more via Zoom.
Email me at support@uscni.org or call 865-687-7698.

Did you know that Web Form Builder can be used for both simple and complicated forms and that it's not limited to the default fonts and buttons? Take a look at a form we developed for WindowTinting.com.
https://forms.windowtinting.com/forms/w … ppingcart/
User 187934 Photo


Senior Advisor
20,196 posts

Is all this data in one table?
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com
User 10077 Photo


Senior Advisor
1,096 posts

The site variables are stored in three tables: sites, sitevariables, pagevariables.

I'm just wondering if passing a large number of variables via one array to the site functions is better or worse than passing individual variables to site functions. For example:

// Example 1
function func_name($var1, $var2, $var3, $var4, $var5){
// stuff to do
}

// Example 2
function func_num2($variablesarray){
// stuff to do
}


In Example 1, I have to spell out every variable that the function needs.

In Example 2, I pass one array that contains all the variables. It allows me to add variables as needed just by adding them to the array. However, with one array that contains ALL the variables for the site, it's a big array. Not all the variables are used by every function, but I never have to keep track of which of them each function needs because they are all in the array.

Example 2 is easier to for programming, but I'm wondering which is better for site performance.
ASK ME ANYTHING
I provide personalized help for Coffeecup Users including personal or group training for Site Designer, Web Form Builder and more via Zoom.
Email me at support@uscni.org or call 865-687-7698.

Did you know that Web Form Builder can be used for both simple and complicated forms and that it's not limited to the default fonts and buttons? Take a look at a form we developed for WindowTinting.com.
https://forms.windowtinting.com/forms/w … ppingcart/
User 187934 Photo


Senior Advisor
20,196 posts

You may see a slight performance gain by accessing individual variables. I would worry about it and do what ever is easiest for coding and readability.:)
I can't hear what I'm looking at.
It's easy to overlook something you're not looking for.

This is a site I built for my work.(RSD)
http://esmansgreenhouse.com
This is a site I built for use in my job.(HTML Editor)
https://pestlogbook.com
This is my personal site used for testing and as an easy way to share photos.(RLM imported to RSD)
https://ericrohloff.com

Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.