Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Passing optional errorno/errormsg by reference

26 views
Skip to first unread message

BKDotCom

unread,
Oct 3, 2003, 12:19:48 PM10/3/03
to
Many built-in functions allow an optional variable to be passed for
error messages. How do I implement this in my own function?

function test($param1, $param2, &$errormsg) {
// complains if 3rd param is not sent
}

function test($param1, $param2, &$errormsg='') {
// complains about something
}

please enlighten me.
Thanks.

Andy Hassall

unread,
Oct 3, 2003, 2:46:28 PM10/3/03
to

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)

Paulus Magnus

unread,
Oct 3, 2003, 3:18:23 PM10/3/03
to

"BKDotCom" <Brad...@swbell.net> wrote in message
news:e8269f23.03100...@posting.google.com...

function test ($param1, $param2, $errormsg = "") {
if ($errormsg != "") {
//Got an error reported!!!
}
}

:))


Andy Hassall

unread,
Oct 3, 2003, 3:30:14 PM10/3/03
to

That's not passing by reference.

BKDotCom

unread,
Oct 3, 2003, 5:15:58 PM10/3/03
to
Andy Hassall <an...@andyh.co.uk> wrote in message news:<tjgrnvch6pqn3tn9b...@4ax.com>...

> As far as I know, I don't think you can have optional reference parameters.

does that make functions such as fsockopen() "special"?

Paulus Magnus

unread,
Oct 3, 2003, 5:24:18 PM10/3/03
to

"Andy Hassall" <an...@andyh.co.uk> wrote in message
news:qhjrnvcuft848t9jj...@4ax.com...

> On Fri, 3 Oct 2003 19:18:23 +0000 (UTC), "Paulus Magnus"
> <paulus...@loves-spam.com> wrote:
>
> >
> >"BKDotCom" <Brad...@swbell.net> wrote in message
> >news:e8269f23.03100...@posting.google.com...
> >> Many built-in functions allow an optional variable to be passed for
> >> error messages. How do I implement this in my own function?
> >>
> >> function test($param1, $param2, &$errormsg) {
> >> // complains if 3rd param is not sent
> >> }
> >>
> >> function test($param1, $param2, &$errormsg='') {
> >> // complains about something
> >> }
> >>
> >> please enlighten me.
> >> Thanks.
> >
> >function test ($param1, $param2, $errormsg = "") {
> > if ($errormsg != "") {
> > //Got an error reported!!!
> > }
> >}
>
> That's not passing by reference.

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


Andy Hassall

unread,
Oct 3, 2003, 5:41:49 PM10/3/03
to
On Fri, 3 Oct 2003 21:24:18 +0000 (UTC), "Paulus Magnus"
<paulus...@loves-spam.com> wrote:

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

Andy Hassall

unread,
Oct 3, 2003, 5:47:10 PM10/3/03
to

Right, looks like it's possible with compiled-in functions. Don't know how
with a user defined PHP function, though.

Pedro

unread,
Oct 3, 2003, 6:36:46 PM10/3/03
to
Andy Hassall wrote:
>On Fri, 3 Oct 2003 21:24:18 +0000 (UTC), "Paulus Magnus"
> 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.


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.

Andy Hassall

unread,
Oct 3, 2003, 6:48:48 PM10/3/03
to
On Fri, 03 Oct 2003 23:36:46 +0100, Pedro <hex...@hotpop.com> wrote:

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

0 new messages