0

How can I make this into a boolean?

I tried this :

array('FALSE' => 'No', 'TRUE' => 'Yes')

I want the TRUE/FALSE to be treated as a boolean and not a string. How to do this?

1 Answer 1

1

When you put values in quotes, they are treated as strings. Simply use the true and false boolean keywords, eg

array(
    false => 'No',
    true  => 'Yes'
)

Be mindful that PHP will auto-cast true to 1 and false to 0 in this case because

The (array) key can either be an integer or a string

This won't stop you being able to use $array[true] or $array[false] though.

See http://php.net/manual/language.types.array.php

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

2 Comments

Shouldn't it be otherwise? true and false cannot be array indexes. I think Yes and No are supposed to be those.
@Hanky웃Panky Depends what the OP wants to do. This array could be used as part of a translation / i8n file for yes/no value output

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.