Results 1 to 9 of 9

Thread: PHP functions

  1. #1
    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?

  2. #2
    Join Date
    Feb 2006
    Location
    Lancashire, UK
    Posts
    474
    you mean there are 30,000 to pass through at the same time?

  3. #3
    Quote Originally Posted by DanCF
    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?
    I'll give you 2 simple examples on how to do what you specified.

    #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 

  4. #4
    Join Date
    Feb 2003
    Location
    AR
    Posts
    2,382

  5. #5
    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.

  6. #6
    Join Date
    Dec 2005
    Posts
    397
    Quote Originally Posted by mikey1090
    you mean there are 30,000 to pass through at the same time?
    Yes. Its a neural network program I wrote in C++ where you just do void function(); and the variables flow from the body tothe function and back to the body without needing to be passed. I am now rewriting the code in php so it can be implemented on in a web script.

    Thanks for all the replies. I think I can figure it out from here.

  7. #7
    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.

  8. #8
    Join Date
    May 2005
    Location
    South Africa
    Posts
    17
    Quote Originally Posted by DanCF
    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.
    As far as I know PHP won't round the number off by default, except if its something like 14.000. In that case do something like this:

    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;
    }
    ?>
    I guess there is an easier way, but this works fine for me...

    Quote Originally Posted by DanCF
    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.
    Not that I know of, but I know Zend engine do check syntax while you are typing...
    Wim

    www.matogen.com - Corporate Web Development

  9. #9
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •