Home

Introduction to Numeric Values

Natural Numbers: Integers

Introduction

An integer is a natural number as 0, 1, 2, 3, 4, 5, 6, etc, including their negative equivalents. To store such a value in the computer memory, declare a variable. Here is an example:

<?php $number ?>

Initializing a Numeric Variable

If you declare a numeric variable but don't initialize it, its default value is 0. Otherwise, to initialize an integral variable, assign it a number that include no symbol other than the digits. Here is the same example we saw already:

<?php $number = 28 ?>

If you want to indicate that the number is negative (less than 0), you must precede it with -. Here is an example:

<?php $number = -1684 ?>

A large natural number can also be expressed in terms of thousands. In this case, you can write a digit, followed by e or E and a value that represents the number of 0s. Here is an example:

<?php $budget = 2e7 ?>

In this case, this is a natural number made of 2 followed by 7 zeros. A natural number can be expressed using:

  • Binary Format: This system uses 2 digits as 0 and 1 that follow each other. In PHP, the value must start with 0b follow by a combination os 0s and 1s
  • Octal Format: This system uses 8 digits in a combination of 0, 1, 2, 3, 4, 5, 6, or 7. The number must start with 0 (zero)
  • Decimal Format: This system uses 10 digits as 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 in a combination of your choice
  • Hexadecimal Format: This system uses 16 symbols as 0, 1, 2, 3, 4, 5, 6, 7, 9, a (or A), b (or B), c (or C), d (or D), e (or E), and f (or F). The value must start with 0x or 0X (zero and x or X) followed by those symbols in the combination that represents the desired value

Introduction to Value Casting: Integral Conversion

Besides the normal way to provide a number, PHP allows you to specify a number as a string in double-quotes. Here is an example:

<?php $number = "19" ?>

In this case, the variable is considered as holding an integer. If such a variable is involved in an arithmetic operation, the conversion is performed automatically. Here is an example:

<?php
    $number = "19";
    $something = "44";

    $result = $number + $something;
    
    echo "Number: ";
    echo $result;
?>

This would produce:

Integer Conversion

Casting is the ability to convert a value from one type to another. To do this, include the desired type in parentheses and precede it with the value to convert.

Based on value casting, to convert a value to an integer, precede the value with (int) or (integer). Here are examples:

<?php
    $number = "19";
    $something = "44";

    $nbr1 = (int)$number;
    $nbr2 = (integer)$something;

    $result = $nbr1 + $nbr2;
    
    echo "Number: ";
    echo $result;
?>

This would produce:

Integer Conversion

On the other hand, to convert an integer to a string, precede it with (string)

 
 
 

Decimals: Floating-Point Numbers

Introduction

A floating-point number, also referred to as real, is a number that includes an integral part and sometimes a decimal or fractional part. In US English, both parts are separated by a period, but there are many more options than that. To use a floating-point number in your script, declare a variable and assign the number to it. You have many options.

The classic way to represent a real number is to provide the integral section, a period, the fractional section. Both parts are made of digits only. Here is an example:

<?php $hourlysalary = 24.75 ?>

A number that is between 0 and 1 can be represented with 0, a period, and the fractional part, such as 0.92. In this case, you can omit the 0. Here is an example:

<?php $factor = .92 ?>

A real number can also be expressed as an exponent. In this case, an exponentional part is considered in a certain power. The number is followed by e or E and a number that represents the power. Here is an example:

<?php $number = 14.66e5 ?>

A natural or a real number can be expressed as an increment of thousands. The number is made of two parts: the natural or decimal section and the exponent part. Consider the following example:

<?php $budget = 2.4858e7 ?>

If the number on the left of e or E has fewer digits than the exponent part, the number would be used and completed with 0s up to the exponent. The value would correspond to a regular decimal number (natural or floating-point). Here is an example:

<?php $budget = 2.4858e7 ?>
<?php
    echo "Budget: ";
    echo $budget
?>

This would produce:

Decimals: Floating-Point Numbers

If the number on the left of e or E has more digits than the exponent part, the number would be used based on the exponent, then a fractional part would be used to complete the exponent. The value would correspond to a floating-point number. Here is an example:

<?php $budget = 54.4803785e5 ?>
<?php
    echo "Budget: ";
    echo $budget
