Created
July 15, 2012 22:05
-
-
Save joepie91/3118866 to your computer and use it in GitHub Desktop.
Testing variable assignments within if statements in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function return_false() | |
{ | |
return false; | |
} | |
function return_true() | |
{ | |
return true; | |
} | |
function return_null() | |
{ | |
return null; | |
} | |
function return_emptystring() | |
{ | |
return ""; | |
} | |
function return_string() | |
{ | |
return "ohai"; | |
} | |
function return_zero() | |
{ | |
return 0; | |
} | |
function return_one() | |
{ | |
return 1; | |
} | |
function return_emptyarray() | |
{ | |
return array(); | |
} | |
function return_array() | |
{ | |
return array(1); | |
} | |
if($var = return_false()) | |
{ | |
echo("Testcase 1 (boolean false) results in TRUE\r\n"); | |
} | |
else | |
{ | |
echo("Testcase 1 (boolean false) results in FALSE\r\n"); | |
} | |
if($var = return_true()) | |
{ | |
echo("Testcase 2 (boolean true) results in TRUE\r\n"); | |
} | |
else | |
{ | |
echo("Testcase 2 (boolean true) results in FALSE\r\n"); | |
} | |
if($var = return_null()) | |
{ | |
echo("Testcase 3 (null) results in TRUE\r\n"); | |
} | |
else | |
{ | |
echo("Testcase 3 (null) results in FALSE\r\n"); | |
} | |
if($var = return_emptystring()) | |
{ | |
echo("Testcase 4 (empty string) results in TRUE\r\n"); | |
} | |
else | |
{ | |
echo("Testcase 4 (empty string) results in FALSE\r\n"); | |
} | |
if($var = return_string()) | |
{ | |
echo("Testcase 5 (filled string) results in TRUE\r\n"); | |
} | |
else | |
{ | |
echo("Testcase 5 (filled string) results in FALSE\r\n"); | |
} | |
if($var = return_zero()) | |
{ | |
echo("Testcase 6 (integer 0) results in TRUE\r\n"); | |
} | |
else | |
{ | |
echo("Testcase 6 (integer 0) results in FALSE\r\n"); | |
} | |
if($var = return_one()) | |
{ | |
echo("Testcase 7 (integer 1) results in TRUE\r\n"); | |
} | |
else | |
{ | |
echo("Testcase 7 (integer 1) results in FALSE\r\n"); | |
} | |
if($var = return_emptyarray()) | |
{ | |
echo("Testcase 8 (empty array) results in TRUE\r\n"); | |
} | |
else | |
{ | |
echo("Testcase 8 (empty array) results in FALSE\r\n"); | |
} | |
if($var = return_array()) | |
{ | |
echo("Testcase 9 (filled array) results in TRUE\r\n"); | |
} | |
else | |
{ | |
echo("Testcase 9 (filled array) results in FALSE\r\n"); | |
} | |
/* | |
Actual results: | |
Testcase 1 (boolean false) results in FALSE | |
Testcase 2 (boolean true) results in TRUE | |
Testcase 3 (null) results in FALSE | |
Testcase 4 (empty string) results in FALSE | |
Testcase 5 (filled string) results in TRUE | |
Testcase 6 (integer 0) results in FALSE | |
Testcase 7 (integer 1) results in TRUE | |
Testcase 8 (empty array) results in FALSE | |
Testcase 9 (filled array) results in TRUE | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment