3

Here is my code:

if(isset($_POST['check']) AND $_POST['check'] == 'First') {
  $errormessage = array();

  if(empty($_POST['full_name']) || strlen($_POST['full_name']) < 4) { 
     $errormessage[] = "FEL - Vänligen ange fullständiga namn. Please enter atleast 3 or more characters for your name";
  }
  if(!isEmail($_POST['usr_email'])) {
     $errormessage[] = "FEL - Invalid email address.";
  }
  if(empty($errormessage)) {
     echo 1;
  } else { 
     echo $errormessage; // <--
  }
}

When echo $errormessage runs it outputs Array. What am I doing wrong?

0

6 Answers 6

29

You are calling echo on an actual array, which does not have an implicit string representation.

In order to output an array's contents you can use the print_r, var_dump or var_export functions or for custom output, you can use array_map or even a foreach loop:

print_r($errormessage);
var_dump($errormessage);
var_export($errormessage);

foreach($errormessage as $error) 
   echo $error . '<br/>';

array_map('echo', $errormessage);
Sign up to request clarification or add additional context in comments.

2 Comments

Or var_dump or if you want to have the array code var_export.
Uuuum, no. echo cannot be used as a callback -- it isn't a function. 3v4l.org/oT6s0
7

$errormessage is an array and using echo on an array prints just Array.

If you want to print your error messages in a decent way, you can either use foreach to iterate the messages and print each message:

echo '<ul>';
foreach ($errormessage as $message) {
    echo '<li>'.htmlspecialchars($message).'</li>';
}
echo '</ul>';

Or you can even use some advanced array processing like array_map and implode to do something like this that is equivalent to the previously shown when the array contains at least one item:

echo '<ul><li>' . implode('</li><li>', array_map('htmlspecialchars', $errormessage)) . '</li></ul>';

Comments

0

You need to pretty-print the array. How you do this is up to you.

If you're passing the array to some JavaScript, you probably want to encode it as a JSON array:

echo json_encode($errormessage);

Comments

0
$errormessage = array();
$errormessage[] = "...";

Both defines $errormessage as array datatype. Echo prints data from string or numeric format. To print data from array either use print_r as suggested or loop through the members of array and use echo

Comments

0

To see what's inside you variable just do

   print_r( $errormessage );
   // or
   var_dump( $errormessage );

Comments

-1

use the code as like this

if(isset($_POST['check']) AND $_POST['check'] == 'First') {
$errormessage = array();
 if(empty($_POST['full_name']) || strlen($_POST['full_name']) < 4)
 {
 $errormessage['error_what_ever_key_you_want'] = "FEL - Vänligen ange fullständiga namn. Please enter atleast 3 or more   characters for your name";
 }
 if(!isEmail($_POST['usr_email'])) {
 $errormessage['error_what_ever_key_you_want'] = "FEL - Invalid email address.";
 }
if(!empty($errormessage)){
echo $errormessage['error_what_ever_key_you_want']; // <--
}
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.