Danh mục

Giải pháp thiết kế web động với PHP - p 8

Số trang: 10      Loại file: pdf      Dung lượng: 603.49 KB      Lượt xem: 9      Lượt tải: 0    
10.10.2023

Hỗ trợ phí lưu trữ khi tải xuống: 1,000 VND Tải xuống file đầy đủ (10 trang) 0

Báo xấu

Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

HOW TO WRITE PHP SCRIPTSCombining calculations and assignmentPHP offers a shorthand way of performing a calculation on a variable and reassigning the result to the variable through combined assignment operators. The main ones are listed in Table 3-3. Table 3-3. Combined arithmetic assignment operators used in PHP Operator += -= *= /= %= Example $a += $b $a -= $b $a *= $b $a /= $b $a %= $b Equivalent to $a = $a + $b $a = $a - $b $a = $a * $b $a = $a / $b $a = $a % $bAdding to an existing stringThe...
Nội dung trích xuất từ tài liệu:
Giải pháp thiết kế web động với PHP - p 8 HOW TO WRITE PHP SCRIPTSCombining calculations and assignment PHP offers a shorthand way of performing a calculation on a variable and reassigning the result to the variable through combined assignment operators. The main ones are listed in Table 3-3. Table 3-3. Combined arithmetic assignment operators used in PHP Operator Example Equivalent to += $a += $b $a = $a + $b -= $a -= $b $a = $a - $b *= $a *= $b $a = $a * $b /= $a /= $b $a = $a / $b %= $a %= $b $a = $a % $bAdding to an existing string The same convenient shorthand allows you to add new material to the end of an existing string by combining a period and an equal sign, like this: $hamlet = To be; $hamlet .= or not to be; Note that you need to create a space at the beginning of the additional text unless you want both strings to run on without a break. This shorthand, known as the combined concatenation operator, is extremely useful when combining many strings, such as you need to do when building the content of an email message or looping through the results of a database search. The period in front of the equal sign is easily overlooked when copying code. When you see the same variable repeated at the beginning of a series of statements, it s often a sure sign that you need to use .= instead of = on its own.All you ever wanted to know about quotes—and more Handling quotes within any computer language—not just PHP—can be fraught with difficulties because computers always take the first matching quote as marking the end of a string. Structured Query Language (SQL)—the language used to communicate with databases—also uses strings. Since your strings may include apostrophes, the combination of single and double quotes isn t enough. Moreover, PHP gives variables and escape sequences (certain characters preceded by a backslash) special treatment inside double quotes. Over the next few pages, I ll unravel this maze and make sense of it all for you. 51 CHAPTER 3 How PHP treats variables inside strings Choosing whether to use double quotes or single quotes might just seem like a question of personal preference, but there s an important difference in the way that PHP handles them. • Anything between single quotes is treated literally as text. • Double quotes act as a signal to process variables and special characters known as escape sequences. Take a look at the following examples to see what this means. In the first example (the code is in quotes1.php), $name is assigned a value and then used in a single-quoted string. As you can see from the screenshot alongside the code, $name is treated like normal text. $name = Dolly; // Single quotes: $name is treated as literal text echo Hello, $name; If you replace the single quotes in the final line with double ones (see quotes2.php), $name is processedDownload from Wow! eBook and its value is displayed onscreen. $name = Dolly; // Double quotes: $name is processed echo Hello, $name; In both examples, the string in the first line is in single quotes. What causes the variable to be processed is the fact that it s in a double-quoted string, not how it originally got its value. Because double quotes are so useful in this way, many people use them all the time. Technically speaking, using double quotes when you don t need to process any variables is inefficient. My preference ...

Tài liệu được xem nhiều: