Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

Why does this PHP code just echo "Array"?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...