1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Is there a way around to use out parameters in PHP

Discussion in 'PHP' started by benjib98, Jan 5, 2008.

  1. #1
    I am migrating code from .NET into PHP and would like to know if there is a way to use out parameters in functions in PHP. I know that PHP directly does not support output parameters(correct me if I am wrong), but maybe there is a way around.
    Please help me, or if you know a link describing a similar solution, just send it to me.
    Thanks

    ben
    SEMrush
     
    benjib98, Jan 5, 2008 IP
    SEMrush
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    PHP does support by-reference parameters (I'm assuming that's what you mean).

    That is to say you can call a PHP function and have it make changes to the original value passed into the function as a parameter.

    As far as I can remember, you just have to put an ampersand before the appropriate parameters in the function definition.

    So:
    function functionName( $parameter1, $parameter2 ) {

    becomes:
    function functionName( &$parameter1, &$parameter2 ) {
     
    TwistMyArm, Jan 5, 2008 IP
  3. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #3
    you can declare a variable global in a function and use it as a normal variable without passing the variable to function

    Regards
    Alex
     
    kmap, Jan 5, 2008 IP
  4. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Avoid using global vars if it's really not neccessery, use the "TwistMyArm" suggestion, passing parameters by reference in php is equivalent to out parameters in .NET
     
    hogan_h, Jan 5, 2008 IP
  5. benjib98

    benjib98 Peon

    Messages:
    23
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    just one more question:is the & prefix for passing parameters by reference valid and supported in PHP4?
    thanks
     
    benjib98, Jan 6, 2008 IP
  6. hogan_h

    hogan_h Peon

    Messages:
    199
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #6
    hogan_h, Jan 6, 2008 IP
  7. thapame

    thapame Well-Known Member

    Messages:
    739
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    180
    #7
    I don't think so,
    you can use parameters with php like

    function functionName( $parameter1, $parameter2 ) {
    print $parameter1;
    prin $parameter2;
    }

    or simply

    $var1="test1";
    $var2="test2";
    function functionName() {
    global $var1, $var2;
    print $var1;
    print $var2;
    }
     
    thapame, Jan 6, 2008 IP
  8. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Did you even read the link that hogan_h gave? Or is your concept of 'out parameters' different to ours?
     
    TwistMyArm, Jan 6, 2008 IP
  9. thapame

    thapame Well-Known Member

    Messages:
    739
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    180
    #9
    yes the link what hogan_h have posted is exactly what I meant.
    I was just commenting to your post regarding

     
    thapame, Jan 6, 2008 IP
  10. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Well now I'm really confused... what exactly is wrong with what I wrote there, then?
     
    TwistMyArm, Jan 7, 2008 IP