PHP kernel exploration: An overview of variables

  • 2020-12-20 03:31:02
  • OfStack

The basic elements of modern programming languages are: variables, process control interfaces, functions, and so on. Can I write a program without using variables? This is obviously possible, for example:


<?php
    echo "Hello AndHM";
?>

This program is very simple, output 1 string content.

Just as we can program in base 2 alone, we can do most of the work without using variables, our program will lose a great deal of flexibility by not using variables, which allow us to store values for use elsewhere in the program, or to save new values by calculation. Variables have three basic characteristics:

The name. An identifier for a variable. Just like puppies, they may be given a pet name. In terms of variable naming, PHP inherits the syntax style of Perl, starting with the dollar sign followed by the variable name. 1 valid variable name begins with a letter or underscore followed by any number of letters, numbers, or underscores. PHP also supports compound variables, which are similar to $$a variables, which are interpreted twice. This gives PHP very flexible dynamic characteristics.

Type. The type of variable, like the breed of a puppy, may vary depending on the pedigree of the dog, some are smart, some can shop, etc. In many static languages, variables are specified as they are defined and are not allowed to change as the program runs, so wouldn't it be cool if you had a dog that could only be named at random? PHP, a weakly typed language, can be assigned any value of any type.
The value of content. This is what the logo represents. It's like this real puppy thing. You can name any puppy as little 7, and in programming languages as well, you can assign a variable to a range of values it can represent. But at the same time, the variable can only have one value.

The letters that make up the variable name in PHP can be English a-ES23en, ES24en-ES25en, or ASCII characters from 127 to 255 (0x7ES28en-0ES29en). Variable names are case sensitive.

In addition to variables themselves, in PHP we are often exposed to the concepts related to variables, such as constants, global variables, static variables, and type conversions. In this chapter we introduce the implementation of these variables. These include the variable low-level storage structure of PHP itself, the implementation of the weak-type system, and the conversion of these types to each other.
Take a look at the PHP code:


<?php
    $foo = 10;
    $bar = 20;
    function change() {
        global $foo;
        $bar = 0;
        $foo++;
    }
    change();
    echo $foo, ' ', $bar;
?>

Running the code will output 11, 20.

But why the output? How are variables implemented inside of PHP? How is the scope of a variable implemented? This is a discussion of the topic around variables in this chapter, so let's start with the most basic implementation of variables.

Not all variables in a programming language can be changed. Think about the variables that we've seen in math. Their values are also immutable. For example: x + y = 10; The values of the variables x and y cannot be changed. The advantage of not changing the value of a variable in a specific scenario, which is an equation that only represents a specific value, is that it causes as few side effects as possible, as in Erlang, a functional programming language that is well worth learning.


Related articles: