php - bcompiler_read

Description

bool bcompiler_read ( resource $filehandle )
Reads data from a open file handle and creates classes from the bytecodes.

Parameters


filehandle
A file handle as returned by fopen().

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 340. bcompiler_read() example
<?php

$fh
= fopen("/tmp/example","r");

bcompiler_read($fh);

fclose($fh);print_r(get_defined_classes());
?>

php - bcompiler_parse_class

Description

bool bcompiler_parse_class ( string $class, string $callback )
Reads the bytecodes of a class and calls back to a user function.

Parameters



class
The class name, as a string.
callback

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 339. bcompiler_parse_class() example
<?php
function readByteCodes($data) {
 
print_r($data);

}
bcompiler_parse_class("DB","readByteCodes");
?>

php - bcompiler_load_exe

Description

bool bcompiler_load_exe ( string $filename )
Reads data from a bcompiler exe file and creates classes from the bytecodes.

Parameters


filename
The exe file path, as a string.

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 337. bcompiler_load() example
<?php

bcompiler_load_exe
("/tmp/example.exe"); 


print_r(get_defined_classes());

?>

php - bcompiler_load

Description

bool bcompiler_load ( string $filename )
Reads data from a bzcompressed file and creates classes from the bytecodes.

Parameters



filename
The bzcompressed file path, as a string.

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 338. bcompiler_load() example
 
<?php

bcompiler_load
("/tmp/example");
print_r(get_defined_classes());
?>

php - bcmul

Description

string bcmul ( string $left_operand, string $right_operand [, int $scale] )
Multiply the left_operand by the right_operand.

Parameters



left_operand
The left operand, as a string.
right_operand
The right operand, as a string.
scale
This optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale().

Return Values

Returns the result as a string.

Examples

Example 332. bcmul() example
<?php

echo bcmul('1.34747474747', '35', 3); // 47.161 
echo bcmul('2', '4'); // 8 
?>

php - bcmod

Description

string bcmod ( string $left_operand, string $modulus )
Get the modulus of the left_operand using modulus.

Parameters



left_operand
The left operand, as a string.
modulus
The modulus, as a string.

Return Values

Returns the modulus as a string, or NULL if modulus is 0.

Examples

Example 331. bcmod() example
 
<?php

echo bcmod('4', '2'); // 0 
echo bcmod('2', '4'); // 2

?>

php - BCMath Arbitrary Precision Mathematics Functions

Introduction

For arbitrary precision mathematics PHP offers the Binary Calculator which supports numbers of any size and precision, represented as strings.

Requirements

Since PHP 4.0.4, libbcmath is bundled with PHP. You don't need any external libraries for this extension.

Installation

These functions are only available if PHP was configured with --enable-bcmath. In PHP 3, these functions are only available if PHP was not configured with --disable-bcmath.
The windows version of PHP has built in support for this extension. You do not need to load any additional extension in order to use these functions.

Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

Table 22. BC math configuration options
Name Default Changeable Changelog
bcmath.scale "0" PHP_INI_ALL

For further details and definitions of the PHP_INI_* constants, see the Appendix I, php.ini directives. Here's a short explanation of the configuration directives.


bcmath.scale integer
Number of decimal digits for all bcmath functions. See also bcscale().

Resource Types

This extension has no resource types defined.

Predefined Constants

This extension has no constants defined.

php - bcdiv

Description

string bcdiv ( string $left_operand, string $right_operand [, int $scale] )
Divides the left_operand by the right_operand.

Parameters



left_operand
The left operand, as a string.
right_operand
The right operand, as a string.
scale
This optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale().

Return Values

Returns the result of the division as a string, or NULL if right_operand is 0.

Examples

Example 330. bcdiv() example
<?php
echo bcdiv('105', '6.55957', 3); // 16.007
?>

php - bccomp

Description

int bccomp ( string $left_operand, string $right_operand [, int $scale] )
Compares the left_operand to the right_operand and returns the result as an integer.

