PHP function question

Ask a Question related to PHP Development, Design and Development.

  1. #1

    Default PHP function question

    When writing a function that takes in a variable number of parameters and
    processing it using func_get_args or a similar technique, I'm assuming that
    one is only able to use the pass by value method of parameter passing. Can
    someone confirm or deny this for me?

    Below is a trivial example of what I'm talking about. I'm assuming the
    function below does nothing useful.

    <?php

    function squares( )
    {
    $params=function_get_args();
    $numparams= func_num_args();
    for ($i=0;$i<$numparams;$i++)
    $params[$i]*=$params[$i];
    }

    ?>


    Tony Guest

  2. Similar Questions and Discussions

    1. IIF function question
      I'm studying for my ColdFusionMX exam using Ben Forta's study guide. There is quite a bit of errata in the book, including code that simply doesn't...
    2. Function Question
      Hi! Is there a considerable degredidation in speed if I where to include an ASP page into every other ASP page that would contain all the...
    3. Simple AS function question
      Why will this not work home_btn.onRollOver = setInterval(delayAction,2000,home); function delayAction (framename:String) var name:String =...
    4. utf8_decode function question
      Hi folks, I read in a security note that something known as a cross-site scripting attacker can use utf8 encoding and that you could decode data to...
    5. Question: ConnectToDatabase function
      Thanks! I wasn't sure if the Try code would continue or not if an exception was "caught". Now I know it doesn't. "Marina" <mzlatkina@hotmail.com>...
  3. #2

    Default Re: PHP function question

    ["Followup-To:" header set to comp.lang.php.]
    Tony wrote:
    > When writing a function that takes in a variable number of parameters and
    > processing it using func_get_args or a similar technique, I'm assuming that
    > one is only able to use the pass by value method of parameter passing. Can
    > someone confirm or deny this for me?
    I think you're right.
    > Below is a trivial example of what I'm talking about. I'm assuming the
    > function below does nothing useful.
    >
    ><?php
    >
    > function squares( )
    > {
    > $params=function_get_args();
    > $numparams= func_num_args();
    > for ($i=0;$i<$numparams;$i++)
    > $params[$i]*=$params[$i];
    > }
    >
    > ?>

    But you can use the $GLOBALS variable to pretend you're passing by
    reference.

    Here's a way

    <?php
    function squares() {
    $parms = func_get_args();
    $nparms = func_num_args();
    $retval = array(); // return array

    for ($i = 0; $i < $nparms; ++$i)
    if ($parms[$i]{0} == '$')
    $GLOBALS[substr($parms[$i], 1)] *=
    $GLOBALS[substr($parms[$i], 1)];
    else $retval[] = $parms[$i] * $parms[$i];
    return $retval;
    }

    $a = 6; $b = 7; $c = 8;
    $res = squares($a, "$b", '$c');

    print_r($res);
    echo $c;
    ?>
    --
    --= my mail box only accepts =--
    --= Content-Type: text/plain =--
    --= Size below 10001 bytes =--
    Pedro Graca Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139