Passing optional errorno/errormsg by reference

This is a discussion on Passing optional errorno/errormsg by reference within the PHP Language forums, part of the PHP Programming Forums category; Many built-in functions allow an optional variable to be passed for error messages. How do I implement this in ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-03-2003
BKDotCom
 
Posts: n/a
Default Passing optional errorno/errormsg by reference

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.
Reply With Quote
  #2 (permalink)  
Old 10-03-2003
Andy Hassall
 
Posts: n/a
Default Re: Passing optional errorno/errormsg by reference

On 3 Oct 2003 09:19:48 -0700, BradKent@swbell.net (BKDotCom) wrote:

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


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 (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Reply With Quote
  #3 (permalink)  
Old 10-03-2003
Paulus Magnus
 
Posts: n/a
Default Re: Passing optional errorno/errormsg by reference


"BKDotCom" <BradKent@swbell.net> wrote in message
news:e8269f23.0310030819.1bb543b2@posting.google.c om...
> 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!!!
}
}

:))


Reply With Quote
  #4 (permalink)  
Old 10-03-2003
Andy Hassall
 
Posts: n/a
Default Re: Passing optional errorno/errormsg by reference

On Fri, 3 Oct 2003 19:18:23 +0000 (UTC), "Paulus Magnus"
<paulus.magnus@loves-spam.com> wrote:

>
>"BKDotCom" <BradKent@swbell.net> wrote in message
>news:e8269f23.0310030819.1bb543b2@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.

--
Andy Hassall (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Reply With Quote
  #5 (permalink)  
Old 10-03-2003
BKDotCom
 
Posts: n/a
Default Re: Passing optional errorno/errormsg by reference

Andy Hassall <andy@andyh.co.uk> wrote in message news:<tjgrnvch6pqn3tn9bbtjg8ot0padjrb8ei@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"?
Reply With Quote
  #6 (permalink)  
Old 10-03-2003
Paulus Magnus
 
Posts: n/a
Default Re: Passing optional errorno/errormsg by reference


"Andy Hassall" <andy@andyh.co.uk> wrote in message
news:qhjrnvcuft848t9jjver5nrlvq63psi7f2@4ax.com...
> On Fri, 3 Oct 2003 19:18:23 +0000 (UTC), "Paulus Magnus"
> <paulus.magnus@loves-spam.com> wrote:
>
> >
> >"BKDotCom" <BradKent@swbell.net> wrote in message
> >news:e8269f23.0310030819.1bb543b2@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


Reply With Quote
  #7 (permalink)  
Old 10-03-2003
Andy Hassall
 
Posts: n/a
Default Re: Passing optional errorno/errormsg by reference

On Fri, 3 Oct 2003 21:24:18 +0000 (UTC), "Paulus Magnus"
<paulus.magnus@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 (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Reply With Quote
  #8 (permalink)  
Old 10-03-2003
Andy Hassall
 
Posts: n/a
Default Re: Passing optional errorno/errormsg by reference

On 3 Oct 2003 14:15:58 -0700, BradKent@swbell.net (BKDotCom) wrote:

>Andy Hassall <andy@andyh.co.uk> wrote in message news:<tjgrnvch6pqn3tn9bbtjg8ot0padjrb8ei@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"?


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

--
Andy Hassall (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Reply With Quote
  #9 (permalink)  
Old 10-04-2003
Pedro
 
Posts: n/a
Default Re: Passing optional errorno/errormsg by reference

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.
Reply With Quote
  #10 (permalink)  
Old 10-04-2003
Andy Hassall
 
Posts: n/a
Default Re: Passing optional errorno/errormsg by reference

On Fri, 03 Oct 2003 23:36:46 +0100, Pedro <hexkid@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.

--
Andy Hassall (andy@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:28 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0