-2

Is this a valid syntax in php? I want a call to be done if these two conditions are met.

if($valuear[$i] < $rake) and ($winuser != $winar[$i])

Thanks.

BigBoyB
  • 261
  • 2
  • 10
  • I think you are missing the opening and closing bracket for your if statment , else it is okay – Andrew Nov 12 '15 at 09:24
  • `and` may not work as you expect it to, it is not the same as `&&` : http://stackoverflow.com/questions/2803321/and-vs-as-operator – CD001 Nov 12 '15 at 09:31

4 Answers4

1

No, it isn't

if (($valuear[$i] < $rake) && ($winuser != $winar[$i]) ){}

You must wrap the whole condition in brackets.

This could help you:

and : &&

or : ||

Luis González
  • 3,199
  • 26
  • 43
  • 2
    You could also use the words `and` and `or`: http://php.net/manual/en/language.operators.logical.php – tino.codes Nov 12 '15 at 09:24
  • 1
    @tino.codes you could but `and` and `or` have lower operator precedence so you may not get what you're expecting : http://stackoverflow.com/questions/2803321/and-vs-as-operator – CD001 Nov 12 '15 at 09:38
  • 1
    @CD001 As a programmer you should know the difference between a _comparison operator_ (http://php.net/manual/en/language.operators.comparison.php) an a _logical operator_ (http://php.net/manual/en/language.operators.logical.php). So the operator precedence is absolutly clear (http://php.net/manual/en/language.operators.precedence.php). Simple rule: _Comparison_ before _Logic_ (execpt the `!` ;) ). – tino.codes Nov 12 '15 at 09:51
  • 1
    @tino.codes I'm not arguing the point - you're obviously aware of the difference... the same cannot be said of everyone on SO - so I'd tend to err on the side of caution. Yes you can use `and` and `or` **but** you need to know when not to - I've never run into a use case with PHP where `&&` and `||` don't get the desired result. – CD001 Nov 12 '15 at 09:54
  • 2
    @CD001 You only get into trouble when you mix it (use `and` and `&&` or `or` and `||`). Than it's hard to understand (for beginners) wich operator has the higher precedence. – tino.codes Nov 12 '15 at 09:59
0

This is the valid way :

if($valuear[$i] < $rake && $winuser != $winar[$i])
ThinkTank
  • 1,187
  • 9
  • 15
  • Could you rephrase your answer to make yourself clear ? Written this way, one can think that the code you provide is not valid. – xlecoustillier Nov 12 '15 at 09:22
0

Set the brackets in this way:

if($valuear[$i] < $rake and $winuser != $winar[$i])

http://php.net/manual/en/control-structures.if.php

The basic syntax is:

if (expr)
  statement
tino.codes
  • 1,512
  • 1
  • 12
  • 23
0

you will get syntax error for what you have done

if($valuear[$i] < $rake) and ($winuser != $winar[$i])

It should be

if(($valuear[$i] < $rake) and ($winuser != $winar[$i]))
  ^                                                   ^
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41