Page 1 of 1
if to variable
Posted: Fri Nov 19, 2004 4:37 pm
by SidewinderX
is there a way i can store an if statment as a variable?
heres what im doing
Code: Select all
<?php
$officialrank = stristr($str, 'Rank:');
sscanf($remove1, 'Rank: %s' , $rank);
if ($rank==5-$star) print '5-Star General';
else if ($rank == "4-Star") print '4-Star General';
else if ($rank == "3-Star") print '3-Star General';
else if ($rank == "2-Star") print '2-Star General';
else if ($rank == "1-Star") print '1-Star General';
else if ($rank == "Colonel") print 'Colonel';
else if ($rank == "Lieutenant") print 'Lieutenant Colonel';
else if ($rank == "Major") print 'Major';
else if ($rank == "Captian") print 'Captian';
else if ($rank == "First Lieutenant") print 'First Lieutenant';
else if ($rank == "Second Lieutenant") print 'Second Lieutenant';
else if ($rank == "Sargent") print 'Sargent Major';
else if ($rank == "Master") print 'Master Sargent';
else if ($rank == "Sargent") print 'Sargent First Class';
else if ($rank == "Sargent") print 'Sargent';
else if ($rank == "Copral") print 'Copral';
else if ($rank == "Private") print 'Private First Class';
else if ($rank == "Private") print 'Private';
else if ($rank == "New") print 'New Recruit';
?>
how can i store that so that the output (either of the 18 ranks) can be displayed as a single variable?
Posted: Fri Nov 19, 2004 5:05 pm
by dull1554
how you have it now the rank is stored in a single variable - $rank
Re: if to variable
Posted: Fri Nov 19, 2004 5:16 pm
by John Cartwright
Code: Select all
<?php
$officialrank = stristr($str, 'Rank:');
sscanf($remove1, 'Rank: %s' , $rank);
if ($rank==5-$star) print '5-Star General';
else if ($rank == "4-Star") $rankdesc = '4-Star General';
else if ($rank == "3-Star") $rankdesc = '3-Star General';
else if ($rank == "2-Star") $rankdesc = '2-Star General';
else if ($rank == "1-Star") $rankdesc = '1-Star General';
else if ($rank == "Colonel") $rankdesc = 'Colonel';
else if ($rank == "Lieutenant") $rankdesc = 'Lieutenant Colonel';
else if ($rank == "Major") $rankdesc = 'Major';
else if ($rank == "Captian") $rankdesc = 'Captian';
else if ($rank == "First Lieutenant") $rankdesc = 'First Lieutenant';
else if ($rank == "Second Lieutenant") $rankdesc = 'Second Lieutenant';
else if ($rank == "Sargent") $rankdesc = 'Sargent Major';
else if ($rank == "Master") $rankdesc = 'Master Sargent';
else if ($rank == "Sargent") $rankdesc = 'Sargent First Class';
else if ($rank == "Sargent") $rankdesc = 'Sargent';
else if ($rank == "Copral") $rankdesc = 'Copral';
else if ($rank == "Private") $rankdesc = 'Private First Class';
else if ($rank == "Private") $rankdesc = 'Private';
else if ($rank == "New") $rankdesc = 'New Recruit';
echo 'Your rank is '.$rankdesc;
?>
althought you should be using a [php_man]switch[/php_man] for this
Posted: Fri Nov 19, 2004 5:17 pm
by SidewinderX
that wont work because
$rank = 5-Star
$rank = 4-Star
$rank = 3-Star
ect...
once it goes through the if statements it changes 5-Star to 5-Star General
4-Star to 4-Star General and that is what i want to store as the variable
Re: if to variable
Posted: Fri Nov 19, 2004 5:24 pm
by MarK (CZ)
SidewinderX wrote:is there a way i can store an if statment as a variable?
heres what im doing
* php code *
how can i store that so that the output (either of the 18 ranks) can be displayed as a single variable?
Code: Select all
<?php
$officialrank = stristr($str, 'Rank:');
sscanf($remove1, 'Rank: %s' , $rank);
switch ($rank) {
case "5-Star" : $person = '5-Star General'; break;
case "4-Star" : $person = '4-Star General'; break;
// ...
// ...
// ...
case "New" : $person = 'New Recruit'; break;
default : $person = 'Unknown'; break;
}
echo $person;
?>
BTW, I think that you have some spelling mistakes in those ranks (copral/corporal, sargent/sergeant)...
Edit:
SidewinderX wrote:that wont work because
$rank = 5-Star
$rank = 4-Star
$rank = 3-Star
ect...
once it goes through the if statements it changes 5-Star to 5-Star General
4-Star to 4-Star General and that is what i want to store as the variable
You want to store it in the same var? or ...?

Posted: Fri Nov 19, 2004 6:20 pm
by rehfeld
theres also an error on line 5
should be
if ($rank== '5-star')
you had
if ($rank==5-$star)
you could also use an array for this, it would cut down on the syntax, and (maybe?) be faster
Code: Select all
<?php
$available_ranks = array(
'rank_name' => 'description',
'5-Star' => '5 star general',
'4-Star' => '4 star general',
'Sargent' => 'Sargent Major',
);
$rank = '5-Star';
if (isSet($available_ranks[$rank])) {
$description = $available_ranks[$rank];
} else {
$description = 'unknown';
}
echo $description; // 5 star general
?>
using an array like this would also make adding future ranks much easier
you could even store the available_ranks into a database or flat file, and never have to add more case statements
Posted: Fri Nov 19, 2004 7:31 pm
by SidewinderX
thanks guys...i used marks idea..when i first read about arrays it confused the heck out of me so i try to stay away from them if i can lol... thanks again