
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP Variable Handling intval() Function
The PHP Variable Handling intval() function is used to convert a value to an integer (a whole number). It accepts two inputs: the value to change and the base (number system) to use. It defaults to base 10 (decimal), but it also supports binary, octal and hexadecimal.
If the value is not a number, PHP will try to convert it with special rules. If the value is null or not a number, intval() usually returns zero. If the value is an array, an empty array returns 0 and a non-empty array returns 1. On 32-bit processors, the largest number it may return is 2,147,483,647. On 64-bit platforms, it can produce even greater results up to 9,223,372,036,854,775,807.
Syntax
Below is the syntax of the PHP Variable Handling intval() function −
int intval ( mixed $value, int $base = 10 )
Parameters
Below are the parameters of the intval() function −
$value − It is the value that needs to be converted into an integer. It can be a number, string or other scalar value.
$base − It is the number system to use for conversion. Default is 10 (decimal).
Return Value
The intval() function returns the integer version of the given value. If conversion fails, it returns 0. Empty array returns 0, non-empty array returns 1.
PHP Version
First introduced in core PHP 4, the intval() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
This basic program converts a floating-point number into an integer with the help of the PHP Variable Handling intval() function. The decimal part is removed and only the whole number remains.
<?php // A floating-point number $num = 12.98; // Convert to integer $result = intval($num); echo $result; ?>
Output
Here is the outcome of the following code −
12
Example 2
In the below PHP code we will use the intval() function and convert a string into a integer number. So the program will convert a numeric string into an integer. If the string starts with numbers they are kept, otherwise it will returns 0.
<?php // A string with numbers $str = "150 apples"; // Convert to integer $result = intval($str); echo $result; ?>
Output
This will generate the below output −
150
Example 3
Now the below code uses different bases like binary, octal and hexadecimal. And the program converts strings in different number systems into integers with the help of the intval() function and specifying the base.
<?php // Binary (2) $binary = "110"; // Octal (8) $octal = "075"; // Hexadecimal (16) $hex = "0x1A"; echo intval($binary, 2) . "\n"; echo intval($octal, 8) . "\n"; echo intval($hex, 16) . "\n"; ?>
Output
This will create the below output −
6 61 26
Example 4
In the following example, we are using the intval() function to show return values with different types of variables.
<?php echo intval(42); echo intval(4.2); echo intval('42'); echo intval('+42'); echo intval('-42'); echo intval(042); echo intval('042'); echo intval(1e10); echo intval('1e10'); echo intval(0x1A); echo intval('0x1A'); echo intval('0x1A', 0); echo intval(42000000); echo intval(420000000000000000000); echo intval('420000000000000000000'); echo intval(42, 8); echo intval('42', 8); echo intval(array()); echo intval(array('foo', 'bar')); echo intval(false); echo intval(true); ?>
Output
Following is the output of the above code −
100 100 100 100 -100 68 104 10000000000 10000000000 43 10400000 -4275113695319687168 9223372036854775807 11 13