Parameters



left_operand
The left operand, as a string.
right_operand
The right operand, as a string.
scale
The optional scale parameter is used to set the number of digits after the decimal place which will be used in the comparison.

Return Values

Returns 0 if the two operands are equal, 1 if the left_operand is larger than the right_operand, -1 otherwise.

Examples

Example 329. bccomp() example
<?php
echo bccomp('1', '2') . "\n"; // -1echo bccomp('1.00001', '1', 3); // 0echo bccomp('1.00001', '1', 5); // 1
?>

php - bcadd

Description

string bcadd ( string $left_operand, string $right_operand [, int $scale] )
Sums left_operand and right_operand.

Parameters



left_operand
The left operand, as a string.
right_operand
The right operand, as a string.
scale
This optional parameter is used to set the number of digits after the decimal place in the result. You can also set the global default scale for all functions by using bcscale().

Return Values

The sum of the two operands, as a string.

Examples

Example 328. bcadd() example

<?php

$a
= '1.234';$b = '5';

echo
bcadd($a, $b); // 6echo bcadd($a, $b, 4); // 6.2340
?>

php - Basics

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.
Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: '[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
Note: For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 127 through 255 (0x7f-0xff).
Tip You may also want to take a look at the Appendix T, Userland Naming Guide.
For information on variable related functions, see the Variable Functions Reference.

<?php
$var
= 'Bob';$Var = 'Joe';
echo
"$var, $Var"; // outputs "Bob, Joe"
$4site = 'not yet'; // invalid; starts with a number 

$_4site = 'not yet'; // valid; starts with an underscore
$täyte = 'mansikka'; // valid; 'ä' is (Extended) ASCII 228. 
?>
In PHP 3, variables are always assigned by value. That is to say, when you assign an expression to a variable, the entire value of the original expression is copied into the destination variable. This means, for instance, that after assigning one variable's value to another, changing one of those variables will have no effect on the other. For more information on this kind of assignment, see the chapter on Expressions.
As of PHP 4, PHP offers another way to assign values to variables: assign by reference. This means that the new variable simply references (in other words, "becomes an alias for" or "points to") the original variable. Changes to the new variable affect the original, and vice versa.
To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable). For instance, the following code snippet outputs 'My name is Bob' twice:
<?php
$foo
= 'Bob'; // Assign the value 'Bob' to $foo

$bar = &$foo; // Reference $foo via $bar. 
$bar = "My name is $bar"; // Alter $bar...
echo $bar;
echo
$foo; // $foo is altered too.

?>
One important thing to note is that only named variables may be assigned by reference.
<?php
$foo
= 25;$bar = &$foo; // This is a valid assignment. 

$bar = &(24 * 7); // Invalid; references an unnamed expression.
function test()
{
return
25;
}
$bar = &test(); // Invalid. 

?>
It is not necessary to initialize variables in PHP however it is a very good practice. Uninitialized variables have a default value of their type - FALSE, zero, empty string or an empty array.

Example 12.1. Default values of uninitialized variables
<?php
echo ($unset_bool ? "true" : "false"); // false 
$unset_int += 25; // 0 + 25 => 25
echo $unset_string . "abc"; // "" . "abc" => "abc" 
$unset_array[3] = "def"; // array() + array(3 => "def") => array(3 => "def") 
?>

php - Basic syntax

Escaping from HTML

When PHP parses a file, it looks for opening and closing tags, which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows php to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser. Most of the time you will see php embedded in HTML documents, as in this example.
<p>This is going to be ignored.</p>
<?php echo 'While this is going to be parsed.'; ?>

<p>This will also be ignored.</p>
You can also use more advanced structures:
Example 10.1. Advanced escaping
<?php
if ($expression) {?> 
<strong>This is true.</strong>
<?php } else { ?> 

<strong>This is false.</strong>
<?php }?>

