EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

PHP Integer

By Shree Ram SharmaShree Ram Sharma

Home » Software Development » Software Development Tutorials » PHP Tutorial » PHP Integer

PHP Integer

Introduction to PHP Integer

Before going to start talking about Integers in PHP, let’s understand the term Integer first. An Integer is a type of data type. A type of variable that keeps the complete numeric value. Complete (whole) numbers like – 1, 23, 343,-23, -50 etc. Integer could be either positive, negative or 0 itself.  Almost every programming language like C, JAVA, and C++ support full-featured integers. But, when we come to the PHP language, the moment we assign any integer value to any variable, it can be considered as an integer data type. Since PHP is a loosely kind programming language, there is no need to declare any variable with specified data type before using it. Integers can be used directly in PHP at the time of the assignment. There is an integer data type in almost all the programming languages to deal with the whole number kind of value.

How to Declare Integer Variable in PHP?

There are 2 ways to declare integer variable in PHP given below:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Positive Integer

A whole number that contains its value either 0 or more than 0. PHP supports its various primitive data types like –  Integer, Float number, Character, String, Booleans (true or false), mixed array, etc.In the example section, we will see one by one of all the types of integers that we can process using the PHP programming language.

Code:

<?php
$x = 124; // declaration and assignment
var_dump($x); // int 124
echo $x;            // 124
?>

Output:

Positive integer

Negative Integer

A whole number that contains its value less than 0.

Code:

<?php
$x = -124; // declaration and assignment
var_dump($x); // int -124
echo $x;            // -124
?>

Output:

Negative integer

We can see the output of var_dump($x) that is showing int -124; int is the data type, and the -124 is the value.

Advanced PHP Integer Examples

Below are the different examples of integer in php:

Example #1

Integer as a hexadecimal code:

Code:

<?php
$x = 0x1B; // hexadecimal number
var_dump($x);
echo $x;
?>

Output:

hexadecimal code

Now, the question is, why 27? This is something how PHP supports integer. The moment we assignment anything (expression, function return, etc.) in a variable that will be considered an integer data type is the output of that code or function in an integer.

Popular Course in this category
PHP Training (5 Courses, 3 Project)5 Online Courses | 3 Hands-on Project | 28+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (6,082 ratings)
Course Price

View Course

