PHP Integer

WBOY
Release: 2024-08-29 12:38:11
Original
811 people have browsed it

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How to Declare Integer Variable in PHP?

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

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
?>
Copy after login

Output:

PHP 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
?>
Copy after login

Output:

PHP 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;
?>
Copy after login

Output:

PHP Integer

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.

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;
?>
Copy after login

Output:

PHP Integer

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;
?>
Copy after login

Output:

PHP 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;
?>
Copy after login

Output:

PHP Integer

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;
?>
Copy after login

Output:

PHP Integer

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.

The above is the detailed content of PHP Integer. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!