Jump to content

Array not printing out


Bizty

Recommended Posts

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

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 ^.-

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>";

        }
}

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;
       }
}
...

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.

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);

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.