"2014" "Sinsing" "PHP" "Fall" "3" first encounter variable

Source: Internet
Author: User

Variable *********************

1. Most of the programming languages we use now support the concept of variables, and we use variables to represent those variable quantities.

2. We can summarize several elements of a variable: first, there is a name for each other, and the second is to store a value, although the value is variable, but this value is still available.

Variable name *********************

1. Variable names are used to distinguish different variables, and variable names begin with the $ symbol, which is borrowed from the syntax rules of Perl.

2. After the $ character, it can be a combination of numeric letters and underscores, but don't start with numbers, I personally recommend using letters to start with.

3. For example $ A, $b, $xin _1, $xin 2 are all valid variable names.

4. When it comes to naming conventions, it seems that there is no universally accepted specification in PHP, although there are so-called PSR-0 to PSR-4 standards, but it is too much to not abide by this standard, so I think it is still a long way from the real standard.

5. For example, the English word password is "password", some people like to use variable $pwd to express, there are people like to use the $passwd to express, there are people like to use the $pass to express, there are people like to use $PD to express, of course, some people will use $password, will be someone with $p, Therefore, the choice of variable names is very free.

6. Our requirements for variable names are as follows: ① can express the meaning of this variable, ② easy to write and communicate.

7. Description: ① The variable name that starts with a number is illegal. ② do not repeat with the keyword, here we have not touched what is the keyword, but we can remember one thing: As long as a variable with a letter plus a number, it is definitely not a keyword.

Introduction of data Types *************************

1. As mentioned above, a variable must have a variable name, but only the variable name is not enough, we also need to store a value.

2. But the computer can only read bytecode, which is the combination of 0 and 1, but we need a variety of data, for which we introduced the concept of "data type".

3. The so-called "data type" is a way for us to parse the combination of 0 and 1 such bytecode, such as 01100001, we use ASCII code to parse the English letter, is a, we parse the decimal number, is 97.

4. As you can see above, for the same bunch of bytecode, how we translate into the information we need depends on the "data type".

5. Someone might ask, what are the types of data in PHP? I'll read it for you next.

There are eight types of data in 6.PHP, and we'll start with four of them: Boolean type, integer type, float type, string type.

Integer type ************************

1. Integer Type I believe that everyone is familiar with the English expression of integers is "int".

2. Since it is an integer, its value range is positive integer, 0, negative integer.

3. For example 4,5,-2 These are all valid integers, and 3.2 is not a valid integer.

4. If the reader learns C language, it will find that there are many types of symbols, unsigned, long-shaped and so on, which is to be considered from the point of view of speed and efficiency, but we PHP is a scripting language, it does not need to consider too many things, so it is all these types of unity called "integer type."

Assignment Statement ********************************

1. We've covered the integer type above, but how do we get a variable to be a whole number? This will require us to write an assignment statement to complete the work.

2. Here we introduce the concept of the assignment statement, the so-called assignment statement, is to assign a variable to the value of the variable, we use the = number, that is, the assignment number.

3. The syntax format of an assignment statement: variable name = the value taken by the variable;

4. Notice the semicolon above do not forget that you can remember this: as long as it is a statement, followed by a semicolon.

5. Let's write an example of an assignment statement: $a = 4;

6. The above statement passes the integer value of 4 to the variable of $ A, but how do we know $ A is 4? We then use Echo to display the information.

View variable ***************************

1. We will have a lot of ways to see the variable information later.

2. We used the Echo statement first, we used to learn that echo can pass some text messages to the browser, but it can also pass variables.

3. For example, echo $a, the value of the variable $ A is sent to the browser, if we visit the page at this time, we can see the variable information.

Use variable combat *******************

1. Now that we have learned the naming of variables, the assignment of variables, the display of variables, it is time to integrate these functions.

2. We create a new xin3.php in the WWW directory of Wamp and enter the following code:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><?php$num = 913;echo "My Birthday"; echo "<br/>"; Echo $num;</span>

3. Remember what the third and fourth line statements mean? The third line of echo "My Birthday" means that the four words of "My Birthday" are printed to the screen, and the echo "<br/>" in line four is the function of line wrapping.

4. Echo $num on line fifth; the value 913 stored in our $num variable is displayed.

5. We entered in the browser: localhost/xin3.php, see the results are as follows:

