As I mentioned previously javascript has a lightweight standard library. As I became more familiar with PHP I often became frustrated that javascript didn't have an equivalent to PHP's is_* such as is_array, is_object, etc.

Yet the browser is the biggest development environment in history so clearly I'm not the only one who feels this pain point. The makers of the underscore.js have done a fine job of solving the problem.

is array?

PHP

is_array

Validates an array.

<?php
$arr = array('one','two');
var_dump(is_array($arr));
// bool(true)
?>

underscore.js

isArray

Validates an array.

<script>
console.log(_.isArray(['one','two']));
// true
</script>

is boolean?

PHP

is_bool

<?php
var_dump(is_bool(true));
// bool(true)
?>

underscore.js

isBoolean

<script>
console.log(_.isBoolean(true));
// true
</script>

is a number?

PHP

PHP has a few methods because it handles numbers different than javascript.

is_numeric handles numbers and numeric strings

<?php
var_dump(is_numeric(42));
// bool(true)
var_dump(is_numeric('42'));
// bool(true)
?>

is_float handles float value validation.

<?php
var_dump(is_float(3.15));
// bool(true)
?>

is_integer handles validating integers.

<?php
var_dump(is_integer(3));
// bool(true)
?>

underscore.js

isNumber

isNumber handles checking for integers as well as floats. What it does not ddo that it's PHP counterpart does is validate numeric string like '42'.

<script>
console.log(_.isNumber(42));
// true

console.log(_.isNumber('42'));
// false

console.log(_.isNumber(3.15));
// true
</script>

is null?

PHP

is_null

Checks if a value is NULL or null but not another bottom value like false, an empty string`

<?php
var_dump(is_null(NULL));
// bool(true)
?>

underscore.js

isNull

<script>
console.log(_.isNull(null));
// true
console.log(_.isNull(undefined));
// false
console.log(_.isNull(''));
// false
console.log(_.isNull(false));
// false
console.log(_.isNull(0));
// false
</script>

is object?

PHP

is_object

This will validate PHP System objects as well as objects that you define yourself

<?php
class FooClass{
  public function __construct(){}
}
var_dump(is_object(new FooClass()));
// bool(true)
var_dump(is_object(new stdClass()));
// bool(true)
?>

underscore.js

isObject

This will validate object literals, objects created with the constructor pattern, and objects created with Object.create().

<script>
function FooClass(){}

var fooObject = new FooClass();
console.log(_.isObject(fooObject));
// true
console.log(_.isObject(Object.create(fooObject)));
// true
console.log(_.isObject({}));
// true
</script>

is string?

PHP

is_string

Validates a string.

<?php
var_dump(is_string('this is a string'));
// bool(true)
?>

underscore.js

isString

Validates a string.

<script>
console.log(_.isString('this is a string'));
// true
</script>

Thanks for reading! Follow me on Twitter and/or G+ for more.

See something wrong or got something to add/change? Fork this git repo and send me a pull request

If you particularly enjoy my work, I appreciate donations given with Gitcoin.co



Published

20 February 2013

Tags