Index
    Preface
      What This Book Is About
      What You Need to Know
      How This Book Is Organized
      How to Use This Book
      Conventions Used in This Book
      Using Code Examples
      How to Contact Us
      Web Site and Code Examples
      Acknowledgments
      Chapter 1.  Database Applications and the Web
      Section 1.1.  The Web
      Section 1.2.  Three-Tier Architectures
      Chapter 2.  The PHP Scripting Language
      Section 2.1.  Introducing PHP
      Section 2.2.  Conditions and Branches
      Section 2.3.  Loops
      Section 2.4.  Functions
      Section 2.5.  Working with Types
      Section 2.6.  User-Defined Functions
      Section 2.7.  A Working Example
      Chapter 3.  Arrays, Strings, and Advanced Data Manipulation in PHP
      Section 3.1.  Arrays
      Section 3.2.  Strings
      Section 3.3.  Regular Expressions
      Section 3.4.  Dates and Times
      Section 3.5.  Integers and Floats
      Chapter 4.  Introduction to Object-Oriented Programming with PHP 5
      Section 4.1.  Classes and Objects
      Section 4.2.  Inheritance
      Section 4.3.  Throwing and Catching Exceptions
      Chapter 5.  SQL and MySQL
      Section 5.1.  Database Basics
      Section 5.2.  MySQL Command Interpreter
      Section 5.3.  Managing Databases and Tables
      Section 5.4.  Inserting, Updating, and Deleting Data
      Section 5.5.  Querying with SQL SELECT
      Section 5.6.  Join Queries
      Section 5.7.  Case Study: Adding a New Wine
      Chapter 6.  Querying Web Databases
      Section 6.1.  Querying a MySQL Database Using PHP
      Section 6.2.  Processing User Input
      Section 6.3.  MySQL Function Reference
      Chapter 7.  PEAR
      Section 7.1.  Overview
      Section 7.2.  Core Components
      Section 7.3.  Packages
      Chapter 8.  Writing to Web Databases
      Section 8.1.  Database Inserts, Updates, and Deletes
      Section 8.2.  Issues in Writing Data to Databases
      Chapter 9.  Validation with PHP and JavaScript
      Section 9.1.  Validation and Error Reporting Principles
      Section 9.2.  Server-Side Validation with PHP
      Section 9.3.  JavaScript and Client-Side Validation
      Chapter 10.  Sessions
      Section 10.1.  Introducing Session Management
      Section 10.2.  PHP Session Management
      Section 10.3.  Case Study: Using Sessions in Validation
      Section 10.4.  When to Use Sessions
      Section 10.5.  PHP Session API and Configuration
      Chapter 11.  Authentication and Security
      Section 11.1.  HTTP Authentication
      Section 11.2.  HTTP Authentication with PHP
      Section 11.3.  Form-Based Authentication
      Section 11.4.  Protecting Data on the Web
      Chapter 12.  Errors, Debugging, and Deployment
      Section 12.1.  Errors
      Section 12.2.  Common Programming Errors
      Section 12.3.  Custom Error Handlers
      Chapter 13.  Reporting
      Section 13.1.  Creating a Report
      Section 13.2.  Producing PDF
      Section 13.3.  PDF-PHP Reference
      Chapter 14.  Advanced Features of Object-Oriented Programming in PHP 5
      Section 14.1.  Working with Class Hierarchies
      Section 14.2.  Class Type Hints
      Section 14.3.  Abstract Classes and Interfaces
      Section 14.4.  Freight Calculator Example
      Chapter 15.  Advanced SQL
      Section 15.1.  Exploring with SHOW
      Section 15.2.  Advanced Querying
      Section 15.3.  Manipulating Data and Databases
      Section 15.4.  Functions
      Section 15.5.  Automating Querying
      Section 15.6.  Table Types
      Section 15.7.  Backup and Recovery
      Section 15.8.  Managing Users and Privileges
      Section 15.9.  Tuning MySQL
      Chapter 16.  Hugh and Dave's Online Wines:A Case Study
      Section 16.1.  Functional and System Requirements
      Section 16.2.  Application Overview
      Section 16.3.  Common Components
      Chapter 17.  Managing Customers
      Section 17.1.  Code Overview
      Section 17.2.  Customer Validation
      Section 17.3.  The Customer Form
      Chapter 18.  The Shopping Cart
      Section 18.1.  Code Overview
      Section 18.2.  The Winestore Home Page
      Section 18.3.  The Shopping Cart Implementation
      Chapter 19.  Ordering and Shipping at the Online Winestore
      Section 19.1.  Code Overview
      Section 19.2.  Credit Card and Shipping Instructions
      Section 19.3.  Finalizing Orders
      Section 19.4.  HTML and Email Receipts
      Chapter 20.  Searching and Authentication in the Online Winestore
      Section 20.1.  Code Overview
      Section 20.2.  Searching and Browsing
      Section 20.3.  Authentication
      Appendix A.  Linux Installation Guide
      Section A.1.  Finding Out What's Installed
      Section A.2.  Installation Overview
      Section A.3.  Installing MySQL
      Section A.4.  Installing Apache
      Section A.5.  Installing PHP
      Section A.6.  What's Needed for This Book
      Appendix B.  Microsoft Windows Installation Guide
      Section B.1.  Installation Overview
      Section B.2.  Installing with EasyPHP
      Section B.3.  What's Needed for This Book
      Appendix C.  Mac OS X Installation Guide
      Section C.1.  Getting Started
      Section C.2.  Installing MySQL
      Section C.3.  Setting Up Apache and PHP
      Section C.4.  What's Needed for This Book
      Appendix D.  Web Protocols
      Section D.1.  Network Basics
      Section D.2.  Hypertext Transfer Protocol
      Appendix E.  Modeling and Designing Relational Databases
      Section E.1.  The Relational Model
      Section E.2.  Entity-Relationship Modeling
      Appendix F.  Managing Sessions in theDatabase Tier
      Section F.1.  Using a Database to Keep State
      Section F.2.  PHP Session Management
      Section F.3.  MySQL Session Store
      Appendix G.  Resources
      Section G.1.  Client Tier Resources
      Section G.2.  Middle-Tier Resources
      Section G.3.  Database Tier Resources
      Section G.4.  Security and Cryptography Resources
      Appendix H.  The Improved MySQL Library
      Section H.1.  New Features
      Section H.2.  Getting Started
      Section H.3.  Using the New Features
    Colophon
    Copyright



 

