if and or

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
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

if and or

Post by Infinity »

could anyone tell me the format for the php if and statement ie

if($userdata['user_rank']==1 and $userdata['user_rank']==2)

and the php if or statement ie

if($userdata['user_rank']==1 or $userdata['user_rank']==2)

thanx
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

You've already got it! :D Those are correct, but are usually just used alternatively to:

Code: Select all

<?php
if($userdata['user_rank'] == 1 && $userdata['user_rank'] == 2)
{
  //dostuff
}

if($userdata['user_rank'] == 1 || $userdata['user_rank'] == 2)
{
  //dostuff
} 
?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

i dont see anythign wrong with that cept that the "and" needs to be && and the "or" should be ||... just operators.

To keep the code clean, atleast its how I do it is i assign the row results as a var, IE:

Code: Select all

<?php
$sql = "SELECT * from whatever";
$quer = mysql_query($sql);
$row = mysql_fetch_array($quer);
$exam = $row["user"];
?>
Just now you can use $exam instead of typing out the row call.

:/edit - man duff thats twice you got me today, your fired. =]
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

tim wrote:i dont see anythign wrong with that cept that the "and" needs to be && and the "or" should be ||... just operators.
Using "and" and "or" is perfectly acceptable. Only difference is [php_man]http://uk.php.net/manual/en/language.op ... precedence[/php_man]
Last edited by McGruff on Tue Aug 09, 2005 5:39 pm, edited 1 time in total.
Infinity
Forum Commoner
Posts: 44
Joined: Mon Feb 02, 2004 12:48 pm

Post by Infinity »

thanx guys my site is coming together nicely due to peeps like you helping us noobs :D
Post Reply