Changing a Variable Depending on the IF stament (sorted now)
Moderator: General Moderators
Changing a Variable Depending on the IF stament (sorted now)
Hi
i need help on making the variable change depending on the if
eg.
if ($users==1) {people=2}
kind of thing
i was thinging like
if ($users==1) {then people=2}
i know that wont work but thats what i mean
thanks reece
i need help on making the variable change depending on the if
eg.
if ($users==1) {people=2}
kind of thing
i was thinging like
if ($users==1) {then people=2}
i know that wont work but thats what i mean
thanks reece
Last edited by reecec on Wed May 31, 2006 11:10 am, edited 1 time in total.
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
You could use the ternary operator for a simple if ... else:
which is equivalent to:
Mac
Code: Select all
$people = ($users == 1) ? 2 : 'some other value';Code: Select all
if ($users == 1) {
$people = 2;
} else {
$people = 'some other value';
}thanks
Thanks for the code
i have tryed this and i get an error
Parse error: parse error, unexpected ';' in /home/reeceth/public_html/game/docrime.php on line 22
thanks alot reece
i have tryed this and i get an error
Code: Select all
$chance = ($level == 1) ? 4
?> THIS IS LINE 22 WHERE UNEXPECTED ; ISthanks alot reece
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
hi
$chance = ($level == 1) ? 4 : 4;
$chance = ($level == 2) ? 4 : 4;
$chance = ($level == 3) ? 3 : 3;
$chance = ($level == 4) ? 3 : 3;
$chance = ($level == 5) ? 2 : 2;
$chance = ($level == 6) ? 2 : 2;
i think this is just taking the last value
please help
and thanks
$chance = ($level == 2) ? 4 : 4;
$chance = ($level == 3) ? 3 : 3;
$chance = ($level == 4) ? 3 : 3;
$chance = ($level == 5) ? 2 : 2;
$chance = ($level == 6) ? 2 : 2;
i think this is just taking the last value
please help
and thanks
switch would be better:
Code: Select all
<?php
switch($level)
{
case 1:
case 2:
$chance = 4;
break;
case 3:
case 4:
$chance = 3;
break;
case 5:
case 6:
$chance = 2;
break;
}
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
Re: hi
In this code you are not setting a value based on an if, you are acutally setting a value to the same regardless. The switch statement is probably the better function to use (as was posted before me).reecec wrote:i think this is just taking the last valueCode: Select all
<?php $chance = ($level == 1) ? 4 : 4; $chance = ($level == 2) ? 4 : 4; $chance = ($level == 3) ? 3 : 3; $chance = ($level == 4) ? 3 : 3; $chance = ($level == 5) ? 2 : 2; $chance = ($level == 6) ? 2 : 2; ?>
So you know, with the ternary operator, this...
Code: Select all
<?php
if ($x == 5)
{
$var = 'not me';
}
else
{
$var = 'me';
}
?>Code: Select all
<?php
$var = ($x == 5) ? 'not me' : 'me';
?>