By not declaring an array before using it can really cause problems.
One experience I just found, I called this test script like this: indextest.php?file=1STLSPGTGUS
This works as expected.
//indextest.php?file=1STLSPGTGUS
$path['templates'] = './mytemplates/';
$file['template'] = 'myindex.tpl.php';
$file['otherthing'] = 'otherthing';
$file['iamempty'] = '';
print ("path['templates'] = " . $path['templates'] . "<br>");
print ("file['template'] = " . $file['template'] . "<br>");
print ("file['otherthing'] = " . $file['otherthing'] . "<br>");
print ("file['iamempty'] = " . $file['iamempty'] . "<br>");
print ("file['file'] = " . $file['file'] . "<br>");// should give: "Notice: Undefined index: file"
print ("file = " . $file);// should give: "Notice: Undefined index: file"
//the Output is:
/*
path['templates'] = ./mytemplates/
file['template'] = myindex.tpl.php
file['otherthing'] = otherthing
file['iamempty'] =
Notice: Undefined index: file in D:\Server\Apache24\htdocs\DeliverText\indextest.php on line 14
file['file'] =
Notice: Array to string conversion in D:\Server\Apache24\htdocs\DeliverText\indextest.php on line 15
file = Array
*/
Now I will just require a file, from another script I bought, at the top of mine and we can see how values are completly wrong for the array $file while array $path is OK:
"checkgroup.php" is the guilty one.
//indextest.php?file=1STLSPGTGUS
require_once($_SERVER['DOCUMENT_ROOT']."/IniConfig.php");
$access = "PUBLIC";
require_once(CONFPATH . "include_secure/checkgroup.php");
$path['templates'] = './mytemplates/';
$file['template'] = 'myindex.tpl.php';
$file['otherthing'] = 'otherthing.php';
$file['iamempty'] = '';
print ("path['templates'] = " . $path['templates'] . "<br>");
print ("file['template'] = " . $file['template'] . "<br>");
print ("file['otherthing'] = " . $file['otherthing'] . "<br>");
print ("file['iamempty'] = " . $file['iamempty'] . "<br>");
print ("file['file'] = " . $file['file'] . "<br>");
print ("file = " . $file);
//the Output is:
/*
path['templates'] = ./mytemplates/
file['template'] = o
file['otherthing'] = o
file['iamempty'] = o
file['file'] = o
file = oSTLSPGTGUS
*/
Initialising the array before, then no problem!
//indextest.php?file=1STLSPGTGUS
require_once($_SERVER['DOCUMENT_ROOT']."/IniConfig.php");
$access = "PUBLIC";
require_once(CONFPATH . "include_secure/checkgroup.php");
$path = array();
$file = array();
$path['templates'] = './mytemplates/';
$file['template'] = 'myindex.tpl.php';
$file['otherthing'] = 'otherthing.php';
$file['iamempty'] = '';
print ("path['templates'] = " . $path['templates'] . "<br>");
print ("file['template'] = " . $file['template'] . "<br>");
print ("file['otherthing'] = " . $file['otherthing'] . "<br>");
print ("file['iamempty'] = " . $file['iamempty'] . "<br>");
print ("file['file'] = " . $file['file'] . "<br>");
print ("file = " . $file);
//the Output is:
/*
path['templates'] = ./mytemplates/
file['template'] = myindex.tpl.php
file['otherthing'] = otherthing.php
file['iamempty'] =
file['file'] =
file = Array
*/
That's how I realised how important it is to initialise variables as we never know what problem we might end up with later, and just for wanting to save time we might end up wasting even more at the end.
I hope this will be helpful for those like me who are not professional.
$foo = array()
and that it wasn't a string turned in to an array, etc.).