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
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 ) {
you can declare a variable global in a function and use it as a normal variable without passing the variable to function Regards Alex
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
just one more question:is the & prefix for passing parameters by reference valid and supported in PHP4? thanks
Yes it is. Take a look at the example for defining and calling a function: http://www.php.net/manual/en/functions.arguments.php#functions.arguments.by-reference
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; }
Did you even read the link that hogan_h gave? Or is your concept of 'out parameters' different to ours?
yes the link what hogan_h have posted is exactly what I meant. I was just commenting to your post regarding