PHP

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

Written by Ian Carnaghan · 16 sec read >

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}

Last Updated On March 22, 2018
Written by Ian Carnaghan
I am a software developer and online educator who likes to keep up with all the latest in technology. I also manage cloud infrastructure, continuous monitoring, DevOps processes, security, and continuous integration and deployment. In my spare time I teach undergraduate classes in web development. Profile

Leave a Reply

avatar
  Subscribe  
Notify of