Previous Section  < Day Day Up >  Next Section

3.5 Integers and Floats

As we discussed in Chapter 2, PHP supports both integer and floating-point numbers. PHP stores integers as a 32-bit signed word, providing a range of integers from -2147483647 to +2147483647. PHP automatically converts numbers that overflow out of this range to floats. You can see this behavior by adding one to the largest integer value:

// Largest positive integer (for a 32 bit signed integer)

$variable =  2147483647;



// prints int(2147483647)

var_dump($variable);



$variable++;



// prints float(2147483648)

var_dump($variable);

Floating-point numbers can store a wide range of values, both very large and very small, by storing a mantissa and an exponent. However a floating-point number can't precisely represent all numbers—for example, the fraction 2/3—and some precision can be lost.

Integers can be represented in a decimal, hexadecimal, or octal notation:

$var = 42;        // a positive integer

$var = -186;      // a negative integer

$var = 0654;      // 428 expressed as an octal number

$var = 0xf7;      // 247 expressed as a hexadecimal number

Floating-point numbers can represented in a decimal or exponential notation:

$var = 42.0;      // a positive float

$var = -186.123;  // a negative float

$var = 1.2e65;    // a very big number

$var = 10e-75;    // a very small number

Apart from the basic operators +, -, /, *, and %, PHP provides the usual array of mathematical library functions. In this section, we present some of the library functions that are used with integer and float numbers.

3.5.1 Absolute Value

The absolute value of an integer or a float can be found with the abs( ) function:

integer abs(integer number)
float abs(float number)

The following examples show the result of abs( ) on integers and floats:

print abs(-1);       // prints 1

print abs(1);        // prints 1

print abs(-145.89);  // prints 145.89

print abs(145.89);   // prints 145.89

3.5.2 Ceiling and Floor

The ceil( ) and floor( ) functions return the integer value above and below a fractional value, respectively:

float ceil(float value)
float floor(float value)

The return type is a float because an integer may not be able to represent the result when a large value is passed as an argument. Consider the following examples:

print ceil(27.3);   // prints 28

print floor(27.3);  // prints 27

3.5.3 Rounding

The round( ) function uses 4/5 rounding rules to round up or down a value to a given precision:

float round(float value [, integer precision])

By default, rounding is to zero decimal places, but the precision can be specified with the optional precision argument. The 4/5 rounding rules determine if a number is rounded up or down based on the digits that are lost due to the rounding precision. For example, 10.4 rounds down to 10, and 10.5 rounds up to 11. Specifying a negative precision rounds a value to a magnitude greater than zero, for example a precision of -3 rounds a value to the nearest thousand. The following examples show rounding at various precisions:

