Conditional Statements Troubleshooting

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
Majoraslayer
Forum Commoner
Posts: 64
Joined: Thu Jun 30, 2005 11:50 am
Location: In Your Mind...
Contact:

Conditional Statements Troubleshooting

Post by Majoraslayer »

Alright, so I'm off to writing my first useful PHP application. Its a user registration system that uses the same php file to display the user control panel or the registration fields and agreements (yes, its inspired by the way phpBB works). Its the first time I've tried conditional statements on my own...and I've screwed up already. Here's my code:

Code: Select all

<?php
if ( $mode=&quote;profile&quote; )
{
echo &quote;їProfile Control Panel Placeholder]&quote;;
}
if ( $mode=&quote;register&quote; and $coppa<>&quote;agree&quote; )
{
echo &quote;їCoppa Agreement Placeholder]&quote;;
}
if ( $mode=&quote;register&quote; and $coppa=&quote;agree&quote; )
{
echo &quote;їRegistration Form Here]&quote;;
}
?>
And to see what its doing, check out

http://www.zorasdomain.com/usercp.php

Basically, what it is supposed to echo is placeholders to check my conditional statements so I can plug in those elements later. It is supposed to check $mode to decide what the user needs (registration or control panel). Then, if it decides that $mode="register", it is supposed to check if $coppa="yes". If it does, it should display the registration form. If it doesn't, then it should display the COPPA agreement. However, its displaying both (I'm using placeholders for the agreement and the form, if you hadn't caught that above). Does anyone know what I'm doing wrong here? Feel free to educate me a bit, as I'm still a complete PHP noob.

Also, clicking the above link shouldn't display anything, as $mode and $coppa wouldn't be assigned a value. No matter what they equal it seems, both conditions are output.
Last edited by Majoraslayer on Tue Jul 12, 2005 11:16 pm, edited 2 times in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

First off use == and not =

Secondly

Code: Select all

if ( $mode="register" ) and ( $coppa<>"agree" )
Should be

Code: Select all

if ( $mode == 'register' && $coppa != 'agree' )
Majoraslayer
Forum Commoner
Posts: 64
Joined: Thu Jun 30, 2005 11:50 am
Location: In Your Mind...
Contact:

Post by Majoraslayer »

I knew about the second errors; you just saw them before I edited my code. It should now just be what I'm focusing on, and with those problems fixed :oops: .

As for the other, it seems to have fixed my problems. I hadn't thought to check for the same data type as well. Its my first time trying to pass variables between pages, so I didn't know to do that >_<.
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post by Sphen001 »

Hi,

The way your code looks right now, it's possible that more that one section will be displayed. If you only want one section to display at any given time, use elseif

Code: Select all

<?php
if ( $mode == "profile" )
{
  echo "[Profile Control Panel Placeholder]";
}
elseif ( $mode == "register" && $coppa != "agree" )
{
  echo "[Coppa Agreement Placeholder]";
}
elseif ( $mode == "register" && $coppa == "agree" )
{
  echo "[Registration Form Here]";
}
?>
Hope this helps :D

Sphen001
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Majoraslayer wrote:As for the other, it seems to have fixed my problems. I hadn't thought to check for the same data type as well.
assigment operator =
comparison operator == both sides are same but might not be same type
comparison operator of the same type === both sides are same and same type
Does not equal each other <> or !=
User avatar
harrisonad
Forum Contributor
Posts: 288
Joined: Fri Oct 15, 2004 4:58 am
Location: Philippines
Contact:

Post by harrisonad »

Code: Select all

if($mode=="profile"){
  echo "[Profile Control Panel Placeholder]";
}elseif($mode=="register"){
  if($coppa!="agree"){
    echo "[Coppa Agreement Placeholder]";
  }elseif($coppa=="agree"){
    echo "[Registration Form Here]";
  }
}
I just removed the redundancies.
Post Reply