php - bcsub

Description

string bcsub ( string $left_operand, string $right_operand [, int $scale] )
Subtracts the right_operand from the left_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 result of the substraction, as a string.

Examples

Example 336. bcsub() example
<?php

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

echo
bcsub($a, $b); // -3 

echo bcsub($a, $b, 4); // -3.7660
?>

php - bcsqrt

Description

string bcsqrt ( string $operand [, int $scale] )
Return the square root of the operand.

Parameters


operand
The 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 square root as a string, or NULL if operand is negative.

Examples

Example 335. bcsqrt() example
<?php
echo bcsqrt('2', 3); // 1.414
?>

php - bcscale

Description

bool bcscale ( int $scale )
Sets the default scale parameter for all subsequent bc math functions that do not explicitly specify a scale parameter.

Parameters


scale
The scale factor.

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 334. bcscale() example
<?php
// default scale : 3

bcscale(3);
echo
bcdiv('105', '6.55957'); // 16.007

// this is the same without bcscale()

echo bcdiv('105', '6.55957', 3); // 16.007
?>

php - bcpowmod

Description

string bcpowmod ( string $left_operand, string $right_operand, string $modulus [, int $scale] )
Use the fast-exponentiation method to raise left_operand to the power right_operand with respect to the modulus modulus.

Parameters



left_operand
The left operand, as a string.
right_operand
The right operand, as a string.
modulus
The modulus, 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, or NULL if modulus is 0.

Notes

Note: Because this method uses the modulus operation, non-natural numbers may give unexpected results. A natural number is any positive non-zero integer.

Examples

The following two statements are functionally identical. The bcpowmod() version however, executes in less time and can accept larger parameters.
<?php
$a
= bcpowmod($x, $y, $mod);
$b = bcmod(bcpow($x, $y), $mod);
// $a and $b are equal to each other.
?>

php - bcpow

Description

string bcpow ( string $left_operand, string $right_operand [, int $scale] )
Raise left_operand to the power 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 333. bcpow() example
<?php
echo bcpow('4.2', '3', 2); // 74.08
?>

php - bcompiler_write_header

Description

bool bcompiler_write_header ( resource $filehandle [, string $write_ver] )
Writes the header part of a bcompiler file.

Parameters


filehandle
A file handle as returned by fopen().
write_ver
Can be used to write bytecode in a previously used format, so that you can use it with older versions of bcompiler.

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 348. bcompiler_write_header() example
<?php
$fh
= fopen("/tmp/example","w");

bcompiler_write_header($fh); 
bcompiler_write_class($fh,"DB"); 
bcompiler_write_footer($fh); 
fclose($fh);
?>

php - bcompiler_write_functions_from_file

Description

bool bcompiler_write_functions_from_file ( resource $filehandle, string $fileName )
Searches for all functions declared in the given file, and writes their correspondent bytecodes to the open file handle.

Parameters



filehandle
A file handle as returned by fopen().
fileName
The file to be compiled. You must always include or require the file you intend to compile.

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 347. bcompiler_write_functions_from_file() example
<?php

require('module.php');
$fh = fopen("/tmp/example","w");

bcompiler_write_header($fh);

bcompiler_write_functions_from_file($fh,'module.php');

bcompiler_write_footer($fh);

fclose($fh);
?>

php - bcompiler_write_function

Description

bool bcompiler_write_function ( resource $filehandle, string $functionName )
Reads the bytecodes from PHP for an existing function, and writes them to the open file handle. Order is not important, (eg. if function b uses function a, and you compile it like the example below, it will work perfectly OK).

Parameters


filehandle
A file handle as returned by fopen().
functionName
The function name, as a string.

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 346. bcompiler_write_function() example
<?php
$fh
= fopen("/tmp/example","w");
bcompiler_write_header($fh);
bcompiler_write_function($fh,"my_function_a");
bcompiler_write_function($fh,"my_function_b"); 
bcompiler_write_footer($fh);
fclose($fh);
?>

php - bcompiler_write_footer

Description

bool bcompiler_write_footer ( resource $filehandle )
Writes the single character \x00 to indicate End of compiled data.

Parameters


filehandle
A file handle as returned by fopen().

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 345. bcompiler_write_footer() example
<?php
$fh
= fopen("/tmp/example","w"); 

bcompiler_write_header($fh); 
bcompiler_write_class($fh,"DB"); 
bcompiler_write_class($fh,"DB_common"); 
bcompiler_write_footer($fh); 
fclose($fh);
?>

php - bcompiler_write_file

Description

bool bcompiler_write_file ( resource $filehandle, string $filename )
This function complies specified source file into bytecodes, and writes them to the open file handle.

Parameters


filehandle
A file handle as returned by fopen().
filename
The source file path, as a string.

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 344. bcompiler_write_file() example
<?php
$fh
= fopen("example.phb", "w");

bcompiler_write_header($fh); 
bcompiler_write_file($fh, "example.php"); 
bcompiler_write_footer($fh); 
fclose($fh);
/* the following should be equivalent:
include "example.php";
and
include "example.phb";
*/
 

?>

php - bcompiler_write_exe_footer

Description

bool bcompiler_write_exe_footer ( resource $filehandle, int $startpos )
An EXE (or self executable) file consists of 3 parts:
The stub (executable code, e.g. a compiled C program) that loads PHP interpreter, bcompiler extension, stored Bytecodes and initiates a call for the specified function (e.g. main) or class method (e.g. main::main)
The Bytecodes (uncompressed only for the moment)
The bcompiler EXE footer

To obtain a suitable stub you can compile php_embed-based stub phpe.c located in the examples/embed directory on bcompiler's CVS.

Parameters


filehandle
A file handle as returned by fopen().
startpos
The file position at which the Bytecodes start, and can be obtained using ftell().

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 343. bcompiler_write_footer() example
<?php
/* creating the output file (example.exe) */ 

$fh = fopen("example.exe", "w");
/* 1) writing a stub (phpe.exe) */ 

$size = filesize("phpe.exe"); 
$fr = fopen("phpe.exe", "r"); 
fwrite($fh, fread($fr, $size), $size);$startpos = ftell($fh);
/* 2) writing bytecodes */

bcompiler_write_header($fh); 
bcompiler_write_class($fh, "myclass"); 
bcompiler_write_function($fh, "main"); 
bcompiler_write_footer($fh);
/* 3) writing EXE footer */

bcompiler_write_exe_footer($fh, $startpos);
/* closing the output file */

 fclose($fh); 
?>

php - bcompiler_write_constant

Description

bool bcompiler_write_constant ( resource $filehandle, string $constantName )
Reads the bytecodes from PHP for an existing constant, and writes them to the open file handle.

Parameters


filehandle
A file handle as returned by fopen().
constantName
The name of the defined constant, as a string.

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 342. bcompiler_write_constant() example

<?php

define
("MODULE_MAX", 30);
$fh = fopen("/tmp/example","w"); 

bcompiler_write_header($fh);  
bcompiler_write_constant($fh,"MODULE_MAX");  
bcompiler_write_footer($fh); fclose($fh);
 

?>

php - bcompiler_write_class

Description

bool bcompiler_write_class ( resource $filehandle, string $className [, string $extends] )
Reads the bytecodes from PHP for an existing class, and writes them to the open file handle.

Parameters


filehandle
A file handle as returned by fopen().
className
The class name, as a string.
extends

Return Values

Returns TRUE on success or FALSE on failure.

Examples


Example 341. bcompiler_write_class() example
<?php

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

bcompiler_write_header($fh);
bcompiler_write_class($fh,"DB");
// you must write DB_common before DB_mysql, as DB_mysql extends DB_common. 
bcompiler_write_class($fh,"DB_common");
bcompiler_write_class($fh,"DB_mysql");
bcompiler_write_footer($fh); 
fclose($fh);
 

?>