php if or

Can some one please explain to me why the below does not work for me.

if($row_rs_media_new['category'] == 'EFI VUTEk' || 'EFI VUTEk Accessories') {
	  $roll_price = $row_rs_media_new['cost']; 
 }

Open in new window


but if I separate to this below is does?

if($row_rs_media_new['category'] == 'EFI VUTEk') {
	  $roll_price = $row_rs_media_new['cost']; 
  }
  
  if($row_rs_media_new['category'] == 'EFI VUTEk Accessories') {
	  $roll_price = $row_rs_media_new['cost']; 
  }

Open in new window


What am I doing wrong with the OR operator? I just want to ask if $row_rs_media_new['category'] is equal to 'EFI VUTEk' or 'EFI VUTEk Accessories'.
petewinterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Marco GasiFreelancerCommented:
Because the syntax is wrong:

if($row_rs_media_new['category'] == 'EFI VUTEk' || $row_rs_media_new['category'] == 'EFI VUTEk Accessories') {
	  $roll_price = $row_rs_media_new['cost']; 
 }

Open in new window

0
Rgonzo1971Commented:
HI,

pls try
if($row_rs_media_new['category'] == 'EFI VUTEk' || $row_rs_media_new['category'] == 'EFI VUTEk Accessories') {
	  $roll_price = $row_rs_media_new['cost']; 
 }

Open in new window

Regards
0
Julian HansenCommented:
[No points please]
Both experts above have answered the question with examples. Here is a more detailed explanation

The if statement works on expressions in other words
if (expr) {
   // Expr evaluates to true then do this bit
}
else {
  // otherwise do this bit
}

Open in new window

The expression evaluates to a boolean value of true or false and based on that the if fires and if false the else (if it exists) fires.

The expression itself can be made up of many sub expressions separated by AND (&&) and OR (||). Each of these is evaluated as a standalone expression in other words if you have
if (A || B) 

Open in new window

when B is evaluated it is done so completely independently of A - nothing from A is carried forward.

Your if statement was evaluated as

if (A == B || C)

The first test would be to see if A==B - if this is true then C is ignored and the if fires.
If A <> B then the next test is C. This is not compared to A because A was in the previous expression and does not carry forward so the expression reduces simply to C.

If C is the value true or is not equal to 0 then C will evaluate to true.
If C is the value false or 0 or null then it will evaluate to false.

To check for C being equal to A you have to expressly type the expression out as the two experts above have demonstrated.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

petewinterAuthor Commented:
Thanks to you both. That makes sense, but why does the below not work for me then?

<?php if ($row_rs_media_new_group['category'] != 'EFI VUTEk Accessories' || $row_rs_media_new_group['category'] != 'Ink Misc') { ?>
    <td bgcolor="#DDDDDD"><?php echo ucwords(strtolower($row_rs_media_new['ink_colour'])); ?></td>
    <?php } ?>

Open in new window


but this does...

 <?php if ($row_rs_media_new_group['category'] != ('EFI VUTEk Accessories' || 'Ink Misc')) { ?>
    <td bgcolor="#DDDDDD"><?php echo ucwords(strtolower($row_rs_media_new['ink_colour'])); ?></td>
    <?php } ?>

Open in new window


Does it work differently when I say not equal != ?
0
Julian HansenCommented:
This bit
('EFI VUTEk Accessories' || 'Ink Misc')

Open in new window

Will always evaluate to 1 - see my earlier post

You are then checking if $row_rs_media_new_group['category'] != 1

If this works for you it is co-incidence. Consider the following code
<?php

$x = 'John';

if ($x != ('Fred' || 'Mary')) {
echo "True<br/>";
}
else {
	echo "False<br/>";
}

$x = 'Mary';

if ($x != ('Fred' || 'Mary')) {
echo "True<br/>";
}
else {
	echo "False<br/>";
}

Open in new window


Run this code and you get
False
False

The ('Fred' || 'Mary') evaluates to 1 and in neither case is $x == 1
Sample here
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petewinterAuthor Commented:
Many thanks. Wanted to share points for the explanation.
0
Julian HansenCommented:
You are welcome.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.