Tìm hiểu PHP sprintf() Function (ok)
Áp dụng làm hàng ghế của rạp chiếu phim :)
<style type="text/css" media="screen">
.board {
position: relative;
width: 80%;
margin: 0 auto;
}
.screen {
text-align: center;
padding: 10px 0;
color: #fff;
font-size: 1.2em;
background: #F75959;
font-weight: bold;
border-radius: 10px;
margin: 10px 0;
}
.chairs {
position: absolute;
}
.chairs div {
padding: 10px 0;
font-weight: bold;
}
table.seats {
width: 40%;
margin: 0 auto;
border-spacing: 2px;
border-collapse: separate;
}
table.seats td {
padding: 9px;
background: #058576;
border-top-right-radius: 10px;
border-top-left-radius: 10px;
color: #fff;
font-size: 15px;
font-weight: bold;
cursor: pointer;
}
table.seats td.empty {
background: none;
cursor: none;
}
</style>
<?php
$rows = array('A','B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K');
?>
<div class="board">
<div class="screen">Màn hình</div>
<div class="chairs">
<?php
foreach ($rows as $row) {
echo sprintf("<div>%s</div>",$row);
}
?>
</div>
<table class="seats">
<tbody>
<?php
foreach ($rows as $row) {
?>
<tr>
<?php
for ($i = 1; $i < 17; $i++) {
$dataseat=sprintf("%s_%02d",$row,$i);
echo sprintf("<td dataseat='%s'>%02d</td>",$dataseat,$i);
if($i == 8) {
echo '<td class="empty"> </td><td class="empty"> </td><td class="empty"> </td><td class="empty"> </td>';
}
}
?>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Note: If there are more % signs than arguments, you must use placeholders. A placeholder is inserted after the % sign, and consists of the argument- number and "$". See example two. Lưu ý: Nếu có nhiều dấu% hơn đối số, bạn phải sử dụng trình giữ chỗ. Một trình giữ chỗ được chèn sau dấu% và bao gồm đối số- số và "\ $". Xem ví dụ hai.
<?php
$num = 54;
$course = 'PHP training';
$year = 2018;
$format = 'There are %2$d students in %1$s batch in the %3$d year';
echo $res = sprintf($format, $course, $num, $year);
?>
There are 54 students in PHP training batch in the 2018 yearĐể nhớ được cách sử dụng của sprintf chúng ta chỉ cần nhớ các công dụng chính của nó + Xử lý khoảng trắng của chuỗi + Xử lý số thập phân
Additional format values. These are placed between the % and the letter (example %.2f):
+ (Forces both + and - in front of numbers. By default, only negative numbers are marked) ví dụ: $num2 = -123; sprintf("%%+d = %+d",$num2); kết quả: %+d = -123; Theo mặc định chỉ có số âm mới để đấu
' (Specifies what to use as padding): Ví dụ: $str1 = "Hello"; sprintf("[%'*8s]",$str1); kết quả: [***Hello]
: Hello có 5 chữ nó bổ sung thêm 3 * để thành 8. Chỉ định những gì sẽ sử dụng làm phần đệm. Mặc định là khoảng trắng.Chú ý- (Left-justifies the variable value), mặc định là căn phải Ví dụ: Căn trái cho giá trị biến
[0-9] (Specifies the minimum width held of to the variable value)
.[0-9] (Specifies the number of decimal digits or maximum string length) Ví dụ: $money = 252; echo sprintf('%01.4f', $money); kết quả 252.0000
sprintf( "%f \n ", 123.4567 ); // Displays "123.456700" (default precision) sprintf( "%.2f \n ", 123.4567 ); // Displays "123.46" sprintf( "%.0f \n ", 123.4567 ); // Displays "123" sprintf( "%.10f \n ", 123.4567 ); // Displays "123.4567000000"

Xoay quanh 4 phép toán: +,-,*,% và một số ký tự đặng biệt ' và . và [09] và $
Last updated
Was this helpful?