質問編集履歴

6

修正

2015/06/29 05:57

投稿

flat
flat

スコア617

test CHANGED
File without changes
test CHANGED
@@ -2,160 +2,38 @@
2
2
 
3
3
 
4
4
 
5
- また、[PHPマニュアルにある変数のスコープの例](http://php.net/manual/ja/language.variables.scope.php#example-113)を試してみたのですが、出力されたのは3はなく2でした。
5
+ [PHPマニュアルにある変数のスコープの例](http://php.net/manual/ja/language.variables.scope.php#example-113)を試してみたのですが、関数内で外部の変数($a,$b)を参照来ませんでした。
6
6
 
7
7
  関数の外部でもグローバル宣言をすると参照出来るようになりましたが、これは関数の内部だけの宣言では参照出来ないという事なのでしょうか?
8
8
 
9
9
 
10
10
 
11
- 追記3
11
+ **PHPマニュアルにある変数のスコープの例**(一部変更)
12
12
 
13
- 上記リンク先の例にある変数$aと$bは明記されていないだけでグローバル宣言がされているものと解釈する事にしました。(ですよね…?)
13
+ ```lang-PHP
14
+
15
+ $a = 1;
16
+
17
+ $b = 2;
14
18
 
15
19
 
16
20
 
17
- **1. グローバル宣言した変数を参照**
21
+ function Sum() {
18
22
 
19
- ```lang-PHP
23
+ global $a, $b;
20
24
 
21
- global $success, $failure; // この変数をグローバル変数にするという宣言
25
+ var_dump( $a, $b ); // $a $b どちらも NULL (参照できてない)
22
-
23
- $success = 'Yes!';
24
-
25
- $failure = 'Sorry...';
26
26
 
27
27
 
28
28
 
29
- function please_marry_me( $result=true ) {
29
+ $b = $a + $b;
30
30
 
31
- global $success, $failure; // ローカル変数ではなくグローバル変数を使うという指示
32
-
33
- $reaction;
34
-
35
- if ( $result === true ) {
36
-
37
- $reaction = '<p>' . $success . '</p>';
38
-
39
- } elseif ( $result === false ) {
40
-
41
- $reaction = '<p>' . $failure . '</p>';
42
-
43
- } else {
44
-
45
- $reaction = '<p>Please enter a true or false.</p>';
46
-
47
- }
31
+ }
48
-
49
- return $reaction;
50
-
51
- }
52
32
 
53
33
 
54
34
 
55
- echo please_marry_me( true );
35
+ var_dump( Sum() ); // NULL
56
36
 
57
- // 安易にグローバル宣言はしない方が良い…?
37
+ echo $b; // 2
58
38
 
59
39
  ```
60
-
61
-
62
-
63
- 追記1
64
-
65
- **2. クロージャによる変数の引き継ぎ**
66
-
67
- ```lang-PHP
68
-
69
- $success = 'Yes!';
70
-
71
- $failure = 'Sorry...';
72
-
73
-
74
-
75
- $please_marry_me = function( $result=true ) use ( $success, $failure ) {
76
-
77
- $reaction;
78
-
79
- if ( $result === true ) {
80
-
81
- $reaction = '<p>' . $success . '</p>';
82
-
83
- } elseif ( $result === false ) {
84
-
85
- $reaction = '<p>' . $failure . '</p>';
86
-
87
- } else {
88
-
89
- $reaction = '<p>Please enter a true or false.</p>';
90
-
91
- }
92
-
93
- return $reaction;
94
-
95
- }; // 変数なのでセミコロンの入れ忘れに注意(気付くのに半日かかってしまいました…)
96
-
97
-
98
-
99
- $success = 'Yes, of course!';
100
-
101
- echo $success; // Yes of course!
102
-
103
- echo $please_marry_me( true ); // Yes!
104
-
105
- /*
106
-
107
- * 引き継がれた変数の値は関数を定義した時点のもの
108
-
109
- * 引き継がれた変数の値は関数の中で(当然ながら)変更可能
110
-
111
- * 参照渡しで引き継ぐと関数を定義した後でも値を変更出来る(呼び出した関数にも反映される)
112
-
113
- * クロージャを値に代入しているので値が上書きされるとクロージャを呼び出せなくなる
114
-
115
- */
116
-
117
- ```
118
-
119
-
120
-
121
- 追記2
122
-
123
- **3. $GLOBALS(スーパーグローバル)による変数の参照**
124
-
125
- ```lang-PHP
126
-
127
- global $success, $failure; // グローバル宣言は必要
128
-
129
- $success = 'Yes!';
130
-
131
- $failure = 'Sorry...';
132
-
133
-
134
-
135
- function please_marry_me( $result=true ) {
136
-
137
- $reaction;
138
-
139
- if ( $result === true ) {
140
-
141
- $reaction = '<p>' . $GLOBALS['success'] . '</p>';
142
-
143
- } elseif ( $result === false ) {
144
-
145
- $reaction = '<p>' . $GLOBALS['failure'] . '</p>';
146
-
147
- } else {
148
-
149
- $reaction = '<p>Please enter a true or false.</p>';
150
-
151
- }
152
-
153
- return $reaction;
154
-
155
- }
156
-
157
-
158
-
159
- echo please_marry_me( true );
160
-
161
- ```

5

理解した箇所を追記

2015/06/29 05:57

投稿

flat
flat

スコア617

test CHANGED
File without changes
test CHANGED
@@ -8,11 +8,17 @@
8
8
 
9
9
 
10
10
 
11
+ 追記3
12
+
13
+ 上記リンク先の例にある変数$aと$bは明記されていないだけでグローバル宣言がされているものと解釈する事にしました。(ですよね…?)
14
+
15
+
16
+
11
17
  **1. グローバル宣言した変数を参照**
12
18
 
13
19
  ```lang-PHP
14
20
 
15
- global $success, $failure; // 内部の宣言だけでは参照出来ない…?
21
+ global $success, $failure; // この変をグローバル変数にするという宣言
16
22
 
17
23
  $success = 'Yes!';
18
24
 
@@ -22,7 +28,7 @@
22
28
 
23
29
  function please_marry_me( $result=true ) {
24
30
 
25
- global $success, $failure;
31
+ global $success, $failure; // ローカル変数ではなくグローバル変数を使うという指示
26
32
 
27
33
  $reaction;
28
34
 

4

追記箇所の明示

2015/06/12 15:26

投稿

flat
flat

スコア617

test CHANGED
File without changes
test CHANGED
@@ -54,7 +54,7 @@
54
54
 
55
55
 
56
56
 
57
- 追記
57
+ 追記1
58
58
 
59
59
  **2. クロージャによる変数の引き継ぎ**
60
60
 
@@ -112,6 +112,8 @@
112
112
 
113
113
 
114
114
 
115
+ 追記2
116
+
115
117
  **3. $GLOBALS(スーパーグローバル)による変数の参照**
116
118
 
117
119
  ```lang-PHP

3

追記

2015/06/12 15:12

投稿

flat
flat

スコア617

test CHANGED
File without changes
test CHANGED
@@ -14,13 +14,13 @@
14
14
 
15
15
  global $success, $failure; // 関数内部の宣言だけでは参照出来ない…?
16
16
 
17
- $success = 'Yes!';
17
+ $success = 'Yes!';
18
18
 
19
19
  $failure = 'Sorry...';
20
20
 
21
21
 
22
22
 
23
- function please_marry_me( $result = true ) {
23
+ function please_marry_me( $result=true ) {
24
24
 
25
25
  global $success, $failure;
26
26
 
@@ -28,15 +28,15 @@
28
28
 
29
29
  if ( $result === true ) {
30
30
 
31
- $reaction = $success;
31
+ $reaction = '<p>' . $success . '</p>';
32
32
 
33
33
  } elseif ( $result === false ) {
34
34
 
35
- $reaction = $failure;
35
+ $reaction = '<p>' . $failure . '</p>';
36
36
 
37
37
  } else {
38
38
 
39
- $reaction = 'Please enter a true or false.';
39
+ $reaction = '<p>Please enter a true or false.</p>';
40
40
 
41
41
  }
42
42
 
@@ -109,3 +109,45 @@
109
109
  */
110
110
 
111
111
  ```
112
+
113
+
114
+
115
+ **3. $GLOBALS(スーパーグローバル)による変数の参照**
116
+
117
+ ```lang-PHP
118
+
119
+ global $success, $failure; // グローバル宣言は必要
120
+
121
+ $success = 'Yes!';
122
+
123
+ $failure = 'Sorry...';
124
+
125
+
126
+
127
+ function please_marry_me( $result=true ) {
128
+
129
+ $reaction;
130
+
131
+ if ( $result === true ) {
132
+
133
+ $reaction = '<p>' . $GLOBALS['success'] . '</p>';
134
+
135
+ } elseif ( $result === false ) {
136
+
137
+ $reaction = '<p>' . $GLOBALS['failure'] . '</p>';
138
+
139
+ } else {
140
+
141
+ $reaction = '<p>Please enter a true or false.</p>';
142
+
143
+ }
144
+
145
+ return $reaction;
146
+
147
+ }
148
+
149
+
150
+
151
+ echo please_marry_me( true );
152
+
153
+ ```

2

誤りを修正

2015/06/12 15:11

投稿

flat
flat

スコア617

test CHANGED
File without changes
test CHANGED
@@ -70,8 +70,6 @@
70
70
 
71
71
  $reaction;
72
72
 
73
- $success = 'Yes, of course!';
74
-
75
73
  if ( $result === true ) {
76
74
 
77
75
  $reaction = '<p>' . $success . '</p>';

1

追記と修正

2015/06/12 11:53

投稿

flat
flat

スコア617

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- 関数の外にある変数を参照する方法はどの様なものがありますか?
1
+ 関数の外にある変数を利用する方法はどの様なものがありますか?
2
2
 
3
3
 
4
4
 
@@ -6,9 +6,13 @@
6
6
 
7
7
  関数の外部でもグローバル宣言をすると参照出来るようになりましたが、これは関数の内部だけの宣言では参照出来ないという事なのでしょうか?
8
8
 
9
+
10
+
11
+ **1. グローバル宣言した変数を参照**
12
+
9
13
  ```lang-PHP
10
14
 
11
- global $success, $failure;
15
+ global $success, $failure; // 関数内部の宣言だけでは参照出来ない…?
12
16
 
13
17
  $success = 'Yes!';
14
18
 
@@ -20,15 +24,19 @@
20
24
 
21
25
  global $success, $failure;
22
26
 
23
- $reaction = '<p>Please enter a true or false.</p>';
27
+ $reaction;
24
28
 
25
- if ( $result == true ) {
29
+ if ( $result === true ) {
26
30
 
27
- $reaction = '<p>' . $success . '</p>';
31
+ $reaction = $success;
32
+
33
+ } elseif ( $result === false ) {
34
+
35
+ $reaction = $failure;
28
36
 
29
37
  } else {
30
38
 
31
- $reaction = '<p>' . $failure . '</p>';
39
+ $reaction = 'Please enter a true or false.';
32
40
 
33
41
  }
34
42
 
@@ -40,4 +48,66 @@
40
48
 
41
49
  echo please_marry_me( true );
42
50
 
51
+ // 安易にグローバル宣言はしない方が良い…?
52
+
43
53
  ```
54
+
55
+
56
+
57
+ 追記
58
+
59
+ **2. クロージャによる変数の引き継ぎ**
60
+
61
+ ```lang-PHP
62
+
63
+ $success = 'Yes!';
64
+
65
+ $failure = 'Sorry...';
66
+
67
+
68
+
69
+ $please_marry_me = function( $result=true ) use ( $success, $failure ) {
70
+
71
+ $reaction;
72
+
73
+ $success = 'Yes, of course!';
74
+
75
+ if ( $result === true ) {
76
+
77
+ $reaction = '<p>' . $success . '</p>';
78
+
79
+ } elseif ( $result === false ) {
80
+
81
+ $reaction = '<p>' . $failure . '</p>';
82
+
83
+ } else {
84
+
85
+ $reaction = '<p>Please enter a true or false.</p>';
86
+
87
+ }
88
+
89
+ return $reaction;
90
+
91
+ }; // 変数なのでセミコロンの入れ忘れに注意(気付くのに半日かかってしまいました…)
92
+
93
+
94
+
95
+ $success = 'Yes, of course!';
96
+
97
+ echo $success; // Yes of course!
98
+
99
+ echo $please_marry_me( true ); // Yes!
100
+
101
+ /*
102
+
103
+ * 引き継がれた変数の値は関数を定義した時点のもの
104
+
105
+ * 引き継がれた変数の値は関数の中で(当然ながら)変更可能
106
+
107
+ * 参照渡しで引き継ぐと関数を定義した後でも値を変更出来る(呼び出した関数にも反映される)
108
+
109
+ * クロージャを値に代入しているので値が上書きされるとクロージャを呼び出せなくなる
110
+
111
+ */
112
+
113
+ ```