Jump to content


Photo

Newbie with a pass by reference problem


  • Please log in to reply
3 replies to this topic

#1 msknight

msknight
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 October 2006 - 09:58 AM

Hi All,

Sorry for being a complete newbie  ;D

A nice simple one for you.  I dabble in different languages, and am working on a PHP project to help people running L2J servers to administer the game.  My work is free to the community.

I'm having a problem, however, with code that is passing two variables by reference.  I understand what I am doing, and I understand the error, which is that passing variables by reference is depreciated in the later versions of PHP.  I just don't know enough to be confident of my solution.

Although setting the variable in the php.ini file will get around it, and I do set a temporary session variable, this isn't an ideal solution.  Also, although fumbling around in the dark is my prefered way of learning, when other people are using my code I sort of owe it to them to say, "I need help!" and then go get it ... hence I'm here.

$usetelnet = fsockopen($telnet_host, $telnet_port, &$errno, &$errstr, $telnet_timeout);
This is what I'm doing wrong ... $errno and $errstr are being passed by reference, but if I don't pass them by reference I'm sort of stuffed.

To my amateure mind, the other side is probably working on the reference and if I don't pass the right numbers, then it may well write in to an unknown place in memory.  That is why I'm concerned.

If I do something like ...
$errnoref = &$errno;
$errstrref = &$errstr;
$usetelnet = fsockopen($telnet_host, $telnet_port, $errnoref, $errstrref, $telnet_timeout);
... will that solve my problem; or will I just crash someones server!

Any help gratefully appreciated.

#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 06 October 2006 - 11:02 AM

What is your added reference for? PHP by default passes $errno and $errstr by reference, so you don't need to do what your doing and you can name them what ever you want...

fsockopen($telnet_host, $telnet_port, $dumb_error, $dumb_message, $telnet_timeout);

quick facts fsockopen!

1. if you pass (4) params

PHP uses...

your host variable
your port variable
your error number variable
your error message variable
the time out param is taken from the global default time out

2. if you pass (3) params

your host variable
your port variable
error number is not assigned
error message is not assigned
your time out variable (ie: on windows running the PHP ISAPI filter, timeout can never be set at runtime, it's always taken from the PHP.INI)


me!

#3 msknight

msknight
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 06 October 2006 - 11:09 AM

Thanks very much for the help.

I was right at the begining of my PHP project when I needed to talk with the gameserver via telnet, so I had to copy someone elses telnet code and adjust it.  The last two months has seen me learn a lot about PHP, but still not enough to get to this kind of detail yet.



#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 06 October 2006 - 11:19 AM

I posted a few facts so you understand PHP(s) way of handling params for this function! The manual function definition can sometimes be misleading, but if you read through the facts and some of the user comments it will help you find out things that the developers don't always include!

me!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users