PHP/Data Type/Integer

Материал из Web эксперт
Перейти к: навигация, поиск

Add an Integer value

   <source lang="html4strict">

<? $origVar = 100; $origVar += 25;

echo "

Added a value, now it"s $origVar

";

?>

      </source>
   
  


Add a PHP block and create a variable that holds an integer

   <source lang="html4strict">

<?

  1. create a variable that holds an integer

$intVar = "9554215464";

echo "

integer: $intVar

";

?>

      </source>
   
  


Addendum: finding the largest integer

   <source lang="html4strict">

<? function maxint(){

 $to_test = 2;
 while(1){
    $last = $to_test;
    $to_test = 2 * $to_test;
    if (($to_test < $last) || (!is_int($to_test))){
        return($last + ($last - 1));
    }    
  }

} $maxint = maxint(); print("Maxint is $maxint
"); ?>

      </source>
   
  


Adding two integers together

   <source lang="html4strict">

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

 </source>
   
  


Add the two integer values and print the result.

   <source lang="html4strict">

<? $a = 85; $b = 24;

echo "

Original value of \$a is $a and \$b is $b

";

$c = $a + $b;

echo "

Added \$a and \$b and got $c

";

?>

      </source>
   
  


++$answer and $answer++

   <source lang="html4strict">

<?php

   $answer = 5;
   echo (++$answer)." ";
   echo "$answer
"; $answer = 5; echo ($answer++)." ": echo $answer;

?>

 </source>
   
  


Assign an integer value this way

   <source lang="html4strict">

<?php $myint = 10; echo $myint . "
"; echo (int) $myint . "
"; $myint = 10 / 3; echo (int) $myint . "
"; ?>

 </source>
   
  


Calculating a date interval with epoch timestamps

   <source lang="html4strict">

<?php $birthday = 163727100; $gestation = 36 * 7 * 86400; // 36 weeks $whoopee_made = $birthday - $gestation; ?>

 </source>
   
  


Calculating Discount

   <source lang="html4strict">

<HTML> <BODY>

Calculating Discount

<?php

 $price = 200;
 $qty = 10;
echo "

Original price is $price

";
 if ($qty =10) {
     $discount=10;
 }elseif ($qty <=20) {
     $discount=15;
 }elseif (qty <=30) {
     $discount=20;
 }elseif($qty <=40){
     $discount=25;
 }
echo "

Discount is $discount percent

"; echo "

Price after discount is ", (($price * (100 - $discount)/ 100)), "

";

?> </BODY> </HTML>

      </source>
   
  


Divide the two values and print the result

   <source lang="html4strict">

<? $a = 85; $b = 24;

echo "

Original value of \$a is $a and \$b is $b

";

$c = $a / $b;

echo "

Divided \$a by \$b and got $c

";

?>

      </source>
   
  


Integer calculation

   <source lang="html4strict">

$i = 1; $j = 2; print "$i $j"; $i++; $j *= 2; print "$i $j"; $i++; $j *= 2; print "$i $j"; $i++; $j *= 2; print "$i $j"; $i++; $j *= 2; print "$i $j";

 </source>
   
  


Integers can be specified using any of the following syntaxes

   <source lang="html4strict">

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

 </source>
   
  


Integer value compare with equal sign

   <source lang="html4strict">

<? $a=100; $b=true; echo "a = $a
"; echo "b = $b
"; if($a==$b)

  echo "a "is equal to" b!"; ?>
          
      </source>
   
  


Integer values can be signed and unsigned in the range from �2,147,483,648 to 2,147,483,647.

   <source lang="html4strict">

<?php $i = 0x7FFFFFFF; echo "$i is " . (is_int($i) ? "an integer" : "a float") . "\n"; $i++; echo "$i is " . (is_int($i) ? "an integer" : "a float") . "\n"; ?>

 </source>
   
  


Int value overflow

   <source lang="html4strict">

<? $too_big = 111; for ($count = 0; $count < 5; $count++) {

   $too_big = 1000 * $too_big;
   print("Is $too_big still an integer?
");

} ?>

      </source>
   
  


Multiply the two values and print the result

   <source lang="html4strict">

<? $a = 85; $b = 24;

echo "

Original value of \$a is $a and \$b is $b

";

$c = $a * $b;

echo "

Multiplied \$a and \$b and got $c

";

?>

      </source>
   
  


Storing Integers in PHP

   <source lang="html4strict">

<?php

   $my_int = 50;       /* Standard Decimal Notation */
   $my_int = O62;      /* Same number, Octal Notation (starts with the letter O)*/
   $my_int = 0x32;     /* Hexadecimal Notation */

?>

 </source>
   
  


Subtracted an Integer value

   <source lang="html4strict">

<? $origVar = 100; $origVar -= 12;

echo "

Subtracted a value, now it"s $origVar

";

?>

      </source>
   
  


Subtract the two values and print the result

   <source lang="html4strict">

<? $a = 85; $b = 24;

echo "

Original value of \$a is $a and \$b is $b

";

$c = $a - $b;

echo "

Subtracted \$b from \$a and got $c

";

?>

      </source>
   
  


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

   <source lang="html4strict">

//To assign the number 68, you would use this:

   $hexnum = 0x44;
   print $hexnum;
 
 </source>
   
  


Using typecasting to convert numbers to strings

   <source lang="html4strict">

<?php

   $i = 123456; 
   $f = 98765.567; 
   
   echo "\$i = " . (string)$i . "\n"; 
   echo "\$f = " . (string)$f . "\n"; 

?>

 </source>