86774

Undefined variable and offset [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined" dir="ltr" rel="nofollow">“Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset” using PHP</a> <span class="question-originals-answer-count"> 28 answers </span> </li> </ul>

my errors is:

Notice: Undefined variable: return in C:\xampp\htdocs\server4.php on line 21

and the line 21 is:

$return .= fgets($this->_socket);

Notice: Undefined offset: 0 in C:\xampp\htdocs\server4.php on line 95

Notice: Undefined offset: 0 in C:\xampp\htdocs\server4.php on line 95

and the line 95 is:

$serverInfo = $master->server_getInfo($serverList[0]['ip'], $serverList[0]['port']);

and my full code:

<?php class Q3Master { private $_socket; private $_port = '20810'; private $_host; public function __construct($masterHost) { $this->_socket = fsockopen('udp://79.175.173.73',20810); stream_set_blocking($this->_socket,0); } public function master_listServers($timeout = '1') { fputs($this->_socket,str_repeat(chr(255),4).'getservers 69 empty full demo'."\n"); $time=time()+$timeout; while($time > time() || strpos($return,'EOT') === FALSE) { $return .= fgets($this->_socket); } $return = explode('\\',$return); unset($return[0]); unset($return[count($return)]); $iplist = array(); foreach($return as $server) { for($i = 0;$i < 4;$i++) $addr[] = ord($server[$i]); for($i = 4;$i < 6;$i++) $port .= dechex(ord($server[$i])); $port = hexdec($port); $iplist[] = array('ip' => join('.',$addr),'port' => $port); unset($addr); unset($port); } return $iplist; } function server_getInfo($adresse, $port) { if($port != 0) { $cmd = "\xFF\xFF\xFF\xFFgetstatus"; $f = fsockopen('udp://'.$adresse, $port); socket_set_timeout ($f, 1); fwrite ($f, $cmd); $data = fread ($f, 10000); fclose ($f); if($data) { $temp = explode("\x0a",$data); $list3 = explode("\\",substr($temp[1],1,strlen($temp[1]))); for ($i = 0;$i <= count($list3);$i = $i + 2) { $list[@$list3[$i]] = @$list3[$i + 1]; } array_pop($list); $players = array(); foreach($temp as $id => $player) { if($id != 0 AND $id != 1) { $infos = explode(' ', $player, 3); $name = explode('"', $infos[2]); $players[] = array('score' => $infos[0], 'ping' => $infos[1], 'name' => $name[1]); } } array_pop($players); $infos = array(); $infos = $list; $infos['players'] = $players; return $infos; } else return FALSE; } else return FALSE; } } $master = new Q3Master('79.175.173.73'); $serverList = $master->master_listServers(); $serverInfo = $master->server_getInfo($serverList[0]['ip'], $serverList[0]['port']); ?>

Answer1:

Add an initaliaze of the var return with:

$return = '';

try this:

public function master_listServers($timeout = '1') { $return = ''; fputs($this->_socket,str_repeat(chr(255),4).'getservers 69 empty full demo'."\n"); $time=time()+$timeout; while($time > time() || strpos($return,'EOT') === FALSE) { $return .= fgets($this->_socket); } $return = explode('\\',$return); unset($return[0]); unset($return[count($return)]); $iplist = array(); foreach($return as $server) { for($i = 0;$i < 4;$i++) $addr[] = ord($server[$i]); for($i = 4;$i < 6;$i++) $port .= dechex(ord($server[$i])); $port = hexdec($port); $iplist[] = array('ip' => join('.',$addr),'port' => $port); unset($addr); unset($port); } return $iplist; }

Answer2:

<blockquote>

Notice: Undefined variable: return in C:\xampp\htdocs\server4.php on line 21

</blockquote>

Then define that variable: $return = null.

<blockquote>

Notice: Undefined offset: 0 in C:\xampp\htdocs\server4.php on line 95

</blockquote>

Then either define that offset: $serverList[0] = array('ip' => null, 'port' => null), or check whether the offset is defined before using it:

if (isset($serverList[0])) { $serverInfo = $master->server_getInfo($serverList[0]['ip'], $serverList[0]['port']); }

But first you should investigate why you where expecting $return and $serverList[0] to be defined. Maybe those variables should have been provided by a different part of the code, that is broken.

Answer3:

You need to define $return before using it, because if the while loop will not being executed $return is not defined and therefore cannot being used as a param of explode().

$return = ''; // <-- define it as an empty string while($time > time() || strpos($return,'EOT') === FALSE) { $return .= fgets($this->_socket); } $return = explode('\\',$return);

Recommend

  • check connection with ssh2 functions in PHP
  • Php, socket and connecting to pop3-server [duplicate]
  • PHP SOAP File Upload
  • INFO_MSG method fails in ejabberd
  • Parsing JSON Lines with Alamofire/Codable
  • udp forwarding to emulator
  • QT background process for keyboard input sniffing
  • cf.net exception and other logging
  • Help accessing xml attribute in php [duplicate]
  • how to pass url variable to next pages in php pagination
  • Global PHP class in functions?
  • Swiftmailer email message returns error when attaching file
  • form_validation class does not load (codeigniter)
  • Adding new table in rest api of prestashop webservice
  • Doctrine2 bulk import try to work with another entity
  • Join two tables and save into third-sql
  • JSON with duplicate key names losing information when parsed
  • Display Images one by one with next and previous functionality
  • Why is the timeout on a windows udp receive socket always 500ms longer than set by SO_RCVTIMEO?
  • Matplotlib draw Spline from multiple points
  • XCode can't find symbols for a specific iOS library/framework project
  • Calling of Constructors in a Java
  • Compare two NSDates in iPhone
  • Transpose CSV data with awk (pivot transformation)
  • Use group_by to filter specific cases while keeping NAs
  • log4net write single file for each call to log.info
  • Benchmarking RAM performance - UWP and C#
  • Acquiring multiple attributes from .xml file in c#
  • JTable with a ScrollPane misbehaving
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • Binding checkboxes to object values in AngularJs
  • failed to connect to specific WiFi in android programmatically
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?