PHP/Data Type/Integer — различия между версиями

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

Версия 10:37, 26 мая 2010

Add an Integer value

<?
$origVar = 100;
$origVar += 25;
echo "<P>Added a value, now it"s $origVar</P>";
?>



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

<?
# create a variable that holds an integer
$intVar = "9554215464";
echo "<P>integer: $intVar</P>";
?>



Addendum: finding the largest integer

<?
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<BR>");
?>



Adding two integers together

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



Add the two integer values and print the result.

<?
$a = 85;
$b = 24;
echo "<P>Original value of \$a is $a and \$b is
$b</P>";
$c = $a + $b;
echo "<P>Added \$a and \$b and got $c</P>";
?>



++$answer and $answer++

 
<?php
    $answer = 5;
    echo (++$answer)." ";
    echo "$answer<BR>";
    $answer = 5;
    echo ($answer++)." ":
    echo $answer;
?>



Assign an integer value this way

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



Calculating a date interval with epoch timestamps

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



Calculating Discount

<HTML>
<BODY>
<H1><CENTER>Calculating Discount</CENTER></H1>
<?php
  $price = 200;
  $qty = 10;
  echo "<H4>Original price is $price</H4>";
  if ($qty =10) {
      $discount=10;
  }elseif ($qty <=20) {
      $discount=15;
  }elseif (qty <=30) {
      $discount=20;
  }elseif($qty <=40){
      $discount=25;
  }
  echo "<H4>Discount is $discount percent</H4>";
  echo "<H4>Price after discount is ", (($price * (100 - $discount)/  100)), "</H4>";
?>
</BODY>
</HTML>



Divide the two values and print the result

<?
$a = 85;
$b = 24;
echo "<P>Original value of \$a is $a and \$b is
$b</P>";
$c = $a / $b;
echo "<P>Divided \$a by \$b and got $c</P>";
?>



Integer calculation

 
$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";



Integers can be specified using any of the following syntaxes

 
<?
$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)
?>



Integer value compare with equal sign

<?
$a=100;
$b=true;
echo "a = $a<br>";
echo "b = $b<br>";
if($a==$b) 
   echo "a "is equal to" b!"; ?>



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

 
<?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"; 
?>



Int value overflow

<?
$too_big = 111;
for ($count = 0; $count < 5; $count++) {
    $too_big = 1000 * $too_big;
    print("Is $too_big still an integer?<BR>");
}
?>



Multiply the two values and print the result

<?
$a = 85;
$b = 24;
echo "<P>Original value of \$a is $a and \$b is
$b</P>";

$c = $a * $b;
echo "<P>Multiplied \$a and \$b and got $c</P>";

?>



Storing Integers in PHP

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



Subtracted an Integer value

<?
$origVar = 100;
$origVar -= 12;
echo "<P>Subtracted a value, now it"s $origVar</P>";
?>



Subtract the two values and print the result

<?
$a = 85;
$b = 24;
echo "<P>Original value of \$a is $a and \$b is
$b</P>";
$c = $a - $b;
echo "<P>Subtracted \$b from \$a and got $c</P>";

?>



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

 
//To assign the number 68, you would use this:
    $hexnum = 0x44;
    print $hexnum;



Using typecasting to convert numbers to strings

 
<?php 
    $i = 123456; 
    $f = 98765.567; 
    
    echo "\$i = " . (string)$i . "\n"; 
    echo "\$f = " . (string)$f . "\n"; 
?>