Changing a Variable Depending on the IF stament (sorted now)

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
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

Changing a Variable Depending on the IF stament (sorted now)

Post by reecec »

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
Last edited by reecec on Wed May 31, 2006 11:10 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You could use the ternary operator for a simple if ... else:

Code: Select all

$people = ($users == 1) ? 2 : 'some other value';
which is equivalent to:

Code: Select all

if ($users == 1) {
    $people = 2;
} else {
    $people = 'some other value';
}
Mac
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

thanks

Post by reecec »

Thanks for the code

i have tryed this and i get an error

Code: Select all

$chance = ($level == 1) ? 4
?>   THIS IS LINE 22 WHERE UNEXPECTED ; IS
Parse error: parse error, unexpected ';' in /home/reeceth/public_html/game/docrime.php on line 22


thanks alot reece
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A ternary requires three (3) expressions. You've given two (2).
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

thanks

Post by reecec »

thanks thats done it so i could just have the same value again just so its 3


thanks reece
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ternary syntax:

$var = ( if ) ? then : else;
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

hi

Post by reecec »

$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
User avatar
ok
Forum Contributor
Posts: 393
Joined: Wed May 31, 2006 9:20 am
Location: The Holy Land

Post by ok »

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;
}
?>
reecec
Forum Contributor
Posts: 218
Joined: Sun Apr 02, 2006 7:12 am

done

Post by reecec »

Thats perfect

thanks to all of you
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Re: hi

Post by RobertGonzalez »

reecec wrote:

Code: 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;
?>
i think this is just taking the last value
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).

So you know, with the ternary operator, this...

Code: Select all

<?php
if ($x == 5)
{
    $var = 'not me';
}
else
{
    $var = 'me';
}
?>
becomes this...

Code: Select all

<?php
$var = ($x == 5) ? 'not me' : 'me';
?>
Post Reply