Bizty Posted August 20, 2009 Share Posted August 20, 2009 I've setup a file called errors.php where I've set the errors up in an array: $error = array(); $error[0] = "Some error"; $error[1] = "Error 1"; now if I say print $error[0]; in that file it works fine. But the thing is I'm including the error.php into another file: <? functions.php: include('errors.php'); function WriteError($e_id) { if(empty($e_id)) { print "Error: empty"; } else { print "Error: ". $error[$e_id] ."; } } ?> then it simply only writes 'Error:' - Any ideas? Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/ Share on other sites More sharing options...
Cosizzle Posted August 20, 2009 Share Posted August 20, 2009 How is this function being called? perhaps you have 2 errors and it can't loop right What I would do is check the array size of your error if its empty do whatever if it contains errors loop through them Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/#findComment-902941 Share on other sites More sharing options...
Bizty Posted August 20, 2009 Author Share Posted August 20, 2009 Right now it's merely just being called if there's a $_GET value. //URL: somesite.php?e=1 if($_GET['e']) { WriteError($_GET['e']); } I've already checked if the error id gets into the script and if I write it like this: function WriteError($e_id) { if(empty($e_id)) { print "Error: empty"; } else { print "Error: ". $error[$e_id] ."; } print $e_id; // writes the number } As for now this only handles 1 error I'm aware of that, but first I need it to write anything at all ^.- Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/#findComment-902948 Share on other sites More sharing options...
Cosizzle Posted August 20, 2009 Share Posted August 20, 2009 is it because you're not closing the last " print "Error: ". $error[$e_id] ."; try print "Error: ". $error[$e_id]; Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/#findComment-902950 Share on other sites More sharing options...
Bizty Posted August 21, 2009 Author Share Posted August 21, 2009 Not the issue I'm afraid, as the one I posted here was a quick rewrite, I'll just give you the actual function as it is in my file, where it actually is closed =( function WriteError($error_id) { if(empty($error_id)) { print "Error: no error id was supplied"; die; } else { print "<p style='font: verdana;font-size:10px;font-weight:bold;color:red;'>Error: ". $error[$error_id] ."</p>"; } } Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/#findComment-903240 Share on other sites More sharing options...
Malevolence Posted August 21, 2009 Share Posted August 21, 2009 maybe try $error['$error_id'] instead of $error[$error_id]. - if you have done this: WriteError("1"); then 1 is a varchar rather than a number. You have to treat it like text now. If you do WriteError(1); it will be set as a number and your current code should work, however you could just change the function to accept a varchar (hence the $error['$error_id'] with the inverted commas) Also, it may be better practise to return a variable with that print statement and then echo the function. i.e. echo WriteError("5"); ... } else { $errMsg = "<p style='font: verdana;font-size:10px;font-weight:bold;color:red;'>Error: ". $error[$error_id] ."</p>"; return $errMsg; } } ... Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/#findComment-903246 Share on other sites More sharing options...
Mark Baker Posted August 21, 2009 Share Posted August 21, 2009 The $error array isn't in scope for the function Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/#findComment-903247 Share on other sites More sharing options...
Bizty Posted August 21, 2009 Author Share Posted August 21, 2009 Malevolence: didn't work encasing it in quotes. Why is it a better practice (thanks for the tip, just want to know the explanation ) Mark Baker: I'm sorry I don't follow there - how do I include the array then? Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/#findComment-903250 Share on other sites More sharing options...
Malevolence Posted August 21, 2009 Share Posted August 21, 2009 I'm not sure if printing within a function works. If it does, you get an instant result, whereas you have far more to play with if its returned. ... } else { $errMsg = "<p style='font: verdana;font-size:10px;font-weight:bold;color:red;'>Error: ". $error[$error_id] ."</p>"; return $errMsg; } } ... $err = WriteError(2); print $err; but you could also pass the error on in a GET variable or session. Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/#findComment-903254 Share on other sites More sharing options...
Mark Baker Posted August 21, 2009 Share Posted August 21, 2009 Mark Baker: I'm sorry I don't follow there - how do I include the array then? function WriteError($e_id) { global $error; if(empty($e_id)) { print "Error: empty"; } else { print "Error: ". $error[$e_id]; } } function WriteError(1); OR function WriteError($e_id) { include('errors.php'); if(empty($e_id)) { print "Error: empty"; } else { print "Error: ". $error[$e_id]; } } function WriteError(1); OR function WriteError($errors,$e_id) { if(empty($e_id)) { print "Error: empty"; } else { print "Error: ". $error[$e_id]; } } function WriteError($errors,1); Link to comment https://forums.phpfreaks.com/topic/171230-array-not-printing-out/#findComment-903261 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.