darwin (owner)

Revisions

gist: 181965 Download_button fork
public
Public Clone URL: git://gist.github.com/181965.git
Embed All Files: show embed
PHP #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
// some helper functions to fix the language, have to put them into global scope, sorry guys
 
// this function is here mainly to fight "0" to eval as FALSE and empty arrays to eval as FALSE, which is a real evil!
function jse($val) {
    if (!isset($val)) return false; // NULL is false
    $type = gettype($val);
    if ($type=='object' || $type=='resource' || $type=='array') return true; // non-null objects are always true
    if ($type=='string') return !!count($val); // non-empty strings are true
    return (boolean)$val; // for integer, double, float and boolean (and possible new types) use PHP rules
}
 
// !!! does not follow PHP coercion rules but javascript-way of evaluating expressions
// see rules http://cz2.php.net/manual/en/language.types.boolean.php
function jsor() {
    $args = func_get_args();
    if (!count($args)) return null;
    foreach($args as $arg) {
        flog($arg, jse($arg));
        if (jse($arg)) return $arg;
    }
    return $args[count($args)-1];
}
 
// !!! does not follow PHP coercion rules but javascript-way of evaluating expressions
// see rules http://cz2.php.net/manual/en/language.types.boolean.php
function jsand() {
    $args = func_get_args();
    if (!count($args)) return null;
    foreach($args as $arg) {
        if (!jse($arg)) return null;
    }
    return $args[count($args)-1];
}
 
?>