Featured Posts

Finding the Current Date and Time in PHP Code

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 09-12-2009

0

Use strftime( ) or date( ) for a formatted time string

Finding the current date and time
<?php
print strftime(‘%c’);
print “\n”;
print date(‘r’);
?>
Output
Wed May 10 18:29:59 2006
Wed, 10 May 2006 18:29:59 -0400

Finding the month, day, and year
<?php
$a = getdate();
printf(‘%s %d, %d’,$a['month'],$a['mday'],$a['year']);
?>

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

How to Calculate Exponents in PHP

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 09-12-2009

1

To raise e to a power, use exp( ):
$exp = exp(2);// 7.3890560989307

To raise it to any power, use pow( ):
$exp = pow( 2, M_E);// 6.5808859910179

$pow = pow( 2, 10); // 1024
$pow = pow( 2, -2); // 0.25
$pow = pow( 2, 2.5);// 5.6568542494924

$pow = pow(-2, 10); // 1024
$pow = pow( 2, -2); // 0.25
$pow = pow(-2, -2.5); // NAN (Error: Not a Number)
The built-in constant M_E is an approximation of the value of e. It equals 2.7182818284590452354. So exp($n) and pow(M_E, $n) are identical.
It’s easy to create very large numbers using exp( ) and pow( ); if you outgrow PHP’s maximum size (almost 1.8e308), how to use the arbitrary precision functions. With exp( ) and pow( ), PHP returns INF (infinity) if the result is too large and NAN (not a number) on an error.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

How to use For Loop in PHP

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 09-12-2009

1

Use a for loop:
<?php
for ($i = $start; $i <= $end; $i++) {
plot_point($i);
}
?>
You can increment using values other than 1. For example:
<?php
for ($i = $start; $i <= $end; $i += $increment) {
plot_point($i);
}
?>
If you want to preserve the numbers for use beyond iteration,
use the range( ) method:
<?php
$range = range($start, $end);
?>
Loops like this are common. For instance, you could be plotting
a function and need to calculate the results for multiple points
on the graph. Or you could be a student counting down the number
of seconds until the end of school.
The for loop method uses a single integer and you have great
control over the loop, because you can increment and decrement
$i freely. Also, you can modify $i from inside the loop.
In the last example in the Solution, range( ) returns an array
with values from $start to $end. The advantage of using range( )
is its brevity, but this technique has a few disadvantages. For
one, a large array can take up unnecessary memory. Also, you're
forced to increment the series one number at a time, so you can't
loop through a series of even integers, for example.
It's valid for $start to be larger than $end. In this case,
the numbers returned by range( ) are in descending order. Also,
you can use it to retrieve character sequences:
<?php
print_r(range('l', 'p'));
?>
Array
(
[0] => l
[1] => m
[2] => n
[3] => o
[4] => p
)
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Syntax to Rounding Floating-Point Numbers in PHP

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 09-12-2009

1

To round a number to the closest integer, use round( ) :
$number = round(2.4); // $number = 2

To round up, use ceil( ) :
$number = ceil(2.4);// $number = 3

To round down, use floor( ) :
$number = floor(2.4); // $number = 2
If a number falls exactly between two integers, PHP rounds away from 0:
$number = round(2.5); //3

$number = round(-2.5);// -3
To keep a set number of digits after the decimal point, round( ) accepts an optional precision argument. For example, perhaps you are calculating the total price for the items in a user’s shopping cart:
<?php
$cart = 54.23;
$tax = $cart * .05;
$total = $cart + $tax; // $total = 56.9415

$final = round($total, 2); // $final = 56.94
?>

To round a number down, use the floor( ) function:
$number = floor( 2.1);//2
$number = floor( 2.9);//2
$number = floor(-2.1);// -3
$number = floor(-2.9);// -3

While to round up, use the ceil( ) function:
$number = ceil( 2.1);//3
$number = ceil( 2.9);//3
$number = ceil(-2.1);// -2
$number = ceil(-2.9);// -2

These two functions are named because when you’re rounding down, you’re rounding “toward the floor,” and when you’re rounding up, you’re rounding “toward the ceiling.”

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Variable contain valid number using PHP

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 08-12-2009

