
Please support if you like my work by payment through upi: sinhamit@icici or payment by bank
account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB
Whole Numbers in Category: PHP by amit
🕙 Posted on 2023-04-30 at 05:00:41 Read in Hindi ...
Integers
Integers are whole numbers, either negative or positive. For 32-bit system (local computer or HOST SERVER), its minimum and maximum values are approximately, negative 2 billion (−2,147,483,648), 0
(zero is considered as positive integer) and positive 2 billion (2,147,483,647).
For 64-bit system, minimum and maximum values for integers are − 9,223,372,036,854,775,808 (2 * 2,147,483,648 * 2,147,483,648), 0
and 9,223,372,036,854,775,807 (zero is the first number in all C based programming language, for counting items in compound data-types, for example, array). You can check these values in your computer or HOST SERVER, by following code:
<?php
echo PHP_INT_MAX
; echo '<br />';
echo PHP_INT_MIN
; echo '<br />';
echo PHP_INT_SIZE
;?>
-9223372036854775808
8
The above three lines are OUTPUT (<br /> HTML element is added accordingly,) of above mentioned code. PHP_INT_MAX PHP_INT_MIN PHP_INT_SIZE
are pre-defined Constants. Constant names are also case-sensitive, and it is convention to write them in all uppercase. You can learn more about naming variables and constants in previous page. If you write these pre-defined constants not in all uppercase, ERRORs will be displayed, as follows:
Fatal error: Uncaught Error: Undefined constant "php_int_max" in C:\xampp\htdocs\php_project\phptest.php:2 Stack trace: #0 {main} thrown in C:\xampp\htdocs\php_project\phptest.php on line 2
Different ways to write Integers in PHP
Integers are whole numbers, without fraction. That is, you can write integers either in binary (only with two digits, 0
and 1
), octal (only eight digits, from 0
to 7
), decimal (only ten digits, from 0
to 9
without . dot), or hexadecimal (only sixteen digits, from 0
to 9
and lowercase/uppercase a
to f
).
<?php
echo 123; // decimal number without dot
echo '<br />'; echo 0123; // octal number equivalent to 83
echo '<br />'; echo 0o123
; // octal number (as of PHP 8.1.0)
echo '<br />'; echo 0x53
; // hexadecimal number equivalent to 83
echo '<br />'; echo 0b1010011
; // binary number equivalent to 83 ?>
There is always some update in PHP, which you should check, on official documentation at php.net. However, when writing codes for your PHP website, you should be careful, and also check the PHP version installed on the HOST SERVER. Any update or feature of latest PHP version may not be available for the PHP version installed on your local PC or remote HOST SERVER.
Octal numbers must be prefixed with 0
(zero), and as of PHP 8.1.0 you can also use 0o
or 0O
(english letter 'o' either in lowercase or uppercase after zero) to distinguish from other methods. Similarly, hexadecimal and binary numbers must be prefixed with 0x
or 0X
and 0b
or 0B
(lowercase/
You must not use 0
(zero) before any digit in decimal representation of integers, because this will be treated as octal representation. Also, you should be careful, while asking your customers to input digit (numbers) for item/product count. Large integers in decimal representation, must be without . (dot) and , (comma) unlike written in general English language. However, you can use _ (underscore) between digits in large integer literals.
<?php
echo 1_234_567_890; // outputs 1234567890
echo '<br />'; echo (int)
'1_234_567_890'; // outputs 1?>
The first line of above code contains a integer value, whereas the second line contains a string value, enclosed in a pair of single quotes. This string value is type-casted into integer, by using (int)
and therefore, you should carefully use _ (underscore) in your integer numbers. The OUTPUT from both lines are as follows:
1
Type Casting
You have read in previous page about Type Juggling and Type Casting. Hope, you have tried some experiments with boolean values to type-cast into different data-types, (for example, (integer)
, (float)
and (string)
). Some examples are described below. Because PHP is a dynamic (loose) data-type scripting language, you should be careful, while receiving inputs from your customers and how you deal with those digits (numbers and characters also). Let't examine some type casting of integer data-type into other data-types and vice-versa.
<?php
echo (int) true
; // outputs 1
echo '<br />'; echo (int) false
; // outputs 0 ?>
<?php
var_dump( (bool) 1
); // outputs bool(true)
echo '<br />'; var_dump( (bool) 0
); // outputs bool(false) ?>
<?php
echo (bool) -123
; // outputs 1
echo '<br />'; echo (bool) -0
; // outputs nothing
echo '<br />'; var_dump( -0
); // outputs 0 ?>
Any integer value other than 0
(zero) when type-casted to boolean, the output will be 1 (true) with echo
function, and bool(true) with var_dump()
function. 0
(zero) or -0
(minus zero) is when type-casted into boolean, the output will be bool(false) with var_dump()
function, and echo
function will output nothing '' or "" (empty string).
Any number, either integer or float (with a dot symbol) inside a pair of single quotes or a pair of double quotes, when type-casted into integer (or float) will output the numeric result. You will learn more about float in next page.
Any character within a pair of single quotes or a pair of double quotes, is treated as string and will not be converted into numeric value. Any character will be treated as zero when type-casted into (integer)
(excluding only one dot symbol and/or first minus character, till next digit within quotes is not number).
<?php
echo (int) 'true'
; // outputs 0
echo '<br />'; echo (int) 'false'
; // outputs 0 ?>
<?php
echo (int) '123'
; // outputs 123
echo '<br />'; echo (int) '123.5'
; // outputs 123 ?>
<?php
echo (int) '-123'
; // outputs -123
echo '<br />'; echo (int) '123abc'
; // outputs 123 ?>
<?php
echo (bool) 'true'
; // outputs 1
echo '<br />'; echo (bool) 'false'
; // outputs 1 ?>
You should be careful while copying these codes, and must avoid pasting them directly into a file with .php
extension. Many browsers, and WEBSITE framework may convert these special characters with CSS style. You can visit previous page, to know how to write HTML, PHP codes, or other characters in a TEXT / CODE Editor to avoid ERRORS.
<?php
echo (int) '−12.3'
; // outputs 0 because it is a string data-type
echo '<br />'; echo −123; // outputs ERROR as shown below ?>
The VSCode Editor will highlight any of these special character with golden border around it. The above PHP code contains a special character − instead of - (dash) symbol in your keyboard. You should find these − symbol in VSCode Editor, and replace by typing - key from your keyboard. Other simple TEXT Editor may not highlight this type of ERROR, and you may get ERRORs in your OUTPUT!
Fatal error: Uncaught Error: Undefined constant "−123" in C:\xampp\
If ERRORs occur in your program, it does not mean that you are a bad programmer, but all these ERRORs displayed in WEB-BROWSER tell us something. When you find solutions to these ERRORs and BUGs (a bug is not an error, but unexpected result is output because of such a bug) then you will become a good programmer. Every person, either beginner or expert, has been encountered with these ERRORs and BUGs in his/her daily work.
Leave a Comment:

Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤

Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