FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » missing variable
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
missing variable [message #175838] Sat, 29 October 2011 19:42 Go to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
here is a tiny piece of my script:

$history = "empty appointment filled ";
echo " history: $history\n";

when it executes I get a PHP Notice
PHP Notice: Undefined variable: history in
/var/www/MP2010-v2/classes/cSchedule.php on line 108

however if I use:
$history = "xxx";
echo " history: $history\n";

it executes fine.

To say I am puzzled is a vast understatement.
Yes, I did cut an paste from the script,
No, I did not include the other 200 lines.
If I paste it into a test script, it runs fine so it is obviously
in the other 200 lines.

If I rename the variable "hist" I get the same error

THis is running from the command line.

any thoughts ?

bill
Re: missing variable [message #175839 is a reply to message #175838] Sat, 29 October 2011 21:35 Go to previous messageGo to next message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma: 0
Senior Member
bill schrieb:

> $history = "empty appointment filled ";
> echo " history: $history\n";

Looks okay to me.

> when it executes I get a PHP Notice
> PHP Notice: Undefined variable: history in
> /var/www/MP2010-v2/classes/cSchedule.php on line 108

Assuming the two lines of code above are 107 and 108 (and that you
indeed copypasted them) I see no reason for this error.

> however if I use:
> $history = "xxx";
> echo " history: $history\n";
> it executes fine.

Just replacing "empty appointment filled " in the above code with "xxx"
has this effect? That simply cannot be.

> To say I am puzzled is a vast understatement.

From my own experience I can tell that such incomprehensible behaviour
usually indicates a very simple mistake.

> If I paste it into a test script, it runs fine so it is obviously in the
> other 200 lines.

So we will obviously need to see those other lines. Maybe you can trim
them down somewhat. And while doing this you might see the cause of the
problem yourself.

> If I rename the variable "hist" I get the same error

Global search & replace? Or manually just in those two lines?

> THis is running from the command line.

Shouldn't matter, but one never knows.

Greetings,
Thomas


--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
Re: missing variable [message #175844 is a reply to message #175839] Sun, 30 October 2011 12:59 Go to previous messageGo to next message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 10/29/2011 5:35 PM, Thomas Mlynarczyk wrote:
> bill schrieb:
>
>> $history = "empty appointment filled ";
>> echo " history: $history\n";
>
> Looks okay to me.
>
>> when it executes I get a PHP Notice
>> PHP Notice: Undefined variable: history in
>> /var/www/MP2010-v2/classes/cSchedule.php on line 108
>
> Assuming the two lines of code above are 107 and 108 (and that
> you indeed copypasted them) I see no reason for this error.
>
>> however if I use:
>> $history = "xxx";
>> echo " history: $history\n";
>> it executes fine.
>
> Just replacing "empty appointment filled " in the above code with
> "xxx" has this effect? That simply cannot be.
>
>> To say I am puzzled is a vast understatement.
>
> From my own experience I can tell that such incomprehensible
> behaviour usually indicates a very simple mistake.
>
>> If I paste it into a test script, it runs fine so it is
>> obviously in the other 200 lines.
>
> So we will obviously need to see those other lines. Maybe you can
> trim them down somewhat. And while doing this you might see the
> cause of the problem yourself.
>
>> If I rename the variable "hist" I get the same error
>
> Global search & replace? Or manually just in those two lines?
>
>> THis is running from the command line.
>
> Shouldn't matter, but one never knows.
>
> Greetings,
> Thomas
>
>

Here is the top half of the function, as it works:
---
public function mCreateAppointment($user, $apptTime, $location,
$duration, $standing, $ptNum, $notify, $comment ) { // create a
new appointment - may be non-pt or empty
// look for identical empty appointment
$sql = "select schedule_id
from schedule
where user='$user' and
apptTime = '$apptTime'and
duration = '$duration' and
appt_status = 'e'
limit 1";
$result =mysql_query( $sql, $this->connection) or
die("MySql ERROR in cSchedule on line " . __LINE__ . " " .
mysql_error()) ;

if (mysql_num_rows($result)) { // found an identical empty
appt, use it
$row = mysql_fetch_array($result);
$id = $row['schedule_id'];
if ($ptNum == "empty") return // do nothing, do not need to
create an empty appointment, as there is already an identical one

$hist = "";
$hist = "empty appointment filled ". nowUS() ." by "
..$_SESSION['current_user'] . "\n";

echo __CLASS__ . "-" . __FUNCTION__ . ": " . __LINE__ . "
history: $hist\n";

$sql = "update schedule set
user = '$user',
location='$location',
standing_script = '$standing',
appt_status = 'a',
patient_number = '$ptNum',
notify = '$notify',
comment = '$comment',
history = concat(history, '$hist')
where schedule_id ='$id' ";
echo __CLASS__ . "-" . __FUNCTION__ . ": " . __LINE__ . "
sql=$sql\n-------------------------------------------------------------\n";

$result = mysql_query( $sql, $this->connection) or die("MySql
ERROR in cSchedule on line " . __LINE__ . " " . mysql_error()) ;
return;
} //found an identical empty appt, use it

note:
$hist = "";
$hist = "empty appointment filled ". nowUS() ." by "
..$_SESSION['current_user'] . "\n";

If I remove the first line ($hist = "") I get the error message.
If I have both line in, the correct text is assigned to hist.

There are only 3 references to hist in the procedure, the two
lines above and the sql.

Although I hate to do this, I am willing to leave the null
assignment to keep the script running, but I sure would like to
know what is going on.

bill
Re: missing variable [message #175845 is a reply to message #175844] Sun, 30 October 2011 19:57 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 30 Oct 2011 08:59:44 -0400, bill wrote:

> if ($ptNum == "empty") return // do nothing, do not need to
> create an empty appointment, as there is already an identical one
> $hist = "empty appointment filled ". nowUS() ." by "
> .$_SESSION['current_user'] . "\n";

No ";" after the preceding "return"

so without the $hist=""; line what you have is:

if ($ptNum == "empty") return $hist = "empty appointment filled ". nowUS
() ." by " .$_SESSION['current_user'] . "\n";

In other words, the first $hist value after the return is only defined if
it's being returned from the function, because it comes between the if
($ptNum == "empty") and the next ";"

The // only creates comments to the end of line, it doesn't terminate the
return

So your if statement assigns the $hist value and immediately uses it as
the return value, when what you want is to assign the value if you didn't
return.

Rgds

Denis McMahon
Re: missing variable [message #175846 is a reply to message #175845] Sun, 30 October 2011 23:29 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 10/30/2011 3:57 PM, Denis McMahon wrote:
> On Sun, 30 Oct 2011 08:59:44 -0400, bill wrote:
>
>> if ($ptNum == "empty") return // do nothing, do not need to
>> create an empty appointment, as there is already an identical one
>> $hist = "empty appointment filled ". nowUS() ." by "
>> .$_SESSION['current_user'] . "\n";
>
> No ";" after the preceding "return"
>
> so without the $hist=""; line what you have is:
>
> if ($ptNum == "empty") return $hist = "empty appointment filled ". nowUS
> () ." by " .$_SESSION['current_user'] . "\n";
>
> In other words, the first $hist value after the return is only defined if
> it's being returned from the function, because it comes between the if
> ($ptNum == "empty") and the next ";"
>
> The // only creates comments to the end of line, it doesn't terminate the
> return
>
> So your if statement assigns the $hist value and immediately uses it as
> the return value, when what you want is to assign the value if you didn't
> return.
>
> Rgds
>
> Denis McMahon

Good eye, Dennis. I completely missed that one.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: missing variable [message #175847 is a reply to message #175846] Mon, 31 October 2011 00:37 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
On Sun, 30 Oct 2011 19:29:30 -0400, Jerry Stuckle wrote:

> On 10/30/2011 3:57 PM, Denis McMahon wrote:

>> No ";" after the preceding "return"

> Good eye, Dennis. I completely missed that one.

Yeah, I think the comment delimiter was visually masking the absence of
the terminator.

Rgds

Denis McMahon
Re: missing variable [message #175857 is a reply to message #175845] Mon, 31 October 2011 10:30 Go to previous message
bill is currently offline  bill
Messages: 310
Registered: October 2010
Karma: 0
Senior Member
On 10/30/2011 3:57 PM, Denis McMahon wrote:
> On Sun, 30 Oct 2011 08:59:44 -0400, bill wrote:
>
>> if ($ptNum == "empty") return // do nothing, do not need to
>> create an empty appointment, as there is already an identical one
>> $hist = "empty appointment filled ". nowUS() ." by "
>> .$_SESSION['current_user'] . "\n";
>
> No ";" after the preceding "return"
>
> so without the $hist=""; line what you have is:
>
> if ($ptNum == "empty") return $hist = "empty appointment filled ". nowUS
> () ." by " .$_SESSION['current_user'] . "\n";
>
> In other words, the first $hist value after the return is only defined if
> it's being returned from the function, because it comes between the if
> ($ptNum == "empty") and the next ";"
>
> The // only creates comments to the end of line, it doesn't terminate the
> return
>
> So your if statement assigns the $hist value and immediately uses it as
> the return value, when what you want is to assign the value if you didn't
> return.
>
> Rgds
>
> Denis McMahon

Good catch. Thank you so much !
bill
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: json and special chars
Next Topic: integer and string what's the difference ?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sun Nov 24 20:52:56 GMT 2024

Total time taken to generate the page: 0.04456 seconds