-2

I am trying to make a simple combat script for a game so far so good but this piece of code which handles the fighting always returns the following errors:

Notice: Use of undefined constant ime - assumed 'ime' in C:\Program Files (x86)\wamp\www\php\gozd.php on line 17

Notice: Use of undefined constant napad - assumed 'napad' in C:\Program Files (x86)\wamp\www\php\gozd.php on line 18

etc... I hope someone can spot the problem?

if($_POST['action'] == 'Napadi') {
    
    $igralec_ime = $_SESSION['username'];
    $igralec = array (
                      ime           => $igralec_ime,
                      napad     => prikazi_stat('ofe',$igralec_ime),
                      obramba   => prikazi_stat('def',$igralec_ime),
                      curhp         => prikazi_stat('curhp',$igralec_ime)
                     );
    
    $monster_ime = $_POST['monster'];
    $monster = array (
                      ime           => $monster_ime,
                      napad     => prikazi_monster_stat('atk',$monster_ime),
                      obramba   => prikazi_monster_stat('def',$monster_ime),
                      curhp         => prikazi_monster_stat('maxhp',$monster_ime)
                     );       
     
    $combat = array();
    $turns = 0;     
    while($igralec['curhp'] > 0 && $monster['curhp'] > 0) {
        
        if($turns % 2 != 0) {
            $napadalec = &$monster;
            $branilec = &$igralec; } 
        else {
            $napadalec = &$igralec;
            $branilec = &$monster; }
         
        $damage = 0;    
        if($napadalec['napad'] > $branilec['obramba']) {
            $damage = $napadalec['napad'] - $branilec['obramba']; }
           
        $branilec['curhp'] -= $damage;
        $combat[$turns] = array(
            napadalec   =>  $napadalec['ime'],
            branilec    =>  $branilec['ime'],
            damage      =>  $damage
                               );
         $turns++; }
     
    update_stat('curhp',$igralec_ime,$igralec['curhp']);
    if($igralec['curhp'] > 0) {
        update_stat('cek',$igralec_ime,prikazi_stat('cek',$igralec_ime)+ prikazi_monster_stat('cek',$monster_ime)); 
        $zmaga = 1;
        $cekini = prikazi_monster_stat('cek',$monster_ime); }
    else {
        $zguba = 1; } }
Community
  • 1
  • 1
  • 1
    Sounds like you should be quoting ime, napad, etc. i.e. `ime => $igralec_ime,` should be `'ime' => $igralec_ime,` – StephenTG Jul 30 '13 at 16:58
  • you have all array index messed up. the names `ime`, `napadalec`, `npad` etc are treated as const by php. so you get the errors – bansi Jul 30 '13 at 17:01

1 Answers1

3

Your array keys are not quoted. The code should read:

"ime"       => $igralec_ime,
"napad"     => prikazi_stat('ofe',$igralec_ime),
"obramba"   => prikazi_stat('def',$igralec_ime),
"curhp"     => prikazi_stat('curhp',$igralec_ime)

However, PHP is "helping" you by saying "gee, I have no idea what ime is -- the programmer probably meant "ime" (with quotes), so let's pretend that is what I just saw".

It is very unfortunate that PHP behaves like this today (the reasons are historical and best left untold), but at least it has the decency to notify that something might be amiss.

Jon
  • 428,835
  • 81
  • 738
  • 806