Short circuit evaluation sometimes casts to boolean

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the dumb-mistakes-and-gotchas category.

Last Updated: 2024-12-03

I had the following code in PHP:

<?php

// Goal: Print "checked" if "english" is in the $languages array, 
// otherwise do not print.
in_array("english", $languages) && "checked";

However this printed true instead of "checked", as I would expect in Ruby/JavaScript/Python. This is because PHP casts the result here to true.

This is different to what happens in Ruby/JavaScript/Python (where "checked" would be returned).