This works as expected, because when PHP hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation ) until it hits another opening tag. The example given here is contrived, of course, but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo() or print(). There are four different pairs of opening and closing tags which can be used in php. Two of those, <?php ?> and <script language="php"> </script>, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.
Note: Also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?> tags to remain compliant with standards.

Example 10.2. PHP Opening and Closing Tags
1. <?php echo 'if you want to serve XHTML or XML documents, do like this'; ?>
2. <script language="php">
echo 'some editors (like FrontPage) don\'t
like processing instructions'
;
</script>
3. <? echo 'this is the simplest, an SGML processing instruction'; ?>
<?= expression ?> This is a shortcut for "<? echo expression ?>"

4. <% echo 'You may optionally use ASP-style tags'; %>
<%= $variable; # This is a shortcut for "<% echo . . ." %>

While the tags seen in examples one and two are both always available, example one is the most commonly used, and recommended, of the two.
Short tags (example three) are only available when they are enabled via the short_open_tag php.ini configuration file directive, or if php was configured with the --enable-short-tags option.
Note: If you are using PHP 3 you may also enable short tags via the short_tags() function. This is only available in PHP 3!
ASP style tags (example four) are only available when they are enabled via the asp_tags php.ini configuration file directive.
Note: Support for ASP tags was added in 3.0.4.
Note: Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.

php - basename

Description

string basename ( string $path [, string $suffix] )
Given a string containing a path to a file, this function will return the base name of the file.

Parameters



path
A path. On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).
suffix
If the filename ends in suffix this will also be cut off.

Return Values

Returns the base name of the given path.

ChangeLog


Version Description
4.1.0 The suffix parameter was added

Examples


Example 609. basename() example
 
<?php
 
$path = "/home/httpd/html/index.php"; 
$file = basename($path); // $file is set to "index.php" 
$file = basename($path, ".php"); // $file is set to "index" 
?>

php - base64_encode

Description

string base64_encode ( string $data )
Encodes the given data with base64.
This encoding is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies.
Base64-encoded data takes about 33% more space than the original data.

Parameters



data
The data to encode.

Return Values

The encoded data, as a string.

Examples


Example 2549. base64_encode() example
 
<?php
 
$str = 'This is an encoded string';
 
echo base64_encode($str); 
?>
The above example will output:

VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

php - base64_decode

Description

string base64_decode ( string $data [, bool $strict] )
Decodes a base64 encoded data.

Parameters



data
The decoded data.
strict
Returns FALSE if input contains space or some other separator.

Return Values

Returns the original data or FALSE on failure. The returned data may be binary.

ChangeLog


Version Description
5.2.0 strict added

Examples


Example 2548. base64_decode() example
<?php

$str
= 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';

echo
base64_decode($str); 
?>
  The above example will output:
This is an encoded string

php - base_convert

Description

string base_convert ( string $number, int $frombase, int $tobase )
Returns a string containing number represented in base tobase. The base in which number is given is specified in frombase. Both frombase and tobase have to be between 2 and 36, inclusive. Digits in numbers with a base higher than 10 will be represented with the letters a-z, with a meaning 10, b meaning 11 and z meaning 35.
Warning base_convert() may lose precision on large numbers due to properties related to the internal "double" or "float" type used. Please see the Floating point numbers section in the manual for more specific information and limitations.

Parameters



number
The number to convert
frombase
The base number is in
tobase
The base to convert number to

Return Values

number converted to base tobase

Examples


Example 1123. base_convert() example

<?php

$hexadecimal
= 'A37334';
echo
base_convert($hexadecimal, 16, 2); 
?>
  The above example will output:
101000110111001100110100

php - Autoloading Objects

Many developers writing object-oriented applications create one PHP source file per-class definition. One of the biggest annoyances is having to write a long list of needed includes at the beginning of each script (one for each class).
In PHP 5, this is no longer necessary. You may define an __autoload function which is automatically called in case you are trying to use a class which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.
Note: Exceptions thrown in __autoload function cannot be caught in the catch block and results in a fatal error.
Note: Autoloading is not available if using PHP in CLI interactive mode.

