and

演算子

 状態:  閲覧数:5,709  投稿日:2010-05-06  更新日:  
論理積。両オペランドが「真」なら「true」を返す。

右辺が「false」でも左辺は評価されるが、左辺が「false」なら右辺は評価されない。
※(「左辺」「右辺」)の(「false」や「true」)は、それぞれのオペランドの処理結果。
また、「左辺処理結果false」でも、左辺の呼出先実行処理結果は評価される。
「右辺処理結果false」でも、右辺の呼出先実行処理結果は評価される。※但し「左辺=true」であることが前提条件。
なお、演算子「and」「&&」は基本的に同義(※厳密には「&&」の方が「and」より優先順位が高い) 。

<pre>
<?php
print "▼論理積。両オペランドが「真」なら「true」を返す
";
print "左辺\$a =".($a=1)."
";
++$a and $x = true;
print "「true」 and 「true」 → ";
var_dump(++$a and $x = true);
print "左辺\$a =".$a."

";

$a=1;
print "▼右辺が「false」でも左辺は評価される
";
print "左辺\$a =".($a=1)."
";
++$a and $x = false;
print "「true」 and 「false」 → ";
var_dump(++$a and $x = false);
print "左辺\$a =".$a."

";

$b=1;
print "▼左辺が「false」なら右辺は評価されない
";
print "右辺\$b =".($b=1)."
";
$x = false and ++$b;
print "「false」 and 「true」 → ";
var_dump($x = false and ++$b);
print "右辺\$b =".$b."

";

$a=1;
print "▼&&演算子が、and演算子と、基本的に同義であることの確認(※厳密には「&&」の方が「and」より優先順位が高い)
";
print "左辺\$a =".($a=1)."
";
++$a && $x = true;
print "「true」 and 「true」 → ";
var_dump(++$a && $x = true);
print "左辺\$a =".$a."

";

print "▼(「左辺」「右辺」)の(「false」や「true」)は、それぞれのオペランドの処理結果であることの確認
";
function fb(){
return false;
}
function fc(){
return true;
}
fb() and fc();
print "「false」 and 「true」 → ";
var_dump(fb() and fc());
fc() and fc();
print "「true」 and 「true」 → ";
var_dump(fc() and fc());
print "
";

print "▼「左辺処理結果false」でも、左辺の呼出先実行処理結果は評価される
";
$az=1;
function fd(){
global $az;
++$az;
return false;
}
print "左辺呼出先\$az =".($az=1)."
";
print "「false」 and 「true」 → ";
var_dump(fd() and fc());
fd() and fc();
print "左辺呼出先\$az =".$az."

";

print "▼「右辺処理結果false」でも、右辺の呼出先実行処理結果は評価される。※但し「左辺=true」であることが前提条件
";
$bz=1;
function fe(){
global $bz;
++$bz;
return false;
}
print "右辺呼出先\$bz =".($bz=1)."
";
print "「true」 and 「false」 → ";
var_dump(fc() and fe());
fc() and fe();
print "右辺呼出先\$bz =".$bz."

";
print "

";
?>
</pre>


▼結果
  /demo/and.html




new clone違い

or(||)

コメント投稿(ログインが必要)



類似度ページランキング
順位 ページタイトル抜粋
1 and 100
2 include_path 40
3 include 40
4 final修飾子 36
5 var_dump() 31
6 array_diff 31
7 Generators 31
8 set_error_handler 30
9 SessionHandler::gc 29
10 syntax error 27
11 array_reduce 27
12 die() 25
13 mixed 25
14 json_encode関数 25
15 popen 25
16 break文 22
17 include require 22
18 session_start() 22
19 Xdebug 22
20 session_set_save_handler() 21
2024/4/26 15:56 更新
週間人気ページランキング / 4-19 → 4-25
順位 ページタイトル抜粋 アクセス数
1 Parse error: syntax error, unexpected 'public' (T_PUBLIC) | Parse error(エラーメッセージ) 16
2 ブラウザを閉じたらセッションデータはどうなるの? | セッション 11
3 PHPで定数を定義する方法は2種類ある / 配列定数の定義 7
4 コード例 … 「例外処理」はネストすることができる 6
4 curl で Cookie を使用する 6
5 Warning: strlen() expects parameter 1 to be string, array given in ○○.php on line △△ | Warning(エラーメッセージ) 5
6 ブラウザを閉じたらセッションデータはどうなるの? | セッション 4
6 Fatal error: Access level to ▲::$△ must be protected (as in class ●) or weaker | Fatal error(エラーメッセージ) 4
7 Fatal error: Call to undefined method MDB2_Error::execute() in ○○ on line △△ | Fatal error(エラーメッセージ) 3
7 Fatal error: Uncaught Error: Class 'Mecab_Tagger' not found in /var/www/★★.php:☆☆ | Fatal error(エラーメッセージ) 3
7 Warning: include() [function.include]: Failed opening '**.php' for inclusion (in | Warning(エラーメッセージ) 3
7 型の種類 | 型 3
7 or(||) | 演算子 3
7 セッション名 | セッション 3
7 セッション管理が必要な理由は、HTTPプロトコルには状態を保持する機能がないため | セッション 3
7 ( ! ) Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined | Fatal error(エラーメッセージ) 3
7 Composer | 依存関係マネージャ 3
8 Cookie / Cookieとは? / Cookie仕様 2
8 セッションCookie | クッキー 2
8 Fatal error: Uncaught Wrench\Exception\HandshakeException: No accept header receieved on handshake response in ★★★ headless-chromium-php/vendor/wrench/wrench/lib/Wrench/Protocol/Protocol.php | エラーメッセージ 2
2024/4/26 1:02 更新