undefined variable

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
ANYIM
Forum Newbie
Posts: 4
Joined: Fri Aug 13, 2010 1:46 am

undefined variable

Post by ANYIM »

hi,Anyone please help. Am getting this error Undefined variable: ipFound on line 34

here's the code.

$ipFound = true;
}

if ($ipFound) { <------ line 34
reset($valueSplitted);


Thanks for ur help.
dejvos
Forum Contributor
Posts: 122
Joined: Tue Mar 10, 2009 8:40 am

Re: undefined variable

Post by dejvos »

I think this code is not enough. I can guess only what it should be.

But I see curly bracket above don't you have a definition of variable in some condition?

For example like that:

Code: Select all

<?php
if(!empty($x) && $condition){
$ipFound = True;
}

if($ipFound){
// IP found
}
?>
In this case $ipFound is not defined when $x is empty. You need to add else branch.
ANYIM
Forum Newbie
Posts: 4
Joined: Fri Aug 13, 2010 1:46 am

Re: undefined variable

Post by ANYIM »

hi,

it's some code i found on the net :) and whn i tried to run it i got that error:

the whole code below.

$location = `which arp`;
$arpTable = `$location`;
$arpSplitted = split("\n",$arpTable);
$remoteIp = $GLOBALS['REMOTE_ADDR'];

foreach ($arpSplitted as $value) {

$valueSplitted = split(" ",$value);
foreach ($valueSplitted as $spLine) {
if (preg_match("/$remoteIp/",$spLine)) {
$ipFound = true;
}

if ($ipFound) { <-------------------------------------------------------------- error comes on this line

reset($valueSplitted);
foreach ($valueSplitted as $spLine) {
if (preg_match("/[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f][:-]".
"[0-9a-f][0-9a-f]/i",$spLine)) {
return $spLine;
}
}
}
$ipFound = false;
}
}
return false;
}
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: undefined variable

Post by shawngoldw »

Try;

Code: Select all

$valueSplitted = split(" ",$value);
foreach ($valueSplitted as $spLine) {
$ipFound = false;
if (preg_match("/$remoteIp/",$spLine)) {
$ipFound = true;
}
If the statement preg_match does not find a match, ipFound never gets defined. Even if it does find a match, I think ipFound is out of scope for the other if statement. This should fix both issues
ANYIM
Forum Newbie
Posts: 4
Joined: Fri Aug 13, 2010 1:46 am

Re: undefined variable

Post by ANYIM »

Thanks Shawn. Error disappeared :) but output is a blank page :(
shawngoldw
Forum Contributor
Posts: 212
Joined: Mon Apr 05, 2010 3:38 pm

Re: undefined variable

Post by shawngoldw »

No problem.

What do you want to see on the page? There are no echo's or print's.


Shawn
Post Reply