W
W
WebDev2016-07-06 16:02:05
PHP
WebDev, 2016-07-06 16:02:05

How do programming languages ​​work?

Hello, please answer a stupid question. Worked only with php and node.js.
Here is the code:

var userCounter = 0;

app.get('/', function (req, res) {
  userCounter ++;
  res.send('user counter = ' + userCounter );
});

In PHP, it will return 0 each time. In the node, it will add one each time. That is, in the case of a node, this is such a once-launched process that will work until it is forcibly stopped, or an error occurs.
How do other programming languages ​​work? Java, C#, Ruby, Python, Perl? I guess most of them work like node. Are there any that work like php?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
X
xfg, 2016-07-07
@kirill-93

All work the same. A PHP script can be run as a daemon and run until it is stopped. As well as in Java, C#, Ruby, Python, Perl, you can make the script die every time.
Strictly speaking, node.js is not a programming language, but an environment for executing javascript code. Many languages ​​have similar non-blocking i/o platforms, in php it's reactphp , in python it's twisted, which appeared long before node.js.
You decide how your program will work, either as a daemon, or run anew each time. This is independent of the programming language. It's just that one approach may be more common in one language, and another in another. But this does not mean that a different approach cannot be implemented in this particular programming language.

V
void_phoenix, 2016-07-06
@void_phoenix

Java, C# also start a process that will run until you stop, which allows you to save the values ​​​​of variables between requests. Or not save, if you place the variable declaration in the handler function, then it will be like in PHP.

V
Vitaly, 2016-07-06
@vshvydky

firstly, this code will not work in php, and secondly, the variables in js and in php are initialized at the start of the "process" and live in their visibility zone until the end of its work (with minor reservations, such as clearing variables, the work of the garbage collector ). The difference in approach is that the node initiates process 1 for many connections, PHP works as an interpreter, and is called by the web server for each request as a separate process, which means that the variables are initialized every time. Separately, about launching a function with a callback in PHP, do you seriously think that it will work in PHP at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question