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:

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:

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: `` 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: `` 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: `` 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: `` 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: ``` ``` This would produce: 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: ``` ``` This would produce: 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: ``` ``` This would produce: 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: ``` Geometry: Rectangle

Geometry: Rectangle

Width:
Height:
Perimeter:
Area:
"; ?> ``` This would produce: 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: ``` Geometry: Rectangle

Geometry: Rectangle

"; \$width = htmlspecialchars(\$_POST['txtWidth']); \$height = htmlspecialchars(\$_POST['txtHeight']); \$perimeter = (\$width + \$height) * 2.00; \$area = \$width * \$height; echo "
Width:
Height:
Perimeter:
Area:
"; ?> ``` This would produce: 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: ``` Brokerage Company

Brokerage Company

Number of Shares:
Price per Share
Principal:
Commission:
Total Investment:
"; ?> ``` Here is an example of using the webpage: To convert a value to the corresponding floating-point number, precede it with (real), (float), or (double). Here are examples: ``` Rectangular Parallelepiped

Rectangular Parallelepiped

Length:
Width:
Height:
Total Area:
Volume:
"; ?> ``` Here is an example of using the webpage: