Showing posts with label PHP intval. Show all posts
Showing posts with label PHP intval. Show all posts

Friday, 2 August 2019

How to convert all $_POST or $_GET values to Integers

By using the built-in PHP functions  array_map and intval.
  • array_map = Applies the callback to the elements of the given arrays, this applies the function for each value of an array (PHP 4 >= 4.0.6, PHP 5)
  • intval = Get the integer value of a variable (PHP 4, PHP 5)
Examples:
{codecitation style=”brush: PHP;”}
$post_int = array_map(‘intval’, $_POST); //Converting every post value to integer
$get_int = array_map(‘intval’, $_GET); //Converting every get value to integer
{/codecitation}

Monday, 20 July 2015

PHP: Expiry date code

<?php
    date_default_timezone_set ("Asia/Calcutta");
    $dateofreg1=date("M d Y");
    $puechese_date="08/13/2014";
    $startTime = strtotime($puechese_date);
    $endTime = strtotime($dateofreg1);
    $timeDiff = abs($startTime-$endTime);
    $numberDays = $timeDiff/86400;
    $numberDays = intval($numberDays);
    $validays="90";
    if($numberDays>$validays)
        {
        echo "Product Valid";
        }
    else
        {
        echo "Product Expired";
        }
?>

Friday, 26 June 2015

PHP: Trim A String with PHP

<?php
/* 
Example usage: 

$long_string="This is a very long string that I want shortened"; 
$short_string=trimString($long_string, 10); 

echo $short_string;  

Will output "This is..." 
*/ 
function trimString($str, $len) { 
    If(strlen($str)>intval($len)) { 
            return(substr($str,0,($len-3))."..."); 
    } 
    return($str); 
?>

Tuesday, 2 June 2015

PHP: Convert an integer to roman-numeral

<?php function romanNumerals($num){ 
    
$n intval($num); 
    
$res ''; 

    
/*** roman_numerals array  ***/ 
    
$roman_numerals = array( 
        
'M'  => 1000, 
        
'CM' => 900, 
        
'D'  => 500, 
        
'CD' => 400, 
        
'C'  => 100, 
        
'XC' => 90, 
        
'L'  => 50, 
        
'XL' => 40, 
        
'X'  => 10, 
        
'IX' => 9, 
        
'V'  => 5, 
        
'IV' => 4, 
        
'I'  => 1); 

    foreach (
$roman_numerals as $roman => $number){ 
        
/*** divide to get  matches ***/ 
        
$matches intval($n $number); 

        
/*** assign the roman char * $matches ***/ 
        
$res .= str_repeat($roman$matches); 

        
/*** substract from the number ***/ 
        
$n $n $number; 
    } 

    
/*** return the res ***/ 
    
return $res; 
} 

echo 
romanNumerals(23); ?>

Friday, 3 October 2014

intval in PHP

intval — Get the integer value of a variable
Syntax: int intval ( mixed $var [, int $base = 10 ] )
Returns the integer value of var, using the specified base for the conversion (the default is base 10).
intval() should not be used on objects, as doing so will emit an E_NOTICE level error and return 1.
Parameters:
var:The scalar value being converted to an integer
base:The base for the conversion
Note:
If base is 0, the base used is determined by the format of var:
        if string includes a "0x" (or "0X") prefix, the base is taken as 16 (hex); otherwise,
        if string starts with "0", the base is taken as 8 (octal); otherwise,the base is taken as 10 (decimal).
Return Values:
The integer value of var on success, or 0 on failure. 
Empty arrays return 0, non-empty arrays return 1.
The maximum value depends on the system. 
32 bit systems have a maximum signed integer range of -2147483648 to 2147483647.
So for example on such a system, intval('1000000000000') will return 2147483647.
The maximum signed integer value for 64 bit systems is 9223372036854775807.
Strings will most likely return 0 although this depends on the leftmost characters of the string. 
The common rules of integer casting apply.
Example #1:

The following examples are based on a 32 bit system.
<?php
echo intval(42);                      // 42
echo intval(4.2);                     // 4
echo intval('42');                    // 42
echo intval('+42');                   // 42
echo intval('-42');                   // -42
echo intval(042);                     // 34
echo intval('042');                   // 42
echo intval(1e10);                    // 1410065408
echo intval('1e10');                  // 1
echo intval(0x1A);                    // 26
echo intval(42000000);                // 42000000
echo intval(420000000000000000000);   // 0
echo intval('420000000000000000000'); // 2147483647
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('foo', 'bar'));     // 1
?>
Note:The base parameter has no effect unless the var parameter is a string.

Changelog:
Version Description
5.1.0 Throws E_NOTICE and returns 1, when an object is passed to var.