Integer


Integers hold whole numbers, either positive or negative, such as 1, -20, 12345,etc. The limit of integer is between -2147483647 and 2147483647. The integer numbers outside the range are automatically converted to floats.


Rules for integers:


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

            <?php 
                $var1 = 6; 
                $var2 = 4; 
                $answer = $var1 + $var2; 
                print "$var1 plus $var2 equals $answer."; 
            ?>
          

output


            6 plus 4 equals 10.          

PHP integer Literal


We may specify integers in hexadecimal (base 16) or octal (base 8). The octal number system only uses the digits 0 to 7. hexadecimal uses 0 to 9, then A, B, C, D, E, and F.


To specify number in octal, we must precede it with a 0 (zero).


            <?php 
                $octalnum = 07358; 
                print $octalnum; 
            ?>
          

output


            7358          


Hexadecimal


To specify a number in hexadecimal, precede it with 0x.


            <?php 
                 $hexnum = 0x44; 
                 print $hexnum;  
            ?>
            

output


            68            

Integer literal


            <?php 
                $a = 1234; # decimal number
                print($a);
                print("\n");
                $a = -123; # a negative number
                print($a);
                print("\n");
                $a = 0123; # octal number (equivalent to 83 decimal)
                print($a);
                print("\n");
                $a = 0x12; # hexadecimal number (equivalent to 18 decimal)
                print($a);
                print("\n");  
            ?>
            

output


            1234
-123
83
18
            


Float


Floats hold fractional numbers as well as very large integer numbers, such as 9.5, 4.00000006, and 123123123123000.


Floating-point numbers are called real numbers or just floats.


Float Literal


We may also specify an exponent with your float, i.e., 3.14159e4 is equal to 31415.9.


               <?php 
                  $a = 1.234; 
                  print ($a);
                  print ("\n");
                  $a = 1.2e3;
                  print ($a);  
                ?>
            

output


            1.234
1200            

floating-point arithmetic


                <?PHP
                  $a = 1.132324; 
                  print($a);
                  $b = $a + 1.9; 
                  print($b);
                  $b = $a + 1.0; 
                  print($b);
                  $c = 1.1e15; 
                  print($c);
                  $d = (0.1+0.7) * 10; 
                  print($d);
                ?>
            

Output


                1.1323243.0323242.1323241.1E+158            


Calculate the area of a circle of given radius


                <?PHP
                  $radius = 2.0;
                  $pi = 3.14159;
                  $area = $pi * $radius * $radius;

                  echo("radius = ");
                  echo($radius);
                  echo("<BR>area = ");
                  echo($area);
                ?>
            

Output


                radius = 2
area = 12.56636




PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion