if statement

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
toshesh
Forum Commoner
Posts: 33
Joined: Thu Jun 19, 2003 9:32 pm

if statement

Post 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";
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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";
}
toshesh
Forum Commoner
Posts: 33
Joined: Thu Jun 19, 2003 9:32 pm

Post 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++
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post 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.
toshesh
Forum Commoner
Posts: 33
Joined: Thu Jun 19, 2003 9:32 pm

Post by toshesh »

thanks for your help. I shall look in the manual
User avatar
evilmonkey
Forum Regular
Posts: 823
Joined: Sun Oct 06, 2002 1:24 pm
Location: Toronto, Canada

Post by evilmonkey »

Let me know, because this might be interesting.

Cheers!
qartis
Forum Contributor
Posts: 271
Joined: Sat Dec 14, 2002 4:43 pm
Location: BC, Canada
Contact:

Post by qartis »

Code: Select all

<?
$ww = "aa"; 
echo ($ww == "aa") ? "bb":"cc";
?>
toshesh
Forum Commoner
Posts: 33
Joined: Thu Jun 19, 2003 9:32 pm

Post by toshesh »

yeah you're the one!!
toshesh
Forum Commoner
Posts: 33
Joined: Thu Jun 19, 2003 9:32 pm

Post 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";
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Basically...

Code: Select all

echo ($ww == "aa") ? "bb":"cc"; 
// Is the same as
if ( $ww == "aa") { echo "bb"; } else { echo "cc"; }
Image Image
toshesh
Forum Commoner
Posts: 33
Joined: Thu Jun 19, 2003 9:32 pm

Post by toshesh »

yep but shorter
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

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