?>

This would produce:

Decimals: Floating-Point Numbers

Numeric Conversion

PHP supports various types of numbers, including integers and floats. The conversion is usually smooth from one type to another. When it comes to strings, the representation is also smooth. For example, you can reprsent a number as a string, in which case the number would be provided in double-quotes. When such a string is involved in an arithmetic operation, the PHP parser would automatically retrieve the number through an easy conversion. Consider the following example:

<?php
    $hourSalary = "24.48";
    $timeWorked = "38.50";
    $weeklySalary = $hourSalary * $timeWorked;
?>

<?php
    echo "Weekly Salary: ";
    echo $weeklySalary
?>

This would produce:

Numeric Conversion

This technique works also for exponent-based formats, that is, a number that has an exponentiation with the letter e or E.

PHP and Numbers

Numbers from Web Controls

Web controls deal with two categories of numbers. Some web controls use some internal numbers for their functionalities. For example, as mentioned in the previous lesson, the size of a combo box is represented as its number of items. In the same way, the numbers of columns or rows of a text area are integers. On the other hand, you may create a form that expect visitors to type some numbers. Consider the following example:

<!DOCTYPE html>
<html>
<head>
<title>Geometry: Rectangle</title>
<style type="text/css">

#formulation
{
    margin: auto;
    width:  200pt;
}
#main-title
{
    font-size:   18pt;
    font-weight: bold;
    font-family: Georgia, "Times New Roman", Times, serif;
}
</style>
</head>
<body>

<?php
echo "<form name='frmCalculation' method='post'>
      <div id='formulation'>
      <p id='main-title'>Geometry: Rectangle</p>

      <table border='4'>
        <tr>
          <td>Width:</td>
          <td>
	    <input name='txtWidth' type='text' style='width: 55px' value='' /></td>
        </tr>
        <tr>
          <td>Height:</td>
          <td>
	    <input name='txtHeight' type='text' style='width: 55px' value='' />
	    <input name='Submit' type='submit' value='Calculate' />
          </td>
        </tr>
        <tr>
          <td>Perimeter:</td>
          <td><input name='txtPerimeter' type='text' value='' /></td>
        </tr>
        <tr>
          <td>Area:</td>
          <td><input name='txtArea' type='text' value='' /></td>
        </tr>
      </table>
    </div>
    </form>";
?>

</body>

</html>

This would produce:

Numbers from Web Controls

When a visitor types a number in a text box, it is primarily considered a string. Still, if then control is holding a value and you involve it in an arithmetical operation, the parser will automatically and internally convert it to the appropriate value (the visitor must type an appropriate value; you can write code that would check whether the value is valid). Here are examples:

<!DOCTYPE html>
<html>
<head>
<title>Geometry: Rectangle</title>
<style type="text/css">

#formulation
{
    margin: auto;
    width:  200pt;
}
#main-title
{
    font-size:   18pt;
    font-weight: bold;
    font-family: Georgia, "Times New Roman", Times, serif;
}
</style>
</head>
<body>

<?php
echo "<form name='frmCalculation' method='post'>
      <div id='formulation'>
      <p id='main-title'>Geometry: Rectangle</p>";

      $width  = htmlspecialchars($_POST['txtWidth']);
      $height = htmlspecialchars($_POST['txtHeight']);
      $perimeter = ($width + $height) * 2.00;
      $area      = $width * $height;

echo "      <table border='4'>
        <tr>
          <td>Width:</td>
          <td>
	    <input name='txtWidth' type='text' style='width: 55px' value='";
echo $width;
echo "' /></td>
        </tr>
        <tr>
          <td>Height:</td>
          <td>
	    <input name='txtHeight' type='text' style='width: 55px' value='";
echo $height;
echo "' />
	    <input name='Submit' type='submit' value='Calculate' />
          </td>
        </tr>
        <tr>
          <td>Perimeter:</td>
          <td><input name='txtPerimeter' type='text' value='";
echo $perimeter;
echo"' /></td>
        </tr>
        <tr>
          <td>Area:</td>
          <td><input name='txtArea' type='text' value='";
echo $area;
echo "' /></td>
        </tr>
      </table>
    </div>
    </form>";
?>

</body>

</html>

This would produce:

