View Full Version : Vars

07-13-2012, 11:17 PM

im wandering the dis-advantage AND advantages of variables, I see alot of pro scripts use:

$config['site_url'] = 'mysite.com';
$config['site_name'] = 'mysite.com';
$config['site_desc'] = 'mysite.com';

^^^ I know they're array, btw wwhy use that as opposed to:t

07-13-2012, 11:22 PM
IMO Its easier to use collections than individual variables, especially if passing between functions.

function doStuff(array $config)

function doStuff()
$argv = func_get_args();

The other pro is that you can reference the array while you cannot late bind a reference on a variable parameter list.

In any case, it doesn't matter if you use variable, arrays or constants to store data. Its all a matter of how you use it.

07-13-2012, 11:27 PM
Because site_url is something that needs to stay fixed throughout the session, so they choose to represent it as a constant and not a variable, $site_url. After all site_url is used to identify something and needs to never change it's meaning.

Changes: I get what you mean, I thought you meant use $site_url inside the $config brackets. One really good reason is when you use _SESSION you don't have to put everything as a session variable, you can just pass a reference to the $config array instead by using _SESSION.

07-25-2012, 10:12 PM
Ok, lets see if i understand it.

I'v got config.php -
$config['site_address'] = 'http://www.thishosting.org'; // No slash
$config['site_path'] = $_SERVER['DOCUMENT_ROOT'];
$config['site_email'] = 'i293';

$config['upload_destination'] = 'webmaster@domain.com';
$config['thumb_destination'] = 'thumbs';

$config['site_name'] = 'ImgIze Script';
$config['site_desc'] = 'i293';

$config['max_width'] = 1900;
$config['max_height'] = 1900;

I need to use some ^^ in a function:

$aConfig = $_SESSION['DOCUMENT_ROOT'] .'/config.php';

