"2014" "Sinsing" "PHP" "Fall" "4" strings and annotations and simple variable operations

Source: Internet
Author: User
Tags integer division php language

Introduction of Strings ********************

1. The four basic data types that we described in the previous section have a string that is not covered, so let's take a look at what a string is.

2. So-called strings, is a string of characters, then what is a character?

3. The so-called character refers to the letters, numbers, words and symbols we use, such as 1, 2, A, B, ~,! , #等等都是一个字符.

4. In many other programming languages, there are two data types, character (char) and string, but we are relatively simple in PHP, and its philosophy is that the character is a string of length 1, so it does not distinguish between characters and strings.

The representation of a string **********************

1. The string is expressed in English word string.

2. The value of a string we have actually used, that is, in single quotation marks or double quotation marks in the part, such as "Sinsing", such as ' Xiao Qian '.

3. For string variables, you can still use echo to output them.

4. Let's create a new xin9.php and enter the following:

<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" ><?php$msg = ' Sinsing '; echo $msg; echo "<br/>"; $msg = "Xiao Qian"; Echo $msg;</span>

5. Then we open the browser to see the results, and found the effect as follows:

6. Note that we are using single quotes for the first time here, and the second time using double quotes, and they often represent the same information.

The difference between single and double quotation marks *****************

1. One might ask: Is there a difference between a string in single and double quotation marks? The answer is yes.

2. We create a new xin10.php file and enter the following code:

<?php$xin = "Sinsing"; echo "first Use single quotes"; echo "<br/>"; Echo ' $xin '; echo "<br/>"; echo "then use double quotes"; echo "<br/>" ; echo "$xin";

3. Then we go to the browser to run a bit and see the effect:

4. Here we will find that when we use single quotation marks, the single quotation mark inside what will output what, because it is more direct, but the double quotation marks need to parse, it will be inside the variable to us to parse the corresponding value, such as our $xin here to parse into "Sinsing" this string.

5. In fact, we use a text editor with a syntax highlighting to write the code when you will find some differences, such as I use Sublime text2, the following:

6. We also found that they have different colors, depending on the color, we can write code to determine what they mean.

Character Set *********************************

1. Sometimes, we will encounter this situation, generally we call it "garbled problem", see me:

2. At this time, we need to set its code, we can use the header function in the PHP file set, but we have not learned the function, we directly set the browser encoding, different browsers may be set differently, in my browser, for example, see I operate:

3. After we have set the character sets, all information is displayed as normal:

Introduction of Annotations **********************

1. Note The concept was early, at least before I was born.

2. Its role is to show us that the PHP parser does not parse the contents of the comment.

3. Comments can be understood as an umbrella, no matter how heavy rain outside, we will not be under the umbrella of any interference.

4. Comments can also be written in the code, but the code will be ignored by the PHP parser, can not be resolved.

5. So, how do we write the notes?

Use of annotations ********************

1. Comments can be divided into single-line comments and multiline comments.

2. The so-called single-line comment is that this line is treated as a comment.

3. #之后的内容会被当做注释, this is the shell-style annotation, the so-called Shell style, is the script under Linux, which is similar to Python, that is, in a row, as long as the #, it until the end of this line, will be treated as comments, PHP can not parse.

The content after 4.//will also be commented, this is the C + + style of comments, because C + + is the first to use this style, as long as in a row encountered two slash, it until the end of the line, the middle of the content will not be resolved.

5. Both of these are single-line comments that work only on one line, and if we want to annotate multiple lines, we must use the C-style comment, which is to use/* as the beginning of the comment, and/* as the end of the comment, and the middle content will be commented.

6. We create a new xin11.php with the following code:

<?php# said that the content behind the well can be casually written echo "I am Sinsing", #这里的也是注释echo "<br/>"; echo "Yong Love Xiao Qian";//double Slash is a typical C + + style//echo  "I am Xiao Qian"; */ This is the typical C language style annotation */echo "<br/>"; echo "Sinsing PHP, looking forward to your attention";

7. Let us first analyze the above code, our code of the second line from the beginning of the use of comments, so the second line will not be resolved, the fourth line is an echo statement, and then a #, then the contents of it is a comment, will not be parsed.

8. Then line fifth outputs a newline, line sixth first outputs a string, and then, because of the use of a double slash, it becomes a comment and cannot be parsed.

9. Then in line eighth, an Echo statement is written in the note, which cannot be executed because it is written in a comment. Lines tenth to 12th these three lines are a C-style comment that starts with/* and ends with a/*.

10. Line 14th outputs a newline, and line 15th outputs a paragraph of text.

11. Let's look at the final effect:

Operator ****************

1. We have learned the variables before, but do not reflect the value of the variable, we have a simple assignment of a variable, see its value, this function is too weak.

2. After we introduce the operator, the function becomes stronger.

The operator in 3.PHP is still quite a lot, PHP manual is divided into 11 categories, here we first learn a few more commonly used bar.

Arithmetic operator ************

1. Arithmetic operators are mainly divided into + 、-、 *,/,% of these five, respectively, is subtraction and modulo.

2. Our operation produces a result, which can be assigned to a variable, such as we use $ A = 4 + 5; Then the value of $ A is 9.
3. Of course, above we are using two specific numerical calculations, in fact, you can use two variables or one is a variable is a specific number.

4. $ A = $b + 2; and $ A = $c + $d; both are the correct arithmetic operations.

5. We first write a xin12.php as follows:

<?php$a = $; $b = 33;//Add the variable $ A and the variable $b $c = $a + $b; echo "Adds the result:"; echo $c; echo "<br/>";//output a line break $d = $c + 2;echo "knot Results obtained after adding 2: "; echo $d;

6. Then we see the results of the operation as follows:


Division explaining ********************

1. Division is complicated because the programming languages also support different, some programming language two integer division, get the integer, such as Java, some programming language, is a decimal, such as our PHP.

2. For example, we create a new xin13.php and write the following code:

<?php$m = $n = 3; $d = $m/$n;//is the integer or decimal output? Echo $d;

3. The function of the above code is quite simple, it is to divide $m and $n, and then get the results to output, we look at it output is God horse thing:

Modulo Operation ******************

1. My first exposure to mold is in my high school math competition, the mold is indeed a profound knowledge, there are a lot of formula theorem.

2. To take the mold simply, is to take the remainder, its operator is a percent semicolon, that is,%.

3. For example, 22 modulo 3 is how much, because 22/3=7....1, so the final result is 1.

4. Let's take a look at the following code:

<?php$m = $n = 3; $d = $m% $n; echo $d;

5. Then we'll look at how it works:

6. There seems to be no problem, but I ask you,-5.1 modulo 2.1 is how much? Maybe some people will be cast off, how to take the mold?

7. In fact, can be taken, only need to grasp two o'clock can be: The 1th is to take a modulo operation before the first will be a decimal integer, it is the way to directly remove the fractional part, retain the integer part, the 2nd is the result of the operation is always the same as dividend, see the following example:

<?php$m = -5.1; $n = 2.1; $d = $m% $n; echo $d;

8. Then its output is such a drop:

9. The operation process is this: first processing the data, 5.1 to 5, 2.1 to 2, and then the quotient is-2, the remainder is-1, so the result is-1.

Description ***************************

1. As you can see, PHP does not support computing well, first of all its weak data types, although very convenient, but not suitable for the algorithm.

2. Then the number of data types is small and cannot be flexibly controlled.

3. There is also its computational speed, as a scripting language, or too slow.

4. Therefore, the PHP language is not suitable for the algorithm, in fact I have not seen who will abandon C and CPP or Java and Pascal, and then use PHP algorithm.

5. Therefore, we do not have to pay too much attention to its data calculation this piece, this is not its strength, and we do not have to be too obsessed with data calculation, because this is not the strength of PHP.

Summary *************************

1. We learned the string in this section, and we know how to represent it in single and double quotes.

2. Then learn the use of annotations.

3. Finally, we introduce the subtraction of four operators.

Mission Combat *************************

1. Suppose we have East China, north China, South China three large regions, each large area has 220 employees, we also have northwest, northeast, southwest three community, each district has 45 employees, please use the method of programming to calculate the number of employees in the company.

2. Answer:

3. Reference code:

<?php$big = 3 * 220;//calculate the number of Regions $small = 3*45;//Calculate the number of cells $all = $big + $small; echo "Total number of personnel of the company:"; Echo $all;

4. Sinsing PHP, look forward to your attention.

"2014" "Sinsing" "PHP" "Fall" "4" strings and comments and simple variable operations

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.