Warning Message From fsockopen()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Warning Message From fsockopen()

Post by JackD »

The following code generates two identical warning messages when it executes the fsockopen(). Is there a way to suppress or eliminate these messages? I tried adding a timeout value to the call, but it did not help.

Code: Select all

 
$SockErr = 0;
$SockErrMsg = "";
 
if (!$Socket_Fd = fsockopen('96.38.238.50', '8000', &$SockErr, &$SockErrMsg))
{ echo $SockErr . ' - ' . $SockErrMsg;
exit;
}
 
The warning message is:
Warning: Call-time pass-by-reference has been deprecated in /home/content/13/5169913/html/ordering.php on line 5
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Warning Message From fsockopen()

Post by Weirdan »

JackD wrote:Is there a way to suppress or eliminate these messages?
Sure. It is using & in a function call that is deprecated. Remove those ampersands and warning will go away.
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Warning Message From fsockopen()

Post by JackD »

Thanks! I thought I had to use those as pointer designations so the error info could be posted to them. The php manual gave the following:

resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )

What is the purpose of the '&' in the above reference?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Warning Message From fsockopen()

Post by Weirdan »

JackD wrote:The php manual gave the following:
resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )
What is the purpose of the '&' in the above reference?
It says that function is free to modify its parameter - and those modifications will be visible to function caller. Consider this example:

Code: Select all

 
function byRef(&$a) {
   $a = 1;
}
 
function byVal($a) {
   $a = 1;
}
$a = 10;
byRef($a);
var_dump($a); // 1
$a = 10;
byVal($a);
var_dump($a); // 10
 
Function signatures that are shown in the PHP manual are how php programmer would have written their declaration (with some additional pseudo-syntax to signify argument types). So, signature for byRef I would write down like:

Code: Select all

void byRef(&$a);
, and signature for byVal like this:

Code: Select all

void byVal($a);
Previously it was possible to force parameter to be passed by reference even to those function that did not declare the parameter as accepted by reference. So in old versions of php if I called

Code: Select all

byVal(&$a);
I would get $a modified. Now this option is no longer supported (and for good reasons) and instead you get the warning.
JackD
Forum Commoner
Posts: 62
Joined: Sat Dec 12, 2009 6:25 pm

Re: Warning Message From fsockopen()

Post by JackD »

So, without the '&', the argument is passed as a constant that cannot be changed by the function, while with the '&' the argument is passed as a variable that can assigned a new value by the function that is visible to the caller.

Thank you very much!!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Warning Message From fsockopen()

Post by Weirdan »

JackD wrote:So, without the '&', the argument is passed as a constant that cannot be changed by the function, while with the '&' the argument is passed as a variable that can assigned a new value by the function that is visible to the caller.
Without the '&' in function declaration the argument is passed as 'cheap copy' - meaning if you attempt to change it from inside the function php interpreter will create a copy of it and perform modification on the copy. Overall it results in that you can change the argument value, but this change won't be visible outside the function. Using '&' in function call has no effect other than producing a warning (unless you have allow_call_time_pass_reference enabled in php.ini).
Post Reply