0

How to check whether variable contains valid number using PHP

You want to ensure that a variable contains a number, even if it’s typed as a string. Alternatively, you want to check if a variable is not only a number, but is also specifically typed as a one.

Use is_numeric( ) to discover whether a variable contains a number:

<?php
if (is_numeric(5))          { /* true  */ }
if (is_numeric(’5′))        { /* true  */ }
if (is_numeric(“05″))       { /* true  */ }
if (is_numeric(‘five’))     { /* false */ }

if (is_numeric(0xDECAFBAD)) { /* true  */ }
if (is_numeric(“10e200″))   { /* true  */ }
?>

Numbers come in all shapes and sizes. You cannot assume that something is a number simply because it only contains the characters 0 through 9. What about decimal points, or negative signs? You can’t simply add them into the mix because the negative must come at the front, and you can only have one decimal point. And then there’s hexadecimal numbers and scientific notation.

Instead of rolling your own function, use is_numeric( ) to check whether a variable holds something that’s either an actual number (as in it’s typed as an integer or floating point), or it’s a string containing characters that can be translated into a number.

There’s an actual difference here. Technically, the integer 5 and the string 5 aren’t the same in PHP. However, most of the time you won’t actually be concerned about the distinction, which is why the behavior of is_numeric( ) is useful.

Helpfully, is_numeric( ) properly parses decimal numbers, such as 5.1; however, numbers with thousands separators, such as 5,100, cause is_numeric( ) to return false.

To strip the thousands separators from your number before calling is_numeric( ), use str_replace( ):

<?php
is_numeric(str_replace($number, ‘,’, ”));
?>

To check if your number is a specific type, there are a variety of related functions with self-explanatory names: is_float( ) (or is_double( ) or is_real( ); they’re all the same) and is_int( ) (or is_integer( ) or is_long( )).

To validate input data, use the techniques from Recipe 9.3 instead of is_numeric( ). That recipe describes how to check for positive or negative integers, decimal numbers, and a handful of other formats.

VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

How to Store Binary Data in Strings using PHP

Posted by admin | Posted in PHP, Software Programs, Theory Subjects | Posted on 08-12-2009

0

You want to parse a string that contains values encoded as a binary structure or encode values into a string. For example, you want to store numbers in their binary representation instead of as sequences of ASCII characters.

1.16.2. Solution

Use pack( ) to store binary data in a string:

<?php
$packed = pack('S4',1974,106,28225,32725);
?>

Use unpack( ) to extract binary data from a string:

<?php
$nums = unpack('S4',$packed);
?>

1.16.3. Discussion

The first argument to pack( ) is a format string that describes how to encode the data that’s passed in the rest of the arguments. The format string S4 tells pack( ) to produce four unsigned short 16-bit numbers in machine byte order from its input data. Given 1974, 106, 28225, and 32725 as input on a little-endian machine, this returns eight bytes: 182, 7, 106, 0, 65, 110, 213, and 127. Each two-byte pair corresponds to one of the input numbers: 7 * 256 + 182 is 1974; 0 * 256 + 106 is 106; 110 * 256 + 65 = 28225; 127 * 256 + 213 = 32725.

The first argument to unpack( ) is also a format string, and the second argument is the data to decode. Passing a format string of S4, the eight-byte sequence that pack( ) produced returns a four-element array of the original numbers. print_r($nums) prints:

Array
(
    [1] => 1974
    [2] => 106
    [3] => 28225
    [4] => 32725
)

In unpack( ), format characters and their count can be followed by a string to be used as an array key. For example:

<?php
$nums = unpack('S4num',$packed);
print_r($nums);
?>

This prints:

Array
(
    [num1] => 1974
    [num2] => 106
    [num3] => 28225
    [num4] => 32725
)

Multiple format characters must be separated with / in unpack( ):

<?php
$nums = unpack('S1a/S1b/S1c/S1d',$packed);
print_r($nums);
?>

This prints:

Array
(
    [a] => 1974
    [b] => 106
    [c] => 28225
    [d] => 32725
)
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)