print round(10.4);           // prints 10

print round(10.5);           // prints 11

print round(2.40964, 3);     // prints 2.410

print round(567234.56, -3);  // prints 567000

print round(567234.56, -4);  // prints 570000

3.5.4 Number Systems

PHP provides the following functions that convert numbers between integer decimal and the commonly used number systems, binary, octal, and hexadecimal:

string decbin(integer number)
integer bindec (string binarystring)
string dechex(integer number)
integer hexdec(string hexstring)
string decoct(integer number)
integer octdec(string octalstring)

The decimal numbers are always treated as integers, and the numbers in the other systems are treated as strings. Here are some examples:

print decbin(45);        // prints "101101"

print bindec("1001011"); // prints 75

print dechex(45);        // prints "2D"

print hexdec("5a7b");    // prints 23163

print decoct(45);        // prints "55"

print octdec("777");     // prints 511

It is possible to represent binary, octal, and hexadecimal numbers that are bigger than can be held in a 32-bit integer. The results of such conversions automatically overflow to a float value. For example:

// $a is an integer assigned the largest possible value

$a = hexdec("7fffffff");



// $a is a float

$a = hexdec("80000000");

3.5.5 Basic Trigonometry Functions

PHP supports the basic set of trigonometry functions listed in Table 3-5.

Table 3-5. Trigonometry functions supported by PHP

Function

Description

float sin(float arg)

Sine of arg in radians

float cos(float arg)

Cosine of arg in radians

float tan(float arg)

Tangent of arg in radians

float asin(float arg)

Arc sine of arg in radians

float acos(float arg)

Arc cosine of arg in radians

float atan(float arg)

Arc tangent of arg in radians

float atan2(float y, float x)

Arc tangent of x/y where the sign of both arguments determines the quadrant of the result

float pi( )

Returns the value 3.1415926535898

float deg2rad(float arg)

Converts arg degrees to radians

float rad2deg(float arg)

Converts arg radians to degrees


3.5.6 Powers and Logs

The PHP mathematical library includes the exponential and logarithmic functions listed in Table 3-6.

Table 3-6. Exponential and logarithmic functions

Function

Description

float exp(float arg)

e to the power of arg

float pow(float base, number exp)

Exponential expression base to the power of exp

float sqrt(float arg)

Square root of arg

float log(float arg [, float base])

Natural logarithm of arg, unless a base is specified, in which case the function returns log(arg)/log(base)

float log10(float arg)

Base-10 logarithm of arg


3.5.7 Testing Number Results

Many of the functions described in this section can return values that are undefined, or are too big or small to hold in a floating point number. PHP provides three functions that can be used to test numeric results before they cause problems later in a script:

boolean is_nan(float val)
boolean is_infinite(float val)
boolean is_finite(float val)

The function is_nan( ) tests the expression val and returns true if val is not a number. For example, the square root of a negative number is not a real number. is_finite( ) returns true if the number val can be represented as a valid float, and is_infinite( ) returns true if val can't. Here are some examples:

// square root of a negative number

$a = -1;

$result = sqrt($a);



print $result;    // prints -1.#IND



// Test if not a number

if (is_nan($result))

    print "Result not defined"; // prints

else

    print "Square root of {$a} = {$result}";

3.5.8 Random Number Generation

PHP provides the function rand( ), which returns values from a generated sequence of pseudo-random numbers. The sequence generated by rand( ) is pseudo random because the algorithm used appears to have random behavior but isn't truly random. The function rand( ) can be called in one of two ways:

integer rand( )
integer rand(integer min, integer max)

When called with no arguments, rand( ) returns a random number between 0 and the value returned by getrandmax( ) . When rand( ) is called with two arguments, the min and max values, the returned number is a random number between min and max. Consider an example:

// Generate some random numbers

print rand( );      // between 0 and getmaxrand( )

print rand(1, 6);  // between 1 and 6 (inclusive)

Prior to PHP 4.2.0, you needed to seed the random number generator with a call to srand( ) before the first use of rand( ), otherwise the function returns the same numbers each time a script is called. Since 4.2.0, the call to srand( ) is not required, however you can reliably reproduce a random sequence by calling srand( ) with the same argument at the start of the script. The following example reliably prints the same sequence of numbers each time it is called:

srand(123456);



// Prints six random numbers

for ($i=0; $i<6; $i++)

    print rand( ) . "   ";

    Previous Section  < Day Day Up >  Next Section







    Copyright © 2010 | Domen maybe sale - bye this domen