PHP 7.2.0 Beta 1 Released

Voting

Please answer this simple SPAM challenge: three plus six?
(Example: nine)

The Note You're Voting On

dimitrov dot adrian at gmail dot com
6 years ago
This is mine type casting lib, that is very useful for me.

<?php

function CAST_TO_INT($var, $min = FALSE, $max = FALSE)
{
   
$var = is_int($var) ? $var : (int)(is_scalar($var) ? $var : 0);
    if (
$min !== FALSE && $var < $min)
        return
$min;
    elseif(
$max !== FALSE && $var > $max)
        return
$max;
    return
$var;
       
}

function
CAST_TO_FLOAT($var, $min = FALSE, $max = FALSE)
{
   
$var = is_float($var) ? $var : (float)(is_scalar($var) ? $var : 0);
    if (
$min !== FALSE && $var < $min)
        return
$min;
    elseif(
$max !== FALSE && $var > $max)
        return
$max;
    return
$var;
}

function
CAST_TO_BOOL($var)
{
    return (bool)(
is_bool($var) ? $var : is_scalar($var) ? $var : FALSE);
}

function
CAST_TO_STRING($var, $length = FALSE)
{
    if (
$length !== FALSE && is_int($length) && $length > 0)
        return
substr(trim(is_string($var)
                    ?
$var
                   
: (is_scalar($var) ? $var : '')), 0, $length);

    return
trim(
               
is_string($var)
                ?
$var
               
: (is_scalar($var) ? $var : ''));
}

function
CAST_TO_ARRAY($var)
{
    return
is_array($var)
            ?
$var
           
: is_scalar($var) && $var
               
? array($var)
                :
is_object($var) ? (array)$var : array();
}

function
CAST_TO_OBJECT($var)
{
    return
is_object($var)
            ?
$var
           
: is_scalar($var) && $var
               
? (object)$var
               
: is_array($var) ? (object)$var : (object)NULL;
}

?>

<< Back to user notes page

To Top