Results 1 to 9 of 9
Thread: PHP functions
-
09-26-2006, 11:29 AM #1Aspiring Evangelist
- Join Date
- Dec 2005
- Posts
- 397
PHP functions
I have some various functions in a php script which I have created just so that I dont have to retype the code each time i need it. The codes just perform mathematical operations on variables, and there are too many variables to pass to the function (~30000), and too many to return. I vaguely remember there being something you can do to a php function to have it use global values, rather than the ones created within the function, but have had no success googling this. Does anyone know how to do this?
-
09-26-2006, 12:17 PM #2Web Hosting Evangelist
- Join Date
- Feb 2006
- Location
- Lancashire, UK
- Posts
- 474
you mean there are 30,000 to pass through at the same time?
-
09-26-2006, 12:28 PM #3Web Hosting Evangelist
- Join Date
- Apr 2005
- Posts
- 522
Originally Posted by DanCF
#1
PHP Code:
$word = 'hello';
function say()
{
global $word;
echo $word;
return 1;
}
$number = say(); // it will echo hello, and variable $number will have 1 assigned to it
#2 (which i prefer)
PHP Code:
function return_something(&$return_data)
{
$return_data = array ('hello', 'world');
if(1)
{
return 1;
}
else
{
return 0;
}
}
if(return_something($please))
{
echo '1 is 1';
}
else
{
echo '1 is not 1';
}
echo $please[0]; // hello
echo $please[1]; // world
-
09-26-2006, 12:28 PM #4Web Hosting Master
- Join Date
- Feb 2003
- Location
- AR
- Posts
- 2,382
You're wanting pass by reference.
http://www.zend.com/manual/language.references.pass.php
-
09-26-2006, 01:07 PM #5Web Hosting Guru
- Join Date
- Nov 2003
- Posts
- 297
IMO there are times to do all 3:
if a variable really is global and in scope used across many modules, use a global.
if it is a collection of related variables, use a data structure.
if what you are passing is very large and you want to return it too, pass a pointer.
-
09-26-2006, 03:42 PM #6Aspiring Evangelist
- Join Date
- Dec 2005
- Posts
- 397
Originally Posted by mikey1090
Thanks for all the replies. I think I can figure it out from here.
-
09-27-2006, 01:44 PM #7Aspiring Evangelist
- Join Date
- Dec 2005
- Posts
- 397
Ok, I am having a heck of a time converting from C++ to PHP. Is there a way to set a variable in php as a double, because in a sigmoid function I wrote, PHP just rounds to zero after the pre-sigmoided value hits 1000.
Also, is there such thing as a C++ to PHP translator/converter? I would really like to be able to check my syntax to make sure everything is correct.
-
09-27-2006, 09:51 PM #8Newbie
- Join Date
- May 2005
- Location
- South Africa
- Posts
- 17
Originally Posted by DanCF
Code:<? function trimAmount($amount){ //returns 2 decimals after the point, regardless of the number if ($amount==floor($amount)) {return (1*$amount).".00";} if (10*$amount==floor(10*$amount)) {return (1*$amount)."0";} return floor($amount*100+0.5)/100; } ?>
Originally Posted by DanCF
-
09-29-2006, 06:34 PM #9Aspiring Evangelist
- Join Date
- Dec 2005
- Posts
- 397
Ok thanks.
Does anyone know how accurate the serialize()/unserialize() function is? I am saving this array of ~100,000 numbers which are decimals to about the 32nd decimal place. I do serialize($array) and write it to a file as a string, then when i want it I read the string in fromthe file then run unserialize($array). This works flawlessly when the array is first created and there are only about 6 decimals. Once they are tweaked and start coming out to really exact values, they end up being saved to the file, but when i recall the string and do unserialize, some are saved as -INF so then when I start doing operations with them,allthe results come out as NaN.