6. Instead of writing the content directly to the Echo statement, we use a custom variable to display the content, but there is always no "variable" effect.

Start to turn into a body ************************

1. Since the variable is variable, how can we make it change? The answer is that we use an assignment statement to make it change.

2. For example, I assign a value of $ A to 4, and then I can reassign to 5, which is a bit of a variable.

3. We create a new xin4.php and enter the following code:

<?php$age = 4;echo "I this year", Echo $age, echo "old", echo "<br/>"; $age = 5;echo "I am" after one year; Echo $age; echo "old";

4. Then we open the browser, input: localhost/xin4.php, enter after you will see the following information:

5. Carefully analyze our code, we in the second line of $age = 4, the $age variable value is 4, but in the seventh row $age = 5, the value of the $age variable is modified in order to 5, which reflects the variable variability.

Boolean type ***********************

1. The English name of the Boolean type is Boolean, and its name originates from a large French mathematician, in fact, there is a algebra called Boolean algebra, which is about the logic of some things.

2. The Boolean type has only two values, one is true, the letter is true, one is false, and the letter is false.

3. Boolean type is still more important, although it has only two values, but these two values are very important, imagine, there is more important than true?

4. If we define a variable $flag = True, and then use the Echo $flag, then we find that the resulting data is 1, as if it's not what we expected, so how do we look at the value of the variable? How does true become a 1? We'll talk about it next.

Run *************** in the browser

1. We create a new a.php file in the WWW directory, and then we enter localhost/a.php in the address bar of the browser to run the file.

2. At this time a.php replaced by other filenames I think we should also know how to do, is to change the browser in the address bar in the corresponding path.

var_dump******** Display full information *******

1. Before we talked about using echo to see the variables, we found that there was absolutely no problem with integers, but for Boolean type variables, it seems that the support is not good, it is time to consider using Var_dump to see the value of the variable.

2. We create a new xin5.php and enter the following:

<?php$flag = True;var_dump ($flag);

3. Then we look at the results of the operation in the browser:

4. Here the Var_dump, is a function, the concept of function we will talk about later.

The function of 5.var_dump is to view information about the value of a variable, which displays the data type of the value and the data content of the value.

6. In fact, for our integer variable, it is also established, for example, we create a new xin6.php file, write the following content:

<?php$m = 44;var_dump ($m);


7. We see how it works in the browser:

Floating-point type **************************

1. The so-called floating-point type, the English expression is float, can be understood as the decimal type, but in the strict sense, floating-point and decimal type is not equivalent.

2. Our decimals are usually represented by a floating-point type.

3. We create a new xin7.php file and write the following:

<?php$f = 22.3;var_dump ($f);

4. Then we run the file with the following effect:

Weakly typed ***************************

1.PHP is a weak type of language, the so-called weak type, I looked up some information, and did not find a satisfactory definition, so I made a definition.

2. The so-called weak type, refers to the variable in the declaration can not specify its type, when modifying its value can change its type.

3. For example, our assignment statement $ A = 4; it does not specify $ A must be of type int, but we use var_dump ($a) and the result is int 4, because the data type of 4 itself is int.

4. For example, $ A = 4; At this time, $ A is plastic, and when we use $ A = 2.2, we find that a has become a floating-point type.

5. We create a new xin8.php and enter the following code:

<?php$a = 4;var_dump ($a); $a = 2.2;var_dump ($a);

6. We run the code with the following effect:


Course Summary **********************

1. In this lesson we explain what variable names are, and the naming conventions for variable names.

2. Then there are three types of data, int, Boolean, float, respectively.

3. Then explain the use of the = number to assign the variable, with echo to display the variable.

4. The details of using Var_dump to view variables are then explained.

5. Finally, the concept of weak type is given.

Course Practice ******************

1. Learning without practice is dangerous, so before starting the course, I suggest you do the following questions first.

2. Title: Assign a value of 23 to the variable $m, display its information with ECHO, then assign a value of 44.3, and display its information with Var_dump.

3. The reference effect is as follows:

4. The reference code is as follows:

<?php$m = 23;echo $m; $m = 44.3;var_dump ($m);

5. Sinsing, look forward to your attention.



"2014" "Sinsing" "PHP" "Fall" "3" first encounter variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.