28, May 2012

can this be simplified? - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: can this be simplified?
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: can this be simplified?  (Read 1503 times)
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« on: Sep 18, 2006, 08:40:00 pm »

this is kind of sloppy i think. i am sure there is a better way to determine this:

Code:
<?php

if ($melee_combat $range_combat
$class_combat $melee_combat; }
elseif (
$melee_combat $mage_combat
$class_combat $melee_combat; } 
elseif (
$range_combat $melee_combat
$class_combat $range_combat; } 
elseif (
$range_combat $mage_combat
$class_combat $range_combat; } 
elseif (
$mage_combat $melee_combat
$class_combat $mage_combat; } 
elseif (
$mage_combat $range_combat
$class_combat $mage_combat; }

?>


Last blog : phpHaze 1.59.1 in Development
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #1 on: Sep 18, 2006, 09:07:33 pm »

It's kind of hard to understand what you want to do exactly. Try this: tell us in simple steps what you want to do (in plain English) from that we can make your code (probably using case)


Last blog : Are You Stumbling Yet?
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #2 on: Sep 18, 2006, 09:13:18 pm »

One thing I quickly notice:

Code:
<?php

if ($melee_combat $range_combat

elseif (
$range_combat $melee_combat

?>

the second has to be true, if the first statement wasn't, so it can be ignored (unless they are the same value, which you are not checking for any of the variables...)


Last blog : Are You Stumbling Yet?
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #3 on: Sep 18, 2006, 09:19:14 pm »

Oke, I think I see what you want to do: you have 3 variables and you want $class_combat to have the value of the variable with the highest value of the 3 right? Try this:

Code:
<?php

$class_combat 
$mage_combat;
if(
$range_combat $class_combat)
{
$class_combat $range_combat;}
if(
$melee_combat $class_combat)
{
$class_combat $melee_combat;}

?>
« Last Edit: Sep 18, 2006, 09:21:17 pm by Mind_nl »


Last blog : Are You Stumbling Yet?
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #4 on: Sep 18, 2006, 10:02:55 pm »

Or put all those values in an array, sort them and get the biggest value (but maybe this would be slower.....)

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #5 on: Sep 18, 2006, 11:33:41 pm »

Or put all those values in an array, sort them and get the biggest value (but maybe this would be slower.....)

this is more the answer i was looking for.. so i would store them in an array then select the MAX value?


Last blog : phpHaze 1.59.1 in Development
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 5799
46391 credits
Members referred : 3



« Reply #6 on: Sep 18, 2006, 11:34:55 pm »

I would say try Mind_nl's way better

Trial and Error my two best teachers Cool
Join us @ facebook or twitter

Last blog : Butterfly Marketing 2.0
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #7 on: Sep 18, 2006, 11:36:28 pm »

the method he proposed last will not work, because you say class combat = mage combat at first, so the rest of the if/else is irrelevant.. we ahve 3 variables, mage combat, range combat, and melee combat. we want to find the highest value of those 3 and set that to be the class combat Smiley


Last blog : phpHaze 1.59.1 in Development
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #8 on: Sep 18, 2006, 11:38:43 pm »

thats exactly what its doing, you set it to the first value first and then override this value if the other turns out to be bigger. The if's are comparing your variable with whats in $class_combat at the time
« Last Edit: Sep 18, 2006, 11:40:38 pm by Mind_nl »


Last blog : Are You Stumbling Yet?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #9 on: Sep 19, 2006, 12:34:01 am »

so you do not think using an array would be better?


Last blog : phpHaze 1.59.1 in Development
Global Moderator
Internet Junkie
*****
Gender: Male
Posts: 1807
9006 credits
Members referred : 6



« Reply #10 on: Sep 19, 2006, 12:51:22 am »

Like Nikolas said, the array way would probably be slower. You have to create the array, fill it, sort it and extract the highest value. This will cause some overhead. Probably not noticable in this case but still.
Heres another way I found which is even more elegant:
Code:
<?php

$class_combat 
MAX($mage_combat,MAX($range_combat,$melee_combat));

?>

Now that's some compact coding right there!  Cool


Last blog : Are You Stumbling Yet?
aka J Love
Community Supporter ?
Bill Gates is my home boy
*****
Gender: Male
Posts: 886
1148 credits
Members referred : 4



« Reply #11 on: Sep 19, 2006, 02:16:20 am »

yes buddy thats exactly what im talking about.. thank you.. i mentioned MAX earlier!


Last blog : phpHaze 1.59.1 in Development
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=4152
Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: can this be simplified?
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 02:23:44 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.