Skip to content

Instantly share code, notes, and snippets.

@davidyell
Last active February 25, 2016 10:08
Show Gist options
  • Save davidyell/6630267 to your computer and use it in GitHub Desktop.
Save davidyell/6630267 to your computer and use it in GitHub Desktop.
The outcomes of testing things using just if() is.
<?php
/**
* As I never am quite sure the outcome of testing variables and arrays using
* just the plain if(), I thought I'd make a quick test so that I can refer to
* it when I next forget this!
*
* Manual ref: http://uk3.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
*/
$emptyArray = array(); // False
$filledArray = array('One','Two'); // True
$boolTrue = true; // True
$boolFalse = false; // False
$boolNull = null; // False
$emptyString = ''; // False
$filledString = 'A string'; // True
if ($emptyArray) {
var_dump('If thinks emptyArray is true');
}
if ($filledArray) {
var_dump('If thinks filledArray is true');
}
if ($boolTrue) {
var_dump('If thinks boolTrue is true');
}
if ($boolFalse) {
var_dump('If thinks boolFalse is true');
}
if ($boolNull) {
var_dump('If thinks boolNull is true');
}
if ($emptyString) {
var_dump('If thinks emptyString is true');
}
if ($filledString) {
var_dump('If thinks filledString is true');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment