Skip to content

Instantly share code, notes, and snippets.

@renepardon
Created November 21, 2018 09:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save renepardon/10bc479d542dfce38cb4a1602df409cf to your computer and use it in GitHub Desktop.
Save renepardon/10bc479d542dfce38cb4a1602df409cf to your computer and use it in GitHub Desktop.
Annoying bool to string cast
function foo(?string $val)
{
echo (string) $val;
}
foo('test')
foo(true)
foo(false)
foo(null)
// Output:
/*
array(2) {
[0]=>
string(6) "string"
[1]=>
string(4) "test"
}
array(2) {
[0]=>
string(6) "string"
[1]=>
string(1) "1"
}
array(2) {
[0]=>
string(6) "string"
[1]=>
string(0) ""
}
array(2) {
[0]=>
string(4) "NULL"
[1]=>
NULL
}
*/
// So true and false are getting casted to string. false will be empty, true will be 1
// SOLUTION: decalare strict types ....
declare(strict_types = 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment