function test($param1, $param2, &$errormsg) {
// complains if 3rd param is not sent
}
function test($param1, $param2, &$errormsg='') {
// complains about something
}
please enlighten me.
Thanks.
As far as I know, I don't think you can have optional reference parameters.
If it's passed by reference, that means the storage for the value was declared
back in the caller's scope. So what happens when you don't pass an existing
variable in?
You could argue that having a default value means to actually create a new
variable in this scope rather than it being a reference, but I think there's
enough ambiguity there for PHP's rejecting this to be a fair choice as well.
--
Andy Hassall (an...@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
function test ($param1, $param2, $errormsg = "") {
if ($errormsg != "") {
//Got an error reported!!!
}
}
:))
That's not passing by reference.
does that make functions such as fsockopen() "special"?
Apologies, I read the post, not the subject.
error_reporting (E_ALL);
$error = "Test";
test (1, 2, &$error);
function test ($param1, $param2, &$errormsg) {
echo ("<p>$errormsg</p>");
}
I expected this code to throw an error when the call to test was made if I
commented out the $error = line but it didn't.
However, I'm not sure this is a good way to solve the problem. I only pass
variables by reference when I want to save memory or modify the referenced
variable. In the case of an error message it seems that a reference isn't
required.
Paulus
>error_reporting (E_ALL);
>$error = "Test";
>
>test (1, 2, &$error);
>
>function test ($param1, $param2, &$errormsg) {
> echo ("<p>$errormsg</p>");
>}
>
>I expected this code to throw an error when the call to test was made if I
>commented out the $error = line but it didn't.
Taking a reference of a variable appears to 'declare' it, and then the
function can write into it.
<?php
error_reporting (E_ALL);
function test ($param1, $param2, &$errormsg) {
return false;
}
if (!test(1,2,$error)) {
var_dump(get_defined_vars());
}
?>
(in the output is: ["error"]=> NULL , so $error now exists in the global
scope)
>However, I'm not sure this is a good way to solve the problem. I only pass
>variables by reference when I want to save memory or modify the referenced
>variable. In the case of an error message it seems that a reference isn't
>required.
I think you've got the intent backwards here; it's not to pass an error
message TO the function, it's to allow the function to 'return' one BACK
through the referenced variable, on top of the usual return value.
e.g.:
<?php
error_reporting (E_ALL);
function test ($param1, $param2, &$errormsg) {
$errormsg = "test error";
return false;
}
if (!test(1,2,$error))
echo $error;
?>
But you might not want to bother, so the OP wants it optional, and I can't see
a way of doing that in a user defined function.
Right, looks like it's possible with compiled-in functions. Don't know how
with a user defined PHP function, though.
I get no errors at all with this (and I get the expected result)
<?php
error_reporting(E_ALL);
function testrefparm($a, $b, &$x, &$y='', &$z=null) {
$x=$a-$b;
$y=$a*$b;
if ($b) $z=$a/$b; else $z='##DIV0';
return $a+$b;
}
echo '<br /><br />test 1: ';
echo testrefparm(14, 3, $a);
echo ' | $a = ', $a;
echo '<br /><br />test 2: ';
echo testrefparm(15, 2, $b, $c);
echo ' | $b = ', $b, ' | $c = ', $c;
echo '<br /><br />test 3: ';
echo testrefparm(16, 1, $d, $e, $f);
echo ' | $d = ', $d, ' | $e = ', $e, ' | $f = ', $f;
echo '<br /><br />test 4: ';
echo testrefparm(17, 0, $g, $h, $i);
echo ' | $g = ', $g, ' | $h = ', $h, ' | $i = ', $i;
?>
Running on Windows2K / Apache 2.0.44 / PHP 5.0.0 dev
However, now matter how I tried, I couldn't get it (or something
similar) to run on Linux / Apache 1.3.27 / PHP 4.3.3RC3
very strange indeed !
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
>I get no errors at all with this (and I get the expected result)
>
>Running on Windows2K / Apache 2.0.44 / PHP 5.0.0 dev
>
>However, now matter how I tried, I couldn't get it (or something
>similar) to run on Linux / Apache 1.3.27 / PHP 4.3.3RC3
There's supposed to be a lot of improvements with references in the Zend
Engine 2 in PHP5, so that's probably one of them.