scope

Variable scope difference between PHP and C: block scope is not exactly the same?

人走茶凉 提交于 2019-12-30 00:46:31
问题 The following PHP code will output 3 . function main() { if (1) { $i = 3; } echo $i; } main(); But the following C code will raise a compile error. void main() { if (1) { int i = 3; } printf("%d", i); } So variables in PHP are not strictly block-scoped? In PHP, variables defined in inner block can be used in outer block? 回答1: PHP only has function scope - control structures such as if don't introduce a new scope. However, it also doesn't mind if you use variables you haven't declared. $i won

accessing “module scope” vars

假装没事ソ 提交于 2019-12-29 20:32:48
问题 I'm currently learning Python, and I have to work on a Python 2.7 project. Accessing "module scope" variables in functions of the module itself is a bit confusing for me, and I didn't succeed in finding a satisfying way. My attempts so far: Way 1: my_module.py my_global_var = None def my_func(): global my_global_var my_global_var = 'something_else' Here I think that confusing local and "module scope" vars may be quite easy. Way 2: my_module.py import my_module my_global_var = None def my_func

accessing “module scope” vars

孤街醉人 提交于 2019-12-29 20:32:01
问题 I'm currently learning Python, and I have to work on a Python 2.7 project. Accessing "module scope" variables in functions of the module itself is a bit confusing for me, and I didn't succeed in finding a satisfying way. My attempts so far: Way 1: my_module.py my_global_var = None def my_func(): global my_global_var my_global_var = 'something_else' Here I think that confusing local and "module scope" vars may be quite easy. Way 2: my_module.py import my_module my_global_var = None def my_func

How to enable constants imported into component in the template without proxy via properties in Angular?

こ雲淡風輕ζ 提交于 2019-12-29 10:03:00
问题 In my component, I have imported a constant and I'm using it as is. import { NullGuid } from "src/app/models/constants"; ... @Component({ ... }) export class TheComponent implements OnInit { constructor(private service: SomeService) { this.proxy = NullGuid; } ... proxy: string = NullGuid; } In the template, I can reach the service like this. <div>{{this.service.getStuff()}}</div> However, I can't do the same with the constant unless it's declared as shown above. Can I somehow expose the

How to enable constants imported into component in the template without proxy via properties in Angular?

社会主义新天地 提交于 2019-12-29 10:02:12
问题 In my component, I have imported a constant and I'm using it as is. import { NullGuid } from "src/app/models/constants"; ... @Component({ ... }) export class TheComponent implements OnInit { constructor(private service: SomeService) { this.proxy = NullGuid; } ... proxy: string = NullGuid; } In the template, I can reach the service like this. <div>{{this.service.getStuff()}}</div> However, I can't do the same with the constant unless it's declared as shown above. Can I somehow expose the

Classes and variables scope in C++

為{幸葍}努か 提交于 2019-12-29 09:46:11
问题 class car { int speed; double position; public: car(int v,double d); int getspeed(); }; int car::getspeed() { return speed; } car::car(int s, double x){ speed=s; position=x; } Even though i specified different variables for car(int s,v), why does it work? i though it should give me a compile time error? this code:which var it uses? class car { int speed; double position; public: car(int speed,double time); int getspeed(); }; int car::getspeed() { return speed; } car::car(int speed, double

scope error in if statement in java program

孤人 提交于 2019-12-29 09:31:25
问题 I'm having an issue with scope in an if statement, at least, I'm pretty sure that is where my error is, and I'm not sure of how to fix the issue (I'm pretty new at programming). Basically, it seems that if I declare something within an if statement, the variable (in this case, an array of structs) does not exist outside of the if statement. However, I really need the declaration for the array to be inside of an if/else because the size of the array is dependent upon N, so how can I fix this

Terminology definition - Scope in C application

别等时光非礼了梦想. 提交于 2019-12-29 09:12:17
问题 Is there a specific term for the following type of C code? In my example, I want to increase the depth of scope for a small chunk of code within a function without having to use needless if/else/for statements so that I can re-declare a variable with the same name multiple times. eg: void myfunc(void) { int a = 0, b = 1; { int c; c = 3; printf("c is: %d", c); } a = 2; { int c = 5; printf("c is %d", c); } } What is the term used to describe how I just wrapped some code in curly braces and

How to use php variables from an included php file

喜欢而已 提交于 2019-12-29 08:57:11
问题 I have these variables that are set in one php file, and when i include that php file in another php file, how do i use those variables from the included php file? 回答1: They should just roll over: File1.php <?php $var1 = "TEST"; ?> File2.php <?php include("File1.php"); echo $var1; //Outputs TEST ?> 回答2: Have you actually tried it? Just use the variables. They are available within the scope of the including file. From the PHP manual: When a file is included, the code it contains inherits the

Is it possible that a variable declared after the main has file scope?

半世苍凉 提交于 2019-12-29 08:46:29
问题 After running this code: #include <stdio.h> int x; int main(void) { printf("%d\n",x); return 0; } int x=5; I expected the output should be 0 . Because of the sequence control structure of the program int x; should be executed first and then 0 is printed and finally int x=5; should be executed. But it is giving the output 5 . How is the program accesses 5 for the x in printf ? 回答1: The first acts as a forward declaration, and the later acts as the actual definition. 回答2: Variable default