...

View Full Version : Vars



Chris-2k
07-13-2012, 10:17 PM
hi,

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
[code]
$site_url
$site_name
$site_desc
[/code=]

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


function doStuff(array $config)
{
print_r($config);
}

function doStuff()
{
$argv = func_get_args();
print_r($argv);
}


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.

DrDOS
07-13-2012, 10: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.

Chris-2k
07-25-2012, 09: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?
}

Keleth
07-25-2012, 10: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.

Chris-2k
07-25-2012, 10:39 PM
can u show me a way by constants..............

so to pass it:
func($aConfig);

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

Chris-2k
07-25-2012, 10: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);
}
}

Chris-2k
07-25-2012, 11:08 PM
so why's Fou-Lu told in post #2, I can do
array $config

Fou-Lu
07-26-2012, 04: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.

Chris-2k
07-26-2012, 01:59 PM
so then in the funci can do config's like:


$aConfig['xxxxxxxxxxxxx']

and how would i call the array?

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


$aConfig['xxxxxxxxxxxxx']

and how would i call the array?

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

Chris-2k
07-26-2012, 02:16 PM
done all that mate, now getting:


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

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

Chris-2k
07-26-2012, 02:32 PM
what you mean?

To call;
test($aConfig)

then:
fnction(array $aConfig)
{
$aConfg['xxxxxxxx']
}

Fou-Lu
07-26-2012, 02: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.

Chris-2k
07-26-2012, 02: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

Keleth
07-26-2012, 03: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.

Chris-2k
07-26-2012, 05: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).

Keleth
07-26-2012, 06: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.

Chris-2k
07-26-2012, 11:18 PM
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?

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

Chris-2k
07-27-2012, 03: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?

Fou-Lu
07-27-2012, 03: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.

Chris-2k
07-27-2012, 03:24 PM
So from the constants above that i showed, its best to do:
THEME_DIR

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

Why?

Keleth
07-27-2012, 03: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.

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

Keleth
07-27-2012, 04: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.

Chris-2k
07-27-2012, 09:15 PM
Ok got a slight problem, in my config file i have:

$aConfig['allowed_mime_types'] = array(
'image/gif',
'image/jpeg',
'image/png',
'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?

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

Chris-2k
07-27-2012, 09:55 PM
Fixed it was a typo lol



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum