[2014] [Xinxing] [php] [autumn] [4] string and comment and simple variable calculation ************* string introduction * *******************
1. the four basic data types we introduced in the previous section are not described as strings, so let's take a look at what strings are.
2. what is a character string?
3. the so-called characters refer to the letters, numbers, words, and symbols we use, such as 1, 2, A, B ,~ ,! And # are all one character.
4. in many other programming languages, there are two types of data: Character (char) and string (string). However, PHP is simple, and its philosophy is: A character is a string with a length of 1, so it does not distinguish between a character and a string.
**********************
1. the string is represented by the English word string.
2. we have already used the value of a string, that is, the part enclosed in single or double quotation marks, such as "xin xing", such as "Xiao Qian '.
3. for string variables, you can still use echo to output them.
4. create an xin9.php file and enter the following content:
"; $ Msg =" Xiaoqian "; echo $ msg;
5. open the browser and check the results. The results are as follows:
6. Note that we use single quotation marks for the first time and double quotation marks for the second time. the information they represent is the same in many cases.
* *********** Differences between single quotes and double quotes *****************
1. May someone ask: Are there any differences between strings expressed by single quotes and double quotes? The answer is yes.
2. create an xin10.php file and enter the following code:
"; Echo '$ sin'; echo"
"; Echo" then use double quotation marks "; echo"
"; Echo" $ xin ";
3. run it in the browser to see the effect:
4. here we will find that when we use single quotes, what is in single quotes is output, because it is more direct, but double quotes need to be parsed, it parses the variables into corresponding values. for example, we use $ xin to parse the variable into the string "xin xing.
5. in fact, when we use a text editor with syntax highlighting to write code, we will find some differences. for example, I am using sublime text2, the following:
6. We also found that they have different colors. based on different colors, we can determine what they mean when writing code.
************************* ********
1. sometimes, we will encounter this situation, which is generally called a "garbled problem". let's look at me:
2. at this time, we need to set its encoding. we can use the header function in the PHP file, but we haven't learned the function yet. we can directly set the browser encoding, different browsers may be set in different ways. take my browser as an example. see my operations:
3. after the character set is set, all information is displayed normally:
********************* *
1. commenting on this concept has been around for a long time, at least before I was born.
2. its function is to show us that the PHP parser will not parse the content in the comment.
3. annotations can be understood as an umbrella. no matter how heavy the rain is outside, we will not be disturbed.
4. you can also write code in the annotation, but the code is ignored by the PHP parser and cannot be parsed.
5. how do we write comments?
********************
1. comments can be divided into single-line comments and multi-line comments.
2. the single line comment is used as the comment.
3. # The subsequent content will be used as a comment. this is a shell-style comment. The so-called shell style is a script in Linux. This is similar to Python, that is, in a line, as long as you encounter #, it will be treated as a comment until the end of this line, and PHP cannot parse it.
4. // the subsequent content will also be treated as a comment, which is a C ++ style comment. because C ++ uses this style first, as long as two diagonal lines are encountered in a line, it will not be parsed until the end of this line.
5. both of the above are single-line annotations, which only work for one row. if we want to comment multiple lines, we must use a C-language annotation, that is, use/* as the beginning of the annotation, use */as the end of the comment. The content in the middle is used as the comment.
6. create a new xin11.php with the following code:
"; Echo" yongaixiaoqian "; // The double slash is a typical c ++ style. // echo" I am Xiaoqian "; /* This is a typical C-language style comment */echo"
"; Echo" Xin Xing PHP, looking forward to your attention ";
7. first, let's analyze the above code. The second line of our code uses annotations from the beginning, so the second line will not be parsed. The fourth line is an echo statement first, then there is a #, so the content after it becomes a comment and won't be parsed.
8. then, the fifth line outputs a line feed. the sixth line first outputs a string, and the double slash is used, so it becomes a comment and cannot be parsed.
9. Then, in row 8, an echo statement is written to the comment, which cannot be executed. Lines 10 to 12th are C-language comments starting with/* and ending.
10. 14th rows output a line break and 15th rows output a text segment.
11. let's take a look at the final effect:
****************
1. we learned about variables but did not reflect the value of the variables. we simply assigned values to a variable and checked its values. This function is still too weak.
2. after the operator is introduced, the function becomes stronger.
3. there are still a lot of operators in PHP. The PHP Manual is divided into 11 categories. here we will first learn a few commonly used operators.
* *************** Arithmetic operators ************
1. arithmetic operators include +,-, *,/, and %, which are Addition, subtraction, multiplication, division, and modulo.
2. our operation will produce a result, which can be assigned to a variable. for example, we use $ a = 4 + 5. then the value of $ a is 9.
3. of course, we use two specific values for calculation. In fact, we can also use two variables or one is a variable and the other is a specific number.
4. for example, $ a = $ B + 2; and $ a = $ c + $ d; are all correct arithmetic operations.
5. write an xin12.php file as follows:
"; // Output a line feed $ d = $ c + 2; echo" result obtained after adding 2: "; echo $ d;
6. the running result is as follows:
********************
1. division is complicated because different programming languages also support different types of division. some programming languages divide two integers and get integers, such as Java and some programming languages, which are decimal places, for example, PHP.
2. for example, create a new xin13.php file and write the following code:
3. the function of the above code is quite simple. it is to divide $ m and $ n, and then output the result. let's take a look at the output of the code:
******************
1. the first time I came into contact with the modulo was in my high school mathematics competition, the modulo was indeed a deep learning and had many formulas and theorems.
2. in simple terms, the modulo is to take the remainder. its operator is the percent sign (%.
3. for example, how much is the 22 modulo 3? because 22/3 = 7... 1, the final result is 1.
4. let's take a look at the following code:
5. then let's take a look at its running effect:
6. it seems that there is no problem, but I will ask you, what is-5.1 modulo 2.1? Some may be lost. how can this model be used?
7. actually, it can be obtained. you only need to grasp two points. The first point is to convert the decimal point into an integer before the modulo operation. this method is to remove the decimal part directly, retained the integer part. The second point is that the result obtained by the operation is always the same as the positive and negative signs of the divisor. See the following example:
8. the output result is as follows:
9. the calculation process is like this: first, process the data, change-5.1 to-5, and change 2.1 to 2. then, the computation result is-2, and the remainder is-1, therefore, the result is-1.
********************* ******
1. you may also see that PHP does not support computing well. The first is its weak data type. although it is convenient, it is not suitable for algorithms.
2. the number of data types is less and cannot be controlled flexibly.
3. its computing speed, as a scripting language, is still too slow.
4. Therefore, PHP is not suitable for algorithms. In fact, I have never seen anyone who will give up c, cpp, Java, and Pascal, and then use PHP to develop algorithms.
5. Therefore, we do not need to pay too much attention to its data computing, which is not its strength. We do not need to be too arrogant in data computing, because it is not PHP's strength.
************************ *
1. in this section, we learned about the string and its single quotation marks and double quotation marks.
2. then I learned how to use annotations.
3. at last, we introduce four operators: addition, subtraction, multiplication, division, and so on.
************************ *
1. suppose we have three regions: East China, North China, and South China. Each region has two hundred and twenty employees. we also have three residential areas, northwest China, Northeast China, and Southwest China. Each district has forty-five employees, calculate the number of employees in the company by programming.
2. answer:
3. reference code:
4. Xin Xing PHP, looking forward to your attention.