PHP Dasar - Tipe Data Number


Tipe Data Number

Di PHP terdapat 2 (dua) jenis tipe data number
  • (int) Bilangan bulat decimal (base 10), hexadecimal (base 16), octa (base 8), binary(base 2);
  • (float) Bilangan pecahan.
kita juga bisa menambahkan _ (garis bawah) di angka, agar mudah dibaca, saat dijalankan, _ (garis bawah) tersebut akan diignore atau tidak dihiraukan.

Untuk angka negatif, kita bisa menggunakan tanda -(minus) di depan angka.

Integer

<?php

echo "Decimal:";
var_dump(1234);

echo "Octal:";
var_dump(01234);

echo "Hexadecimal:";
var_dump(0x1A);

echo "Binary:";
var_dump(0b11111);

echo "Underscore:";
var_dump(123_321_000);

Floating Point

<?php

echo "Floating Point:";
var_dump(123.4);

echo "Floating Point dengan E notation : (1.7 x 1000)";
var_dump(1.7e3);

echo "Floating Point dengan E notation : (1.7 x 0.001)";
var_dump(1.7e-3);

echo "Floating Point dengan Underscore";
var_dump(1.2_345);

Integer Overflow

Kapasitas integer di PHP ada batasnya, 2147483647 untuk sistem operasi 32 bit, dan 9223372036854775807 untuk sistem operasi 64 bit.

Jika membuat number melebihi nilai tersebut, maka otomatis tipe numbernya akan berubah menjadi float.

<?php

echo "Integer Overflow 32 Bit : ";
var_dump(2147483647);//Integer Overflow 32 Bit : int(2147483647)

echo "Integer Overflow 64 Bit : ";
var_dump(9223372036854775807);//Integer Overflow 64 Bit : int(9223372036854775807)

Posting Komentar

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.