Example 19.7. Autoload example
This example attempts to load the classes MyClass1 and MyClass2 from the files MyClass1.php and MyClass2.php respectively.
<?php
function __autoload($class_name) {
require_once
$class_name . '.php';
}
$obj = new MyClass1();$obj2 = new MyClass2(); 

 ?>

php - Audio Streams

ogg:// PHP 4.3.0 and up (PECL)
  • ogg://soundfile.ogg
  • ogg:///path/to/soundfile.ogg
  • ogg://http://www.example.com/path/to/soundstream.ogg
This wrapper is not enabled by default: In order to use the ogg:// wrapper you must install the » OGG/Vorbis extension available from » PECL.
Files opened for reading via the ogg:// wrapper are treated as compressed audio encoded using the OGG/Vorbis codec. Similarly, files opened for writing or appending via the ogg:// wrapper are writen as compressed audio data. stream_get_meta_data(), when used on an OGG/Vorbis file opened for reading will return various details about the stream including the vendor tag, any included comments, the number of channels, the sampling rate, and the encoding rate range described by: bitrate_lower, bitrate_upper, bitrate_nominal, and bitrate_window.

Table O.11. Wrapper Summary
Attribute Supported
Restricted by allow_url_fopen No
Allows Reading Yes
Allows Writing Yes
Allows Appending Yes
Allows Simultaneous Reading and Writing No
Supports stat() No
Supports unlink() No
Supports rename() No
Supports mkdir() No
Supports rmdir() No


Table O.12. Context options
Name Usage Default Mode
pcm_mode PCM encoding to apply while reading, one of: OGGVORBIS_PCM_U8, OGGVORBIS_PCM_S8, OGGVORBIS_PCM_U16_BE, OGGVORBIS_PCM_S16_BE, OGGVORBIS_PCM_U16_LE, and OGGVORBIS_PCM_S16_LE. (8 vs 16 bit, signed or unsigned, big or little endian) OGGVORBIS_PCM_S16_LE Read
rate Sampling rate of input data, expressed in Hz 44100 Write/Append
bitrate When given as an integer, the fixed bitrate at which to encode. (16000 to 131072) When given as a float, the variable bitrate quality to use. (-1.0 to 1.0) 128000 Write/Append
channels The number of audio channels to encode, typically 1 (Mono), or 2 (Stero). May range as high as 16. 2 Write/Append
comments An array of string values to encode into the track header. Write/Append

php - atanh

Description

float atanh ( float $arg )
Returns the inverse hyperbolic tangent of arg, i.e. the value whose hyperbolic tangent is arg.
Note: This function is not implemented on Windows platforms.

Parameters



arg
The argument to process

Return Values

Inverse hyperbolic tangent of arg

php - atan2

Description

float atan2 ( float $y, float $x )
This function calculates the arc tangent of the two variables x and y. It is similar to calculating the arc tangent of y / x, except that the signs of both arguments are used to determine the quadrant of the result.
The function returns the result in radians, which is between -PI and PI (inclusive).

Parameters



y
Dividend parameter
x
Divisor parameter

Return Values

The arc tangent of y/x in radians.

php - atan

Description

float atan ( float $arg )
Returns the arc tangent of arg in radians. atan() is the complementary function of tan(), which means that a==tan(atan(a)) for every value of a that is within atan()'s range.

Parameters



arg
The argument to process

Return Values

The arc tangent of arg in radians.

php - Assignment Operators

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the rights (that is, "gets set to").
The value of an assignment expression is the value assigned. That is, the value of "$a = 3" is 3. This allows you to do some tricky things:
<?php

$a
= ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
?>
In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example:
<?php

