Clarification on how PHP handles true and false values

I would like to use a true/false result to do alternative things in PHP. I was surprised by the following:

$falsevar = false;
$truevar = true;
echo "falsevar which I just set to false is".$falsevar; //displays true
echo "truevar which I just set to true is".$truevar; //displays 1

Output:

falsevar which I just set to false istruevar which I just set to true is1

What should I make of this result? Why would PHP echo out false as true?

Further confusing me, my use case is the following:

$prompt = "I would like a sandwich to eat";
$myarray = array("pizza", "hamburger", "hot dog");
$wantsItem = false;
foreach ($myarray as $needle) {
    if (strpos($prompt, $needle) != false) {
        echo "Match found: {$needle}n";
$wantsItem = true;
    }
}
echo "wants item is".$wantsItem;

When I run this this which I believe should return false, nothing echoes at all.

falsevar which I just set to false istruevar which I just set to true is1value of wants item

Can someone explain to me the behaviour of true and false in PHP? (Note, I understand that true and false are case insensitive so that should not be the issue.)

Thanks for any suggestions.

2

What you are seeing is the result of “type juggling”, where you have a variable which is a boolean, and you’re using it somewhere you need a string.

The rules for that are described in the “Strings” section of the manual:

A bool true value is converted to the string "1". bool false is converted to "" (the empty string). This allows conversion back and forth between bool and string values.

Your first example is a bit confusing, because you haven’t offset the value with a newline or other separator, and your strings begin “falsevar” and “truevar”, making it really hard to read the output. Here’s a clearer version:

$falsevar = false;
$truevar = true;
echo "Variable set to false is [" . $falsevar . "]n";
echo "Variable set to true is [" . $truevar. "]n";

As described in the manual, this results in:

Variable set to false is []
Variable set to true is [1]

For debugging, it’s generally more useful to use a function like var_dump, which gives you a description of a value. In your second example, you could write this:

$prompt = "I would like a sandwich to eat";
$myarray = array("pizza", "hamburger", "hot dog");
$wantsItem = false;
foreach ($myarray as $needle) {
    if (strpos($prompt, $needle) != false) {
        echo "Match found: {$needle}n";
        $wantsItem = true;
    }
}
var_dump($wantsItem);

That gives the output:

bool(false)

Aside, as Robert Mihai Ionas pointed out, you have a subtle bug in your code, unrelated to the question: strpos returns 0 for “matches at the start”, and false for “does not match”, and this causes a different type-juggling problem: both are “false-y values”. So as the strpos manual page notes, you should use strpos($prompt, $needle) !== false instead of strpos($prompt, $needle) != false.

Alternatively, you can use the recently-added str_contains function instead, which doesn’t have this fiddly problem.

2

Why would PHP echo out false as true?

It doesn’t. false is echoed as an empty string. truevar in your output is the beginning of the second string that you echo, not the value of $falsevar.

it would have been clearer if you put the outputs on separate lines.

echo "falsevar which I just set to false is".$falsevar."<br>";
echo "truevar which I just set to true is".$truevar."<br>";

Then the output would have been:

falsevar which I just set to false is
truevar which I just set to true is1

Use !== false because 0 is equal with false

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật