0

私の現在のコードは次のとおりです。

if ( ( $status == 'active' ) || 
     ( $status == 'full' ) ) {

AND ステートメントも含める必要があります。したがって、$status が full または active で、かつ $position が「need photo」または「completed」に一致する場合、それが表示されます。AND ステートメントを含めるにはどうすればよいですか?

次のことを試しましたが、うまくいかないようでした。

if ( ( $status == 'active' ) || 
     ( $status == 'full' ) && 
     ( $position == 'need photo' ) || 
     ( ( $position == 'completed' ) ) {

何か助けはありますか?ありがとうございました!:-) 私はこれらすべてにかなり慣れていません。Google を試しましたが、明確な答えが見つかりませんでした。

4

3 に答える 3

3

&&よりも優先順位が高い||ため、試したコードは次と同じです。

if ($status == 'active' || ($status == 'full' && $position == 'need photo') || $position == 'completed') {

どちらかstatusactiveであるか、両方statusfullでありpositionであるか、need photoまたはpositioncompleted

しかし、あなたが望む:

if (($status == 'active' || $status == 'full') && ($position == 'need photo' || $position == 'completed')) {

つまり、どちらかstatusactiveまたはstatusでありfull、いずれかpositionneed photoまたはpositionである場合completed

于 2013-05-24T20:50:00.947 に答える
1

operator precedence に関する PHP ドキュメントによると、ANDは よりも優先されるため、式を括弧でORグループ化する必要があります。OR

if ( ($status == 'active || $status == 'full) && ($position == 'need photo' || $position == 'completed') ) {
    ...
于 2013-05-24T20:50:31.340 に答える
0

いくつかの括弧が欠けているだけだと思います。ここif ((A) && (B))で、A と B は複雑な式 (2 つのサブ式を含む式) です。

あなたの場合:A =( $status == 'active' ) || ( $status == 'full' )およびB =( $position == 'need photo' ) || ( $position == 'completed' )

だから、これを試してください: if ( **(** ( $status == 'active' ) || ( $status == 'full' ) **)** && **(** ( $position == 'need photo' ) || ( $position == 'completed' ) **)** ) {

于 2013-05-24T20:55:14.877 に答える