Related Courses
Java Servlet Training (6 Courses, 12 Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

In the case of about code, we are getting 27 because we have assigned a hexadecimal value that equals to 27. Where 0x is 16 and B is 11; if we add both of these, wee will get 27 as an output.

Example #2

Integer as an octal code:

Code:

<?php
$x = 0123; // octal number
var_dump($x);
echo $x;
?>

Output:

example 2 of hexadecimal

Again this is an integer since the output of the given octal code is an integer.

Example #3

The function returned as an integer:

Code:

<?php
function addNumbers($a, $b){
$sum = 0;
$sum = $a + $b;
return $sum;
}
$x = addNumbers(10,20);
var_dump($x);
echo $x;
?>

Output:

function returned as integer

Again we have 30 the sum of given two integers as an integer. But if we make a very minor change in the above code,, it will give us float as an output.

Example #4

The function returned as a float:

Code:

<?php
function addNumbers($a, $b){
$sum = 0;
$sum = $a + $b;
return $sum;
}
$x = addNumbers(10,20.0);
var_dump($x);
echo $x;
?>

Output:

function as float

We can see, all things remain the same as was in the previous code example except the function parameter value.

$x = addNumbers(10,20.0);

We have used 20.0 in place of 20. This is something good enough to change the output from integer to floating. The output remains the same, but the data type has been changed from integer to floating.

Example #5

Using a mixed data type:

<?php
$x = 12.0; // float number
var_dump($x);
echo $x;
$x = 120; // integer number
var_dump($x);
echo $x;
?>

Output:

mixed datatype

We can see that any variable’s data type will be changed dynamically as per the value assigned to it. In the above code, we are using float data type value first; then, it gives data type float. But the moment we assign the value as an integer, it will give us integer data type as a result.

After going through the above example, we are now clear with how we can identify the integer.

Integers can be a normal whole numbers (that is a base 10 of any number), hexadecimal notation numbers (base 16 that starts with the 0x) or the octal notation number (base 8 that starts with a 0), or any complete number (whole number) prefixed with the a sign either – or + or by nothing.

Conclusion

After coming across all the statements and the example code given above, we can say PHP is a loosely typed programming language. The use of the word loosely comes to a meaning that there is no need to use any data type at the time of declaration. If we need to declare any type, we need not required to give its data type. PHP itself will take care of the data type of any variable of the identifiers as per the value assigned to it. So, in-directly, PHP supports all the available data types in the market in general.

Recommended Articles

This is a guide to PHP Integer. Here we discuss the 2 types, which include positive and negative integers with advanced PHP integer examples. You may also look at the following articles to learn more –

  1. Factorial in PHP
  2. Variables in PHP
  3. PHP Math Functions
  4. PHP Magic Constants

PHP Training (5 Courses, 3 Project)

5 Online Courses

3 Hands-on Project

28+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PHP Tutorial
  • Data Types
    • PHP Data Types
    • PHP Integer
    • PHP Booleans
  • PHP Basic
    • Introduction To PHP
    • What is PHP
    • PHP Keywords
    • Advantages of PHP
    • Career In PHP
    • Comments in PHP
    • PHP Commands
    • PHP Frameworks
    • PHP Compiler
    • Variables in PHP
    • PHP Superglobal Variables
    • PHP Versions
    • Object in PHP
    • PHP Object Injection
    • What is Drupal
    • Top PHP Frameworks
    • WebStorm IDE
    • What is phpMyAdmin?
    • PhpStorm
    • Install phpMyAdmin
    • Phalcon Model
  • Operators
    • PHP Operators
    • Arithmetic Operators in PHP
    • Comparison Operators in PHP
    • Logical Operators in PHP
    • Bitwise Operators in PHP
    • Ternary Operator in PHP
    • PHP String Operators
  • Control Statements
    • Control Statement in PHP
    • PHP if Statement
    • if else Statement in PHP
    • elseif in PHP
    • PHP Switch Statement
    • Continue in PHP
    • Break in PHP
  • Loops
    • PHP Loops
    • For Loop in PHP
    • PHP Do While Loop
    • PHP While Loop
    • While Loop in PHP
    • Foreach Loop in PHP
  • Constructor
    • Constructor in PHP
    • Destructor in PHP
  • State Management
    • Cookie in PHP
    • Sessions in PHP
  • Array
    • What is PHP Array
    • Arrays in PHP
    • 2D Arrays in PHP
    • Associative Array in PHP 
    • Multidimensional Array in PHP
    • Indexed Array in PHP
    • PHP Array Functions
    • PHP unset Array
    • PHP Append Array
    • PHP Array Search
    • PHP Split Array
    • PHP array_push()
    • PHP array_pop()
  • Functions
    • Functions in PHP
    • PHP Math Functions
    • PHP Recursive Function
    • PHP String Functions
    • Hashing Function in PHP
    • Date Function in PHP
    • PHP Anonymous Function
    • Calendar in PHP
    • PHP Call Function
    • PHP Pass by Reference
    • PHP list
    • PHP ucfirst()
    • PHP ucwords()
    • trim() in PHP
    • isset() Function in PHP
    • PHP replace
    • PHP fpm
    • preg_match in PHP
    • PHP preg_replace()
    • PHP ob_start()
    • PHP Reflection
    • PHP Split String
    • PHP URL
    • PHP preg_match_all
    • PHP strtoupper()
    • PHP preg_split()
    • PHP substr_replace()
    • PHP setlocale()
    • PHP substr_count()
    • PHP Serialize
    • PHP strlen()
    • PHP async
    • PHP Date Time Functions
    • PHP timezone
    • PHP Data Object
    • print_r() in PHP
    • PHP header()
    • PHP strip_tags()
    • PHP chop()
    • PHP MD5()
    • PHP unset()
    • PHP crypt()
    • PHP wordwrap()
    • PHP is_null()
    • PHP strtok()
    • PHP bin2hex()
    • PHP parse_str()
    • PHP levenshtein()
    • PHP addslashes()
    • PHP strtotime
    • PHP strpos
    • PHP sha1()
    • PHP explode()
    • PHP sscanf()
    • PHP require_once
    • PHP Zip Files
    • PHP $_SERVER
    • PHP $_POST
    • PHP Include and Require
    • PHP POST Method
  • Advanced
    • Overloading in PHP
    • Overriding in PHP
    • Method Overloading in PHP
    • Inheritance in PHP
    • Multiple Inheritance in PHP
    • PHP Interface
    • Encapsulation in PHP
    • PHP Constants
    • PHP Magic Constants
    • PHP Regular Expressions
    • PHP GET Method
    • PHP Annotations
    • PHP Encryption
    • PHP file Functions
    • PHP readfile
    • PHP?Write File
    • PHP Append File
    • PHP Type Hinting
    • PHP Filters
    • PHP Float
    • PHP Form
    • PHP Form Builder
    • PHP Form Validation
    • Sorting in PHP
    • PHP usort()
    • Sort string PHP
    • PHP Stack Trace
    • PHP Stack Overflow
    • PHP Pagination
    • PHP implode
    • Polymorphism in PHP
    • Abstract Class in PHP
    • PHP Final Class
    • PHP Custom Exception
    • error_reporting() in PHP
    • PHP Log Errors
    • Access Modifiers in PHP
    • PHP Change Date Format
    • Static Method in PHP
    • PHP File Handling
    • PHP Output Buffering
    • Get IP Address in PHP
    • PHP Open File
    • Upload a File in PHP
    • String in PHP
    • Public Function in PHP
    • Private in PHP
    • Protected in PHP
    • basename in PHP
    • Validation in PHP
    • PHP mail()
    • PHP Email Form
    • PHP Directory
    • PHP Create Session
    • PHP include_once
    • PHP json_decode
    • PHP XMLWriter
    • PHP XML Reader
    • PHP XML Parser
    • PHP XML into Array
    • Phalcon Framework
  • Programs
    • Patterns in PHP
    • Star Patterns in PHP
    • Swapping in PHP
    • Fibonacci Series PHP
    • Factorial in PHP
    • Reverse String in PHP
    • Square Root in PHP
    • Random Number Generator in PHP
    • Palindrome in PHP
    • Prime Numbers in PHP
    • Armstrong Number in PHP
    • Socket Programming in PHP
    • Login Page in PHP
    • PHP Login Template
    • PHP Object to String
  • Database
    • PHP Database Connection
    • How to Connect Database to PHP
  • Interview Questions
    • PHP Interview Questions
    • PHP OOP Interview Questions
    • CakePHP Interview Questions
    • Core PHP Interview Questions

Related Courses

PHP Training Course

Java Servlet Training

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - PHP Training (5 Courses, 3 Project) Learn More