2

I'm trying to check value's of keys :

$errorArray=array();
 $naamarray["naam"]=false;
 $naamarray["voornaam"]=false;
 $naamarray["adres"]=false;
 $naamarray["woonplaats"]=false;
 $naamarray["postcode"]=false;
 $naamarray["telefoonnummer"]=false;
 $naamarray["geboortedatum"]=false;
 $naamarray["adres"]=false;
 $naamarray["wachtwoord"]=false;
 $naamarray["email"]=false;
 $naamarray["email"]=true;

  foreach($naamarray as $key => $value){
        if($value == false){
            array_push($errorArray,$key);
            echo $key;
            echo $value;
        }
 }

But the value never get's shown, what is my mistake ?

1
  • try echoing the key and value before the if statment to see what you get Commented Jul 13, 2011 at 18:47

6 Answers 6

4

The false is boolean type in php. Since you have assigned that to your array values, you need to use var_dump to see actual value for your keys :

var_dump($key);

See the var_dump manual for more info


You may want to assign string values to your array values instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Why strings? From what I can tell of the context making them strings wouldn't benefit anything, as you're not going to just show 'false' or 'true' to the user for a form validation. Using a boolean is appropriate, as it takes less space and correctly evaluates as true or false in comparisons ($boolean) or (!$boolean). Using strings means you have to also worry about case-sensitivity, and then we start going down the True, False, FileNotFound enumeration slope. thedailywtf.com/Articles/What_Is_Truth_0x3f_.aspx
@Zimzat: I am not sure about OP's requirements. If his requirement is like what you say then off course using boolean is the way to go. If he just wants to check what goes inside the keys, he can simply use string values. Otherwise, var_dump is needed to see false and true values.
You can easily see a true value as it casts to the string of 1. The false value is the only difficult part. Alternatively, var_export can also be used to see boolean values in their native form. For the OP, I'd recommend simply var_dump the entire errorArray after done, rather than during.
1

Please note: echo false does not echo anything.

Comments

1

because they are always false, it means NOTHING

echo false;

gives you

Comments

0

Try this:

$errorArray=array();
 $naamarray["naam"]='false';
 $naamarray["voornaam"]='false';
 $naamarray["adres"]='false';
 $naamarray["woonplaats"]='false';
 $naamarray["postcode"]='false';
 $naamarray["telefoonnummer"]='false';
 $naamarray["geboortedatum"]='false';
 $naamarray["adres"]='false';
 $naamarray["wachtwoord"]='false';
 $naamarray["email"]='false';
 $naamarray["email"]='true';

  foreach($naamarray as $key => $value){
        if($value == 'false'){
            array_push($errorArray,$key);
            echo $key;
            echo $value;
        }
 }

3 Comments

Why the downvote? Sure I wouldn't recommend anybody use strings as bools but var_dump isn't exactly UI friendly, maybe he wants to display it in a nice format, and this will allow him to do that.
Rather than change the internal value to a string (binding yourself to a specific value, making changing or checking it elsewhere that much more difficult), it would be easier to change the value at the time of output, like this: echo ($value) ? 'true' : 'false';
Alternatively: var_export($value); Still really only good for debug or system check pages. Not user friendly for form validation output.
0

You cannot echo a boolean variable in PHP. If you just want to debug - use

var_dump($value); 

instead.

1 Comment

Sure you can. Echoing a boolean value simply casts it to string first. A true value casts to the string '1' and a false value casts to the string '' (blank).
0

Funny, the keys do echo on my home server. But then again; echo'ing bool false will not output anything.

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.