This is the complete code. I don’t understand what the backslash in line 39 means. Can you help me explain it?
<!doctype html>
<html> <head> <title>Make an annual calendar</title> <style> // body{text-align:center;} . box{margin:0 auto;width:880px;} .title{background:#ccc;} table{height:200px;width:200px;font-size:12px;text-align:center;float :left;margin:10px;font-family:arial;} </style> </head> <body> <?php function calendar( $y)//// 1. Define the almanac generation function { // 3. Get the week value of January 1st of the specified year $w = date('w', strtotime("$y- 1-1"));
$html = '<div class="box">';
// 2. Splice the table for each month for ( $m = 1; $m <= 12; ++$m) { $html .= '<table>'; $html .= '<tr class="title"> <th colspan="7">' . $y . 'Year' . $m . 'Month</td></tr>'; $html .= '<tr>< td>Day</td><td>One</td><td>Two</td><td>Three</td><td>Four</td>< td>五</td><td>Six</td></tr>';
// Get the number of days in the current month $m t', strtotime("$y-$m")); // Loop from the first day of the month to the last day for ($d = 1; $d <= $max; ++$d) { . # if ($w && $d == 1) { // If the first day of the month is not a Sunday, fill in the blanks $html .= "<td colspan=\"$w\"> </td>"; ## } elseif ($d == $max) { $w = ($w + 1 > 6) ? 0 : $w + 1; $html .= html .= '</div>'; return $html; } echo calendar('2017'); ?> </body> </html>
It is not recommended to write this, you’d better write it like this
I suggest you learn more about string splicing methods and efficiency issues
Found it, it is an escape character. But why can’t we directly $html .= "<td colspan="$w"> </td>";
I know an error will be reported
Parse error: syntax error, unexpected '$w' (T_VARIABLE) in /home/phpcn6peh2pwcun/wwwroot/compile.php(41) : eval()'d code on line 39
Require $html .= "<td colspan=\"$w\"> </td>";
But why is this?
This is the complete code. I don’t understand what the backslash in line 39 means. Can you help me explain it?
<!doctype html>
<html><head>
<title>Make an annual calendar</title>
<style>
// body{text-align:center;}
. box{margin:0 auto;width:880px;}
.title{background:#ccc;}
table{height:200px;width:200px;font-size:12px;text-align:center;float :left;margin:10px;font-family:arial;}
</style>
</head>
<body>
<?php
function calendar( $y)//// 1. Define the almanac generation function
{
// 3. Get the week value of January 1st of the specified year
$w = date('w', strtotime("$y- 1-1"));
$html = '<div class="box">';
// 2. Splice the table for each month
for ( $m = 1; $m <= 12; ++$m) {
$html .= '<table>';
$html .= '<tr class="title"> <th colspan="7">' . $y . 'Year' . $m . 'Month</td></tr>';
$html .= '<tr>< td>Day</td><td>One</td><td>Two</td><td>Three</td><td>Four</td>< td>五</td><td>Six</td></tr>';
// Get the number of days in the current month $m
t', strtotime("$y-$m"));
// Loop from the first day of the month to the last day
for ($d = 1; $d <= $max; ++$d) {
. #
if ($w && $d == 1) { // If the first day of the month is not a Sunday, fill in the blanks
$html .= "<td colspan=\"$w\"> </td>";
##
} elseif ($d == $max) { $w = ($w + 1 > 6) ? 0 : $w + 1;
$html .= html .= '</div>';
return $html;
}
echo calendar('2017');
?>
</body>
</html>