Defined variables and arrays vs functions in php
Posted
by
Frank Presencia Fandos
on Stack Overflow
See other posts from Stack Overflow
or by Frank Presencia Fandos
Published on 2012-04-04T23:07:59Z
Indexed on
2012/04/04
23:29 UTC
Read the original article
Hit count: 195
Introduction
I have some sort of values that I might want to access several times each page is loaded. I can take two different approaches for accessing them but I'm not sure which one is 'better'. Three already implemented examples are several options for the Language, URI and displaying text that I describe here:
Language
Right now it is configured in this way: lang()
is a function that returns different values depending on the argument.
Example: lang("full")
returns the current language, "English", while lang()
returns the abbreviation of the current language, "en". There are many more options, like lang("select")
, lang("selectact")
, etc that return different things. The code is too long and irrelevant for the case so if anyone wants it just ask for it.
Url
The $Url array also returns different values depending on the request. The whole array is fully defined in the beginning of the page and used to get shorter but accurate links of the current page.
Example: $Url['full']
would return "http://mypage.org/path/to/file.php?page=1" and $Url['file']
would return "file.php". It's useful for action="" within the forms and many other things. There are more values for $Url['folder']
, $Url['file']
, etc. Same thing about the code, if wanted, just request it.
Text
[You can skip this section]
There's another array called $Text that is defined in the same way than $Url. The whole array is defined at the beginning, making a mysql call and defining all $Text[$i]
for current page with a while loop. I'm not sure if this is more efficient than multiple calls for a single mysql cell.
Example: $Text['54']
returns "This is just a test array!" which this could perfectly be implemented with a function like text(54)
.
Question
With the 3 examples you can see that I use different methods to do almost the same function (no pun intended), but I'm not sure which one should become the standard one for my code. I could create a function called url()
and other called text()
to output what I want. I think that working with functions in those cases is better, but I'm not sure why. So I'd really appreciate your opinions and advice.
Should I mix arrays and functions in the way I described or should I just use funcions?
Please, base your answer in this:
- The source needs to be readable and reusable by other developers
- Resource consumption (processing, time and memory).
- The shorter the code the better.
- The more you explain the reasons the better.
Thank you
PS, now I know the differences between $Url and $Uri.
© Stack Overflow or respective owner