IF && statement problem


 
Thread Tools Search this Thread
Top Forums Programming IF && statement problem
# 1  
Old 02-15-2011
Power IF && statement problem

Hello there,

My first time on the forums, glad to be here Smilie

I'm completely new to programming in PHP and I have a question which I hope someone could help me with.

I am currently using this statement:

Code:
if(($session['cid'] == 2) && ($item['type'] == Dagger) && ($item2['type'] == Dagger)){
              $DualWield=", equip='10'";

I want it to also include this statement below to the above code:

Code:
($item['type'] == Fist Weapon) && ($item2['type'] == Fist Weapon)

I tried:

Code:
if(($session['cid'] == 2) && ($item['type'] == Dagger) && ($item2['type'] == Dagger)) || (($session['cid'] == 2) && ($item['type'] == Fist Weapon) && ($item2['type'] == Fist Weapon)) {

But I got a
Code:
Parse error:  syntax error, unexpected T_BOOLEAN_OR


Can anyone here help me and tell me how the above statement should be written? Smilie

Last edited by pludi; 02-15-2011 at 08:25 AM.. Reason: code tags, please...
# 2  
Old 02-15-2011
I suspect you can't do
Code:
if (expr) bool (expr)

in php. Try the following instead:
Code:
if (($session['cid'] == 2) && (($item['type'] == DAGGER) && ($item2['type'] == DAGGER)) || 
    (($item['type'] == FIST WEAPON) && ($item2['type']== FIST WEAPON)))

Andrew
# 3  
Old 02-15-2011
Hi Andrew,

Thanks for offering to help, I tried using the statement rewrite you listed:

if(($session['cid'] == 2) && (($item['type'] == DAGGER) && ($item2['type'] == DAGGER)) || (($item['type'] == FIST WEAPON) && ($item2['type']== FIST WEAPON))){

but still no go, it gave me this error:

Parse error: syntax error, unexpected T_STRING on that line

Any suggestions?
# 4  
Old 02-16-2011
What are 'Dagger' and 'Fist Weapon' anyway - variables, constants, strings? I noticed, but ignored, the fact that 'Fist weapon' had a space in it. If it is a variable then how did you get this far? If it is a string literal, ie you are looking for the word "dagger" in $item['type'] then you need to quote these strings. Knowing what you are doing here will help.

Andrew
# 5  
Old 02-16-2011
Thanks for replying to my thread Andrew.

To answer your question, you are correct, I am looking for a word Dagger in $item['type']. The code was working fine when I use this statement below without putting Dagger in quotes:

if(($session['cid'] == 2) && ($item['type'] == Dagger) && ($item2['type'] == Dagger)){
$DualWield=", equip='10'";
However, when I tried to add the new statement to include Fist Weapons, that's when I ran into the problems with the errors.

Do you think I need to put quotes around Fist Weapons and leave the quotes off Dagger? Is that even possible without triggering errors.

To give you a little more insight on what the statement is suppose to do:

It is checking the database to see if cid (hero id) is 2 and the hero is holding a Dagger in hand 1 and Dagger in hand 2, then equip it for double wielding.

The new statement is so it will check to see if the hero (hero id) is 2, and if the hero is holding Fist Weapon in 1 hand, and another Fist Weapon in hand 2, then again, it will equip it for double wielding.

Any suggestions? It's just strange that the first line of code works perfectly for Daggers but when I try to add Fist Weapons in the statement, I get all the errors.
# 6  
Old 02-16-2011
I don't know enough about php to know why it worked with unquoted "dagger" but I think it's good policy to quote all strings in any programming language. It might be worth investigating the string matching functions, too. Not only will it emphasize the fact that you are using strings but it gives you the option to ignore case if you like.

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

2. Shell Programming and Scripting

Using && in if statement with 3 expressions

how do you write an if statement for something like if ((expr 1 >= expr 2 && expr 3 >= expr 4) && expr 5 <= expr 6) if ((TRUE && TRUE) && TRUE) then condition... i've done it this way but it doesn't seem to work. if (] && "$ex_day" -le "$curr_day" ); then condition... (3 Replies)
Discussion started by: angilulu
3 Replies

3. Shell Programming and Scripting

if statement with '&&' gives error

Hi, I'm using the && operator in if statement: if ; then exp $UID/$PWD@$ORACLE_SID FILE=./DUMP/$TODAY$CONCAT_STR$USERNAME.dmp STATISTICS=NONE LOG=./LOG/$TODAY$CONCAT_STR$USERNAME.log elif ; then expdp $UID/$PWD@$ORACLE_SID DIRECTORY=./DUMP/ DUMPFILE=$TODAY$CONCAT_STR$USERNAME.dmp... (8 Replies)
Discussion started by: priya001
8 Replies

4. Shell Programming and Scripting

How to write If statement using && and operator in Unix

Hi What is the syntax for if statement using && and || operator? if && ] || here its giving me an error to this if statement any suggestion?? (2 Replies)
Discussion started by: Avi
2 Replies

5. Shell Programming and Scripting

Problem with call of Java Programm & return code handling & output to several streams.

Hello Everybody, thanks in advance for spending some time in my problem. My problem is this: I want to call a java-Programm out of my shell skript, check if die return code is right, and split the output to the normal output and into a file. The following code doesn't work right, because in... (2 Replies)
Discussion started by: danifunny
2 Replies

6. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

7. UNIX for Dummies Questions & Answers

Problem with xterm & tcsh & sourcing a script in a single command

Hi friends, I have a script that sets the env variable path based on different conditions. Now the new path variable setting should not done in the same terminal or same shell. Only a new terminal or new shell should have the new path env variable set. I am able to do this only as follows: >cd... (1 Reply)
Discussion started by: sowmya005
1 Replies

8. Shell Programming and Scripting

using && in if statement ..

Hi All, Can some one tell me how to get run the following: data1="hello" data2="world" if then { echo "good afternnon" } else { echo " good morning" } fi The above code gives me an error ad below : ./if.h: line 3: ' (7 Replies)
Discussion started by: jisha
7 Replies

9. Shell Programming and Scripting

if statement with two conditions -e, &&

Wow I'm so zoned out I don't even know if I posted this question up already (I couldn't find it in my book marks or in "yesterday's" post). My question is, I'm writing a korn script that does something like the following, but I don't yet completely understand the syntax. I need to check that... (16 Replies)
Discussion started by: yongho
16 Replies

10. Shell Programming and Scripting

problem w/ -z && -n switches in an if statment

When ever i run this if statement with the -n OR -z switches it always returns says positive and does "HAVE STUFF". BUT the file does not contain the word optimus in any form. $echo this is mojo > test.file $if ; then >echo HAVE STUFF >else^Jecho NO STUFF >fi HAVE STUFF $if ; then... (1 Reply)
Discussion started by: Optimus_P
1 Replies
Login or Register to Ask a Question