Numbers from Web Controls

Numbers from Web Controls

Converting a Value to a Number

Converting a Value to a Number

If you don't want to rely on the automatic conversion, especially if there is a particular conversion you want to perform, you can indicate the type of value you want. PHP supports various types of values and numbers.

As mentioned previously, to convert a value to an intger, precede it with either (int) or (integer). In this case, a floating-point number would be converted to an integer. Here is an example:

<!DOCTYPE html>
<html>

<head>
<title>Brokerage Company</title>
<style type="text/css">
#container
{
	width: 350px;
	margin: auto;
}
#main-title
{
	font-size: 24pt;
	font-family: Georgia, "Times New Roman", Times, serif
}
</style>
</head>

<body>
<?php
    $numberOfShares = (int)htmlspecialchars($_POST['txtNumberOfShares']);
    $pricePerShare = (integer)htmlspecialchars($_POST['txtPricePerShare']);

    $principal = $numberOfShares * $pricePerShare;
    $commission = 60.00 + ($principal * 0.0028);
    $totalInvestment = $principal + $commission;

    echo "<div id='container'>
          <form method='post'>
          <p id='main-title'>Brokerage Company</p>

          <table style='width: 320px'>
            <tr>
              <td>Number of Shares:</td>
              <td><input name='txtNumberOfShares' type='text' 
                         style='width: 80px' value='$numberOfShares' /></td>
            </tr>
            <tr>
              <td>Price per Share</td>
              <td>
                <input name='txtPricePerShare' type='text' 
                       style='width: 80px' value='$pricePerShare' />
                <input name='btnCalculate' type='submit' value='Calculate' />
              </td>
           </tr>
           <tr>
             <td>Principal:</td>
             <td><input name='txtPrincipal' type='text' value='$principal' /></td>
           </tr>
           <tr>
             <td>Commission:</td>
             <td><input name='txtCommission' type='text' value='$commission' /></td>
           </tr>
           <tr>
             <td>Total Investment:</td>
             <td><input name='txtTotalInvestment' 
                        type='text' value='$totalInvestment' /></td>
           </tr>
         </table>
         </form>
         </div>";
?>
</body>

</html>

Here is an example of using the webpage:

Converting a Value to a Number

To convert a value to the corresponding floating-point number, precede it with (real), (float), or (double). Here are examples:

<!DOCTYPE html>

<html>
<head>
<title>Rectangular Parallelepiped</title>
<style type="text/css">
#container
{
	width: 350px;
	margin: auto;
}
#main-title
{
	font-size: 24pt;
	font-family: Georgia, "Times New Roman", Times, serif
}
</style>
</head>

<body>
<?php
    $length = (real)htmlspecialchars($_POST['txtLength']);
    $width  = (float)htmlspecialchars($_POST['txtWidth']);
    $height = (double)htmlspecialchars($_POST['txtHeight']);

    $area   = ($length * $width  * 2.00) +
              ($length * $height * 2.00) +
              ($height * $width  * 2.00);
    $volume = $length * $width * $height;

    echo "<div id='container'>
          <form method='post'>
          <p id='main-title'>Rectangular Parallelepiped</p>

          <table style='width: 320px'>
            <tr>
              <td>Length:</td>
              <td><input name='txtLength' type='text' 
                         style='width: 80px' value='$length' /></td>
            </tr>
            <tr>
              <td>Width:</td>
              <td>
                <input name='txtWidth' type='text' 
                       style='width: 80px' value='$width' />
              </td>
           </tr>
           <tr>
             <td>Height:</td>
             <td><input name='txtHeight' type='text'  
                        style='width: 80px' value='$height' />
                 <input name='btnCalculate' type='submit' value='Calculate' />
             </td>
           </tr>
           <tr>
             <td>Total Area:</td>
             <td><input name='txtArea' 
                        type='text' value='$area' /></td>
           </tr>
           <tr>
             <td>Volume:</td>
             <td><input name='txtVolume' type='text' value='$volume' /></td>
           </tr>
         </table>
         </form>
         </div>";
?>
</body>
</html>

Here is an example of using the webpage:

Converting a Value to a Number

Converting a Value to a Number

Converting a Value to a Number

 
   
 

Previous Copyright © 2015, FunctionX Next