Page 1 of 1

if statement

Posted: Thu Jun 26, 2003 9:24 pm
by toshesh
Yes I know there's a syntax error and I know it's a basic thing but could someone help me?

$ww = "aa";
$ww == "aa"? echo "bb";echo "cc";

Posted: Thu Jun 26, 2003 9:28 pm
by evilmonkey
I think you want this:

Code: Select all

$ww="aa";
if ($ww=="aa")
{
echo "aa";
}
else if ($ww=="bb")
{
echo "bb";
}
else
{
echo "Not aa or bb";
}

Posted: Thu Jun 26, 2003 9:32 pm
by toshesh
it's not that.

I saw somewhere that a conditional statement can be made using "?" followed by actions. something like...

condition?if yes do this:if not do this;

I'm not sure if it exsits in php but pretty sure it does in c++

Posted: Thu Jun 26, 2003 9:34 pm
by evilmonkey
hmm, I'm not too familiar with C++, and I definitly don't remeber that in C (unless I've forgotten it entirely). I've never come accross anything like this in PHP, try querying the PHP manual (http://www.php.net), see if you can get anything.

Good luck.

Posted: Thu Jun 26, 2003 9:38 pm
by toshesh
thanks for your help. I shall look in the manual

Posted: Thu Jun 26, 2003 9:47 pm
by evilmonkey
Let me know, because this might be interesting.

Cheers!

Posted: Thu Jun 26, 2003 10:32 pm
by qartis

Code: Select all

<?
$ww = "aa"; 
echo ($ww == "aa") ? "bb":"cc";
?>

Posted: Thu Jun 26, 2003 11:58 pm
by toshesh
yeah you're the one!!

Posted: Fri Jun 27, 2003 12:14 am
by toshesh
this works too:

$ww = "aa";
($ww == "aa")?$pp = true:$pp = false;

//testing part
if ($pp == false)
echo "false";
else if ($pp == true)
echo "true";
else
echo "nothing";

Posted: Fri Jun 27, 2003 2:02 am
by phice
Basically...

Code: Select all

echo ($ww == "aa") ? "bb":"cc"; 
// Is the same as
if ( $ww == "aa") { echo "bb"; } else { echo "cc"; }

Posted: Fri Jun 27, 2003 5:38 am
by toshesh
yep but shorter

Posted: Fri Jun 27, 2003 6:02 am
by twigletmac
It is also possible to shorten:

Code: Select all

($ww == "aa")?$pp = true:$pp = false;
to

Code: Select all

$pp = ($ww == 'aa') ? true : false;
Mac