Why The value is old

Posted by Tianxz on Fri 21 May 2010 05:29 PM — 8 posts, 32,961 views.

#0
I have a trigger that get value $a from command hp.

In PHP, The code is
Function aaa()
{
$world->Excute('hp');//This command will get new value for $a
$world->DoAfterNote(2,$a);
}

But the result is The value of last time. It doesn't the value that had refreshed in 2s. I think The DoAfterNote only delay the display. But the content wasn't the result after 2s. Isn't it? Could you tell me which command can display real result.
USA #1
When this code is evaluated:
$world->DoAfterNote(2,$a);

The current value of $a is passed to DoAfterNote.

If you want the future value of $a to be used, you need to evaluate it again, for example by registering a function call with DoAfter rather than using DoAfterNote, such that the function call picks up the (then-current) value of $a.
USA #2
$world->DoAfterSpecial(2, "$world->Note($a)", 12)

This will take the text in the middle and run it as script (that's what the 12 is for) after 2 seconds. Since the string isn't evaluated until 2 seconds later, you get the value of $a at that point in time.
USA #3
Ah, no, PHP will evaluate $a as-is.

You need to use single-quotes to have it not evaluate $a.

$ cat test.php
<?
    $a = 5;
    print("Hello $a\n");
    print('Goodbye $a' . "\n");
?>
$ php test.php
Hello 5
Goodbye $a
$
Amended on Fri 21 May 2010 08:07 PM by David Haley
USA #4
Whoops, I forgot about that! Thanks DH.

$world->DoAfterSpecial(2, '$world->Note($a)', 12)
USA #5
It's kind of annoying when you have special behavior due to which quotation mark like that, especially when you come from lots of Lua and Python where ' and " mean the same thing. :P FWIW I often forget it too, and I tend to use single quotes and my variables never get subbed in when I write Perl or PHP... (although I write almost no PHP these days)
Australia Forum Administrator #6
Personally I'm not that keen on languages that inline variables like that. For example, if you want to send "the contents of variable 'a' followed by 'a'" then this won't work:


$aa


... because that is the contents of variable 'aa'.
USA #7
Does PHP have anything spiffy to get around that like the following in Perl?
 ${a}a