Author Topic: Need Help Debugging Code  (Read 1098 times)

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Need Help Debugging Code
« on: April 30, 2007, 01:29:26 PM »
Okay, I got this function to calculate the experience.
I can input the attacker level, the defender level and wheter it's a match against a trainer or against a player.
here's the function:
Code: [Select]
function experience($alvl,$dlvl,$t)
{
if($t == 1)
{
$exp = rand(($dlvl * 19),($dlvl * 19) + rand(0,2*$dlvl));
$silver = round(rand($dlvl*1.5, 2 * $dlvl));
}
else
{
$exp = round($dlvl * 40 * ($dlvl / $alvl));
if($dlvl == $alvl) $exp = round($exp * 1.5);
elseif($dlvl > $alvl) $exp = $exp * 2;

$silver = round((2/3) * $exp);

}

$ret['silver'] = $silver;
$ret['exp'] = $exp;
return $ret;
}

But this doesn't work and I dont know why! If i take out the lines $ret[silver] = $silver; and $ret[exp] = $exp; in the else {} statement, it works. If I leave them in, it wont work. Meaning it won't work as in the whole page is blank (this is a function in a file that gets included on every page)

Can anyone tell me what's wrong here? (I've had this problem mutliple times now with the same method of returning an array by a function. Sometimes it worked when I put one line before the other, but not the other way around... I'm getting confused here)

- Sinzygy

Offline Broda

  • Level 13
  • *
  • Posts: 97
  • Reputation: +2/-0
    • View Profile
    • Nightfall Games
Re: Need Help Debugging Code
« Reply #1 on: April 30, 2007, 01:56:33 PM »
I'm still getting used to PHP syntax but shouldn't you use double quotes "" in the array

Instead of this:
Code: [Select]
$ret['silver'] = $silver;
$ret['exp'] = $exp;

This:
Code: [Select]
$ret["silver"] = $silver;
$ret["exp"] = $exp;

Just my first thought.

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Need Help Debugging Code
« Reply #2 on: April 30, 2007, 02:24:30 PM »
doesn't work neither with double-quotes (""), single quotes ('') nor nothing ().

Offline Zeggy

  • Global Moderator
  • Level 35
  • *****
  • Posts: 1,187
  • Reputation: +13/-4
    • View Profile
Re: Need Help Debugging Code
« Reply #3 on: April 30, 2007, 02:35:02 PM »
Using single or double quotes shouldn't matter in arrays :) I use single-quotes personally, but I see other people use double quotes.

I'm not sure what's wrong, but it might not be this particular code that is the problem. I used the code and tested it on a single PHP page, like this:

<?php
//your function here

$a experience(20,30,1);
print_r($a);
?>


I get "Array ( [silver] => 52 [exp] => 570 )" or similar returned using different values in the function. I don't get any errors at all.
Or maybe I am using the function in a wrong way?

Offline Broda

  • Level 13
  • *
  • Posts: 97
  • Reputation: +2/-0
    • View Profile
    • Nightfall Games
Re: Need Help Debugging Code
« Reply #4 on: April 30, 2007, 02:38:34 PM »
Maybe it's the way the page is included?

Try using require_once("file.php")

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Need Help Debugging Code
« Reply #5 on: April 30, 2007, 03:57:45 PM »
Broda: still nothing.

I use an include('file.php'); in the template. But I really don't think this is the problem.
And Zeggy: yes, I assume it will work, since I can't find any errors in the code itself. But it isn't working for me and I don't know why... its a realy bummer :(

Offline Sinzygy

  • Level 28
  • **
  • Posts: 420
  • Reputation: +11/-0
    • View Profile
Re: Need Help Debugging Code
« Reply #6 on: April 30, 2007, 04:35:46 PM »
okay, I finally managed to get it working:

instead of inserting each one by itself it made
Code: [Select]
$ret = array("exp"=>$exp,"silver"=>$silver);
return $ret;

that seemed to work... still haven't figured out why tho :(

maybe it was because I only ran it on my local machine and there might be some faulty setup. But I really wouldn't know how this could've happened...
« Last Edit: April 30, 2007, 04:38:31 PM by Sinzygy »

Offline Broda

  • Level 13
  • *
  • Posts: 97
  • Reputation: +2/-0
    • View Profile
    • Nightfall Games
Re: Need Help Debugging Code
« Reply #7 on: April 30, 2007, 04:48:11 PM »
Strange. Glad you got it working though :)

 


SimplePortal 2.3.3 © 2008-2010, SimplePortal