function test(array $aConfig) {
// I can use em now?

07-25-2012, 11:19 PM
Uh... no.

$aConfig is a string, not an array.

If you want to pass $aConfig, you don't need to put array before it, specially since its not an array. Simply function text ($aConfig) { will work, as long as when you call the function, you pass it $aConfig. However, if you're going to use the variable in there, you can also make it global within the function

function text () {
global $aConfig;

Now $aConfig is the same version that is outside the function. However, as mentioned, as these are configs that are not going to change during runtime, set them to constants rather then as variables. Constants are always global.

07-25-2012, 11:39 PM
can u show me a way by constants..............

so to pass it:

then to use:
function func($aConfig) {}

07-25-2012, 11:58 PM
well thats nnot working:

func.php -
function performaLocalUpload($files, $aConfig) {
global $aConfig;
if($ext = checkitsaValidImage($files))
$new_name = gen_uniqueFilename() . $ext; // we will give an unique name.
$uploadFilePath = $aConfig['upload_destination'] .'/'. $new_name;

if (!move_uploaded_file($files, $uploadFilePath)) {
trigger_error('Failed to upload image');

// Create the thumbnail
# create_thumb($customPixels, $save_image, $new_name, $type, $thumbDir);

uploader.php -
t was writtn by Christopher Reynolds. Feb 2011 **************************************/
$aConfig = $_SERVER['DOCUMENT_ROOT'] .'/config.php';

if(isset($_FILES['image_upload']) && !empty($_FILES['image_upload'])) {
// Local upload
$tFiles = $_FILES['image_upload']['tmp_name'];
$aFiles = $_FILES['image_upload'];

for($i = 0; $i < count($tFiles); ++$i)
performaLocalUpload($aFiles['tmp_name'][$i], $aConfig);

07-26-2012, 12:08 AM
so why's Fou-Lu told in post #2, I can do
array $config

07-26-2012, 05:45 AM
I wouldn't recommend global unless you cannot modify the signature.

You can use array $config as a part of the parameter list *if* you provide it with an array. You have not.

$aConfig = $_SESSION['DOCUMENT_ROOT'] .'/config.php';

function test(array $aConfig) {
// I can use em now?

test($aConfig); // with throw an error.

$aConfig is a string, not an array.

07-26-2012, 02:59 PM
so then in the funci can do config's like:


and how would i call the array?

07-26-2012, 03:09 PM
so then in the funci can do config's like:


and how would i call the array?

You mean into a function? You pass it $aConfig.

07-26-2012, 03:16 PM
done all that mate, now getting:

Catchable fatal error: Argument 2 passed to performaLocalUpload() must be an array, string given

07-26-2012, 03:18 PM
Then your provided parameter is not an array.

07-26-2012, 03:32 PM
what you mean?

To call;

fnction(array $aConfig)

07-26-2012, 03:41 PM
I don't understand your question.
If the function parameter list is typecast as an array or object class, then you need to provide it with that type in order to process the function. If it doesn't receive that type, it throws an E_RECOVERABLE_ERROR. This saves a check against is_array or instanceof within the function.

07-26-2012, 03:54 PM
Yea Fou-Lu i know that............

i bet this is simple, cos $aConfig is a string an not array......

Do you no how to mke it an array?A

07-26-2012, 04:18 PM
I'm really confused why arrays are coming into this at all? You seem to be passing strings, strings that should stay constant, not arrays...

If you wanna make it a constant, just throw

define('SITE_ADDRESS', 'http://www.thishosting.org'

Now its consistent wherever you call it, and as your site address doesn't change, it makes sense to keep it constant.

07-26-2012, 06:55 PM
Because ii had used normal vars as you see and it looks messy (my opinion) when trying to pass, say 10 vars to a func.....

I'm not familiar with constants, soo arrays are good (for my config.php).

07-26-2012, 07:02 PM
Constants are easy to use (as I showed above), and not something to avoid. When you have data that is static, and shouldn't change, you should use a constant. Don't avoid a topic just because you're unfamiliar with it; you'll never learn.

07-27-2012, 12:18 AM
Well yea Keleth, i can write them:

define('THEME_DIR', 'content/theme');
define('SITE_ADDRES', 'http://www.');

To us 'em:
__THEME_DIR__ $theme['name']; // folder

Then how would I pass to a func?

07-27-2012, 04:47 AM
Same as any other argument used when calling.
Or just don't. Constants declared with define() are global.

07-27-2012, 04:01 PM
Ok thanks guys now i know more, i'll try use some...

One more thing that's struck me is the use of 2x underscores at beggining an end, eg: call it:
__THEME_DIR__ Why when my constant don't?

07-27-2012, 04:13 PM
Never start your constants, variables or function names with 2x underscores. I have no idea why people have started doing this. PHP has reserved that 2x underscores may be stolen and used by the PHP core at any time.
I don't follow most of the naming guide suggestions in php as I prefer camelcase for all my naming. But they do make it clear that all symbols starting with __ are magical: http://ca.php.net/manual/en/userlandnaming.rules.php
Magic constants also end with 2x underscores.

07-27-2012, 04:24 PM
So from the constants above that i showed, its best to do:

In some code you did for me, i see the 2x __:
$aConfig = parse_ini_file(__DIR__ . '/config/imageconfig.ini');


07-27-2012, 04:55 PM
__DIR__ is a PHP core variable. As Fou just said, PHP has used the 2x underscore before and after to signify its core constants.

07-27-2012, 05:14 PM
So your saying that __DIR__ is global and can be used in any script without defining it?

07-27-2012, 05:16 PM
__DIR__ is defined by PHP so is outside your code, same as all the PHP functions you use. But all constants are global within the script they are defined in.

07-27-2012, 10:15 PM
Ok got a slight problem, in my config file i have:

$aConfig['allowed_mime_types'] = array(
'image/bmp' ); // allowed image types, to add more use: 'image{type}',

Now when testing to see the allowed mime, it's throwing this:

Warning: in_array() expects parameter 2 to be array, null given

Maybe it's an array prob, any help?

07-27-2012, 10:17 PM
Where is your in_array call? It says that the array you have provided it simply does not exist.

07-27-2012, 10:55 PM
Fixed it was a typo lol