Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts

    is there a better way to write this:

    is there a better way to write this instead of having so many if statements? or is it fine how it is?

    PHP Code:
    if ($newchance0 100) {
    $newchance0 100
    }
    if (
    $newchance1 100) {
    $newchance1 100
    }
    if (
    $newchance2 100) {
    $newchance2 100
    }
    if (
    $newchance3 100) {
    $newchance3 100
    }
    if (
    $newchance4 100) {
    $newchance4 100
    }
    if (
    $newchance5 100) {
    $newchance5 100

    cheers.

    Dan
    Last edited by Dan13071992; 02-01-2012 at 10:34 PM.
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #2
    Regular Coder
    Join Date
    May 2007
    Posts
    101
    Thanks
    16
    Thanked 12 Times in 12 Posts
    Do it like this.

    PHP Code:
    if ($newchance0 100$newchance0 100;

    if (
    $newchance1 100$newchance1 100;

    if (
    $newchance2 100$newchance2 100;

    if (
    $newchance3 100$newchance3 100;

    if (
    $newchance4 100$newchance4 100;

    if (
    $newchance5 100$newchance5 100

    Or use an array.

    for ($i = 0; $i < 6; $i++) if ($newchance[$i] > 100) $newchance[$i] = 100;

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Newchance isn't an array, but you could use a concat and ${'newchance' . $i} instead (or variable variables, which I never recommend).

    I'd suggest making an array as well. You could walk the array instead:
    PHP Code:
    $newchance = array(10478210726, ...);
    function 
    capChance(&$v$k$max 100)
    {
        
    $v $v 100 $v 100 $v;
    }
    array_walk($newchance'capChance');
    print_r($newchance); 
    107 will be capped at 100.

  • #4
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    i believes it was an array as its used like this:

    $chance = explode("-", $fetch->crimechance);
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Dan13071992 View Post
    i believes it was an array as its used like this:

    $chance = explode("-", $fetch->crimechance);
    $chance here is an array. Not sure what $newchanceX is from though, as those are scalar numbers.

  • #6
    Regular Coder
    Join Date
    Dec 2010
    Location
    Kent, UK
    Posts
    573
    Thanks
    23
    Thanked 10 Times in 10 Posts
    i was using this:

    $newchance5 = $chance[5];

    just so i knew what was what at the time of writing my previous part of code
    http://360-tactics.co.uk/forum/index.php

    Crime-Wave

    please post your code wrapped in tags
    please post your PHP wrapped in tags

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Oh yeah, then just use the $chance and either iterate or map for sure. This can be done prior to the creation of the $newchanceX variables if you need to keep them.

    Edit:
    I biffed my walk function too. It *should* actually work, but its (IMO) wrong. That should be using just 100, not $v = 100.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •