Without declaring the array before use, it can really cause problems. One experience that I just found, I called this test script as follows: 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 just need a file from another script I bought at the top, and we will see how the values are completely incorrect for the $ file array, while the $ path array is fine: "checkgroup.php" is to blame.
//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 */
Initializing an array earlier, 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 is why I realized how important it is to initialize the variables, since we never know what problem we may encounter later, and just to save time we could end up spending even more. I hope that it will be useful for people like me who are not professional.
Dan Bon Nov 12 '18 at 0:39 2018-11-12 00:39
source share