Web programming

Working with Number Data Types in PHP

Ngoc Phuong

Ngoc Phuong

2021-02-26 . 150 view

In PHP, automatic data type conversion is provided, for example, if you assign an integer value to a variable, the type of that variable will automatically be an integer. Then, if you assign a string to the same variable, the type will change to a string. This is something you need to pay attention to, as it can lead to errors in your program.

I. Integers in PHP

An integer is a number without any decimal part.

For example, 2, 256, -256, 10358, -179567 are integers. While 7.56, 10.0, 150.67 are floats.

Therefore, the integer data type is a non-decimal number from -2147483648 to 2147483647. A value larger (or smaller) than this will be stored as a float as it exceeds the limit of an integer.

Note: Even when the operation 4 * 2.5 is 10, the result is still stored as a float, as one of the operands is the float 2.5.

Some rules for integers:

  • An integer must have at least one digit.
  • An integer should not have a decimal point.
  • An integer can be either positive or negative.
  • Integers can be specified in three formats: decimal (base 10), hexadecimal (base 16 - prefixed with 0x) or octal (base 8 - prefixed with 0).

PHP also provides several functions to check whether a variable is an integer:

  • is_int()
  • is_integer()
  • is_long()

Example

$x = 5985;
var_dump(is_int($x));

$x = 59.85;
var_dump(is_int($x));

II. Float Numbers in PHP

A float is a number with a decimal point or a number in exponential form.

For example, 2.0, 256.4, 10.358, 7.64E+5, 5.56E-5 are all floats.

PHP also provides several functions to check whether a variable is a float:

  • is_float()
  • is_double()

Example

$x = 10.365;
var_dump(is_float($x));

III. Numerical Strings in PHP

In PHP, the function is_numeric() is provided to determine whether a variable is a string or a number. If the function returns TRUE, it's a Number type, FALSE implies it's a String type.

Example

// Kiểm tra kiểu String hay Number 
$x = 5985;
var_dump(is_numeric($x));  // Trả về: bool(true)

echo "<br>";

$x = "5985";
var_dump(is_numeric($x));  // Trả về: bool(true)

echo "<br>";

$x = "59.85" + 100;
var_dump(is_numeric($x));  // Trả về: bool(true)

echo "<br>";

$x = "Hello";
var_dump(is_numeric($x));  // Trả về: bool(false)

IV. Data type casting in PHP

In some programs, you sometimes need to convert data types for easier calculation, PHP provides functions for you to do this.

For example, the functions (int), (integer) or intval() are often used to convert a value to an integer. Similar to integers, floats also have the function (float) for type casting.

Example

// Chuyển kiểu dữ liệu từ float sang int 
$x = 23465.768;
$int_cast = (int)$x;
echo $int_cast;
  
echo "<br>";

// Chuyển kiểu dữ liệu từ string sang int 
$x = "23465.768";
$int_cast = (int)$x;
echo $int_cast;

// Function intval() trả về số nguyên nếu thành công, trả về 0 nếu thất bại. Nếu bạn truyền vào một mảng rỗng thì sẽ trả về 0, mảng không rỗng sẽ trả về 1.

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(4200000000000000000);   // 0
echo intval('4200000000000000000'); // 2147483647
echo intval(42, 8);                   // 42
echo intval('42', 8);                 // 34
echo intval(array());                 // 0
echo intval(array('hello', 'word'));     // 1

 

Ngoc Phuong
Ngoc Phuong

Web Developer

Thank you for visiting my website. My name is Ngoc Phuong, and I have over 10 years of experience in website development. I am confident in stating that I am an expert in creating impressive and effective websites. If you need a website designed, please feel free to contact me via email at [email protected].

0 feedback