if to variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

if to variable

Post 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?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

how you have it now the rank is stored in a single variable - $rank
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: if to variable

Post 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
Last edited by John Cartwright on Fri Nov 19, 2004 5:17 pm, edited 1 time in total.
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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
User avatar
MarK (CZ)
Forum Contributor
Posts: 239
Joined: Tue Apr 13, 2004 12:51 am
Location: Prague (CZ) / Vienna (A)
Contact:

Re: if to variable

Post 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 ...? :?
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Post 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
Post Reply