$a
= 3;$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;$b = "Hello ";$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";
?>
Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop. Since PHP 4, assignment by reference has been supported, using the $var = &$othervar; syntax, but this is not possible in PHP 3. 'Assignment by reference' means that both variables end up pointing at the same data, and nothing is copied anywhere. To learn more about references, please read References explained. As of PHP 5, objects are assigned by reference unless explicitly told otherwise with the new clone keyword.

php - assert_options

Description

mixed assert_options ( int $what [, mixed $value] )
Set the various assert() control options or just query their current settings.

Parameters



what
Table 260. Assert Options
option ini-parameter default description
ASSERT_ACTIVE assert.active 1 enable assert() evaluation
ASSERT_WARNING assert.warning 1 issue a PHP warning for each failed assertion
ASSERT_BAIL assert.bail 0 terminate execution on failed assertions
ASSERT_QUIET_EVAL assert.quiet_eval 0 disable error_reporting during assertion expression evaluation
ASSERT_CALLBACK assert.callback (NULL) user function to call on failed assertions
value
An optional new value for the option.

Return Values

Returns the original setting of any option or FALSE on errors.

php - assert

Description

bool assert ( mixed $assertion )
assert() will check the given assertion and take appropriate action if its result is FALSE.
If the assertion is given as a string it will be evaluated as PHP code by assert(). The advantages of a string assertion are less overhead when assertion checking is off and messages containing the assertion expression when an assertion fails. This means that if you pass a boolean condition as assertion this condition will not show up as parameter to the assertion function which you may have defined with the assert_options() function, the condition is converted to a string before calling that handler function, and the boolean FALSE is converted as the empty string.
Assertions should be used as a debugging feature only. You may use them for sanity-checks that test for conditions that should always be TRUE and that indicate some programming errors if not or to check for the presence of certain features like extension functions or certain system limits and features.
Assertions should not be used for normal runtime operations like input parameter checks. As a rule of thumb your code should always be able to work correctly if assertion checking is not activated.
The behavior of assert() may be configured by assert_options() or by .ini-settings described in that functions manual page.
The assert_options() function and/or ASSERT_CALLBACK configuration directive allow a callback function to be set to handle failed assertions.
assert() callbacks are particularly useful for building automated test suites because they allow you to easily capture the code passed to the assertion, along with information on where the assertion was made. While this information can be captured via other methods, using assertions makes it much faster and easier!
The callback function should accept three arguments. The first argument will contain the file the assertion failed in. The second argument will contain the line the assertion failed on and the third argument will contain the expression that failed (if any - literal values such as 1 or "two" will not be passed via this argument)

Parameters


assertion
The assertion.

Return Values

FALSE if the assertion is false, TRUE otherwise.

Examples


Example 1816. Handle a failed assertion with a custom handler
<?php 
// Active assert and make it quiet
assert_options(ASSERT_ACTIVE, 1); 
assert_options(ASSERT_WARNING, 0); 
assert_options(ASSERT_QUIET_EVAL, 1);
// Create a handler function

function my_assert_handler($file, $line, $code)
{
echo
"<hr>Assertion Failed:
File '$file'<br />
Line '$line'<br />
Code '$code'<br /><hr />"
;
}
// Set up the callback

 assert_options(ASSERT_CALLBACK, 'my_assert_handler');
// Make an assertion that should fail

assert('mysql_query("")'); 
?>

php - aspell_suggest

Description

array aspell_suggest ( int $dictionary_link, string $word )
aspell_suggest() suggest spellings for the given word.

Parameters


dictionary_link
The dictionnary link identifier returned by aspell_new().
word
The tested word.

Return Values

Returns an array of suggestions.

Examples


Example 326. aspell_suggest() Example
<?php

$aspell_link
= aspell_new("english");

if (!
aspell_check($aspell_link, "test")) {
 
$suggestions = aspell_suggest($aspell_link, "test");

  foreach (
$suggestions as $suggestion) {
   echo
"Possible spelling: $suggestion<br />\n";
  }
}
?>