Archive - Jan 26, 2007

Date

Register globals inactive and you want it's function?

in

Register global is default off after version 4.2.0. It's rare there are scripts that need register global support. But if there is one, here is a way can always extract register globals.

1. The very popular and common method:

foreach($_REQUEST as $key => $var){
$$key = $var;
}

2. The import_request_variables() function:
This function globalize each value in $_GET, $_POST and $_COOKIE. It was introduced after PHP 4.1.0. The function takes 2 arguments: the first is which array will be extracted, 'G' for $_GET, 'P' for $_POST and 'C' for $_COOKIE(capitalization doesn't matter here). You can combine any of these three and only extract the ones you need. The second argument choses the prefix of each variable. prefix + the key in the array will be the new variable's name.

//suppose in $_POST['aa'] = 30;
//$_POST['bb'] = 20;
import_request_variables ('GPC','vars_');
echo $vars_aa;//30
echo $vars_bb;//20

Note: The extract() can be pretty useful here too.

How to make a function with infinite argument(parameter)

in

There is no point of making one function can have infinite argument, because pass an array of argument could be a lot smarter. But, just some knowledge you can learn and show off. Here is an example of how it works:

function bcaddall($add){
$num = 0;
$args = func_get_args();
foreach($args as $arg){
$num = bcadd($num, $arg);
}
return $num;
}

This is pretty simple, PHP have a built in function func_get_args(), this function can make all the arguments into an array. After that, everything is simple, you know how to work with an array right?

In windows, newline = newline + carriage return

in

I have spend hour trying to figure out why a very simple array look exactly the same as the other is not the same.

$string = '1
2
3';
$string = explode("\n", $string);
$array = array('1','2','3');

I found those two are not identical. and that's because $string is actually this in windows:

$string = "1\r\n2\r\n3";

Wish I thought about it earlier, it cost me like hours to figure it out.

Introduction to BCMath

What is BCMath and why should anyone use it?
BC stand for Binary Calculator. BCMath can break free from PHP's limit on numbers and theoretically possible on calculating infinite large and infinite precision numbers. It receives numbers as strings and output them as string. There is not much of a reason to use BCMath, because PHP are not made to do those very mathy calculations. But if you want, this tutorial is for you.
What are the functions in BCMath?
There are only 10 functions in BCMath, there are all basic ones, you can find the equivalent in PHP math operators.
bcadd
Adds two numbers together, equivalent to "+". Optional third parameter choses how much precision the function is going to calculate.
Example:

bcadd('333','666');//return 999
 
bcadd('0.9','0.01',1);//return 0.9

bccomp
Compare two numbers, return a integer value states the 1nd value is larger(1), equal(0) or smaller(-1). Optional third parameter choses how much precision the function is going to count.
Example:

bccomp('30','50');//return -1
 
bccomp('50','30');//return 1
 
bccomp('50.05','50',1);//return 0

bcdiv
Divide the first number to the 2nd number, return a string result. Equivalent to "/". Optional third parameter choses how much precision the function is going to count.
Example:

bcdiv('1','3',3);//return 0.333

bcmod
Find the modules of a number. first number is the number get module by the 2nd one. return a string. Equivalent to "%".
Example:

bcmod('1','3');//return 0

bcmul
Multiply the first number to the second one. Return a string. Optional third parameter choses how much precision the function is going to count. Equivalent to "*".
Example:

bcmul('1','3',5);return 3.00000

bcpow
The first number is the base and the second is the exponent. Optional third parameter choses how much precision the function is going to count. Equivalent to the pow() in PHP. Note that the function only take in integers as exponents(check out this post to see how to solve the problem) unlike pow(). and the function can't have exponents lager than 2^31.
Example:

bcpow('3','2',2);//return 9.00
bcpow('3','0.9');//return 1
bcpow('3','999999999999999999999999999999999999');//return 1

bcpowmod
bcpowmod is equivalent to a bcpow then bcmod, like this:

bcpowmod($base, $exponent, $mod);
bcmod(bcpow($base, $exponent), $mod);

This function can have exponent over 2^31 and have a optional forth parameter choses how much precision the function is going to count.

bcscale
bcscale set the default precision of all bc functions. Input must be a integer, and it return true when success and false when fail.
Example:

bcscale(3);
bcadd('3','3');//return 6.000

bcsqrt
Calculate the square root of the input number, optional 2nd parameter choses how much precision the function is going to count. Equivalent to sqrt() function.
Example:

bcsqrt('2',5);//return 1.41421

bcsub
Subtract the 2nd number from the first number. optional 3nd parameter choses how much precision the function is going to count. Equivalent to "-".
Example:

bcsub('3','2',2);//return 1.00

What else do I need to know?
BCMath only have 10 functions, so you have to write your own function, like one that do sin() and cos(). That's when you will find algorithms really important.
There is another math extension called GMP, quite like BCMath but much more powerful(built in functions saves you time).

Honey Pot that kill bots