[Học PHP cơ bản]: Bài 3 – Hiển thị ra màn hình trong PHP
Trong bài 3 về chuỗi bài học PHP cơ bản này, Như lần trước AnonyHome có giới thiệu thì hôm nay chúng ta sẽ tìm hiểu về cách hiển thị ra màn hình trong PHP
Trong PHP ta có thể sử dụng hai lệnh chính để hiển thị ra màn hình đó là echo
hoặc print
.
Echo
echo
được sử dụng để hiển thị kết quả của một hoặc nhiều chuỗi ký tự, biến hoặc biểu thức toán học….
1 |
<span style="box-sizing: border-box; color: #999999; font-weight: bold;"><?php</span><br /> <span style="box-sizing: border-box; font-weight: bold;">echo</span> <span style="box-sizing: border-box; color: #dd1144;">"Hello!"</span>;<br /> <span style="box-sizing: border-box; color: teal;">$name</span> = <span style="box-sizing: border-box; color: #dd1144;">"PHP"</span>;<br /> <span style="box-sizing: border-box; font-weight: bold;">echo</span> <span style="box-sizing: border-box; color: teal;">$name</span>;<br /> <span style="box-sizing: border-box; font-weight: bold;">echo</span> <span style="box-sizing: border-box; color: teal;">1</span> + <span style="box-sizing: border-box; color: teal;">1</span>;<br /><span style="box-sizing: border-box; color: #999999; font-weight: bold;">?></span> |
Khi cần hiển thị nhiều chuỗi cùng một lúc chúng ta sử dụng dấu
,
để phân biệt giữa các chuỗi với nhau:
1 |
<span style="box-sizing: border-box; color: #999999; font-weight: bold;"><?php</span><br /> <span style="box-sizing: border-box; color: teal;">$a </span>= <span style="box-sizing: border-box; color: #dd1144;">"Cr Thắng"</span>;<br /> <span style="box-sizing: border-box; color: teal;">$b</span> = <span style="box-sizing: border-box; color: #dd1144;">"Blog"</span>;<br /> <span style="box-sizing: border-box; font-weight: bold;">echo</span> <span style="box-sizing: border-box; color: teal;">$a</span>, <span style="box-sizing: border-box; color: #dd1144;">" "</span>, <span style="box-sizing: border-box; color: teal;">$b</span>;<br /><span style="box-sizing: border-box; color: #999999; font-weight: bold;">?></span> |
print
được dùng tương tự như echo
để hiển thị giá trị của một hoặc nhiều chuỗi, biến hoặc kết quả của biểu thức…
1 |
<span style="box-sizing: border-box; color: #999999; font-weight: bold;"><?php</span><br /> <span style="box-sizing: border-box; color: teal;">$a</span> = <span style="box-sizing: border-box; color: #dd1144;">"Xin Chào"</span>;<br /> <span style="box-sizing: border-box; font-weight: bold;">print</span> <span style="box-sizing: border-box; color: teal;">$a</span>;<br /><span style="box-sizing: border-box; color: #999999; font-weight: bold;">?></span> |
Tuy nhiên, khác với
echo
thì câu lệnh print
luôn trả về giá trị là 1.
1 |
<span style="box-sizing: border-box; color: #999999; font-weight: bold;"><?php</span><br /> <span style="box-sizing: border-box; color: teal;">$result</span> = <span style="box-sizing: border-box; font-weight: bold;">print</span> <span style="box-sizing: border-box; color: #dd1144;">"Xin chào"</span>;<br /> <span style="box-sizing: border-box; font-weight: bold;">echo</span> <span style="box-sizing: border-box; color: teal;">$result</span>;<br /><span style="box-sizing: border-box; color: #999999; font-weight: bold;">?></span> |
Khác với
echo
, chúng ta cũng có thể sử dụng print
để in ra các giá trị thuộc kiểu dữ liệu mảng:
1 |
<span style="box-sizing: border-box; color: #999999; font-weight: bold;"><?php</span><br /> <span style="box-sizing: border-box; color: teal;">$arrInt</span> = [<span style="box-sizing: border-box; color: teal;">1</span>, <span style="box-sizing: border-box; color: teal;">2</span>, <span style="box-sizing: border-box; color: teal;">3</span>];<br /> <span style="box-sizing: border-box; font-weight: bold;">print</span> <span style="box-sizing: border-box; color: teal;">$arrInt</span>;<br /><span style="box-sizing: border-box; color: #999999; font-weight: bold;">?></span> |
Chúng ta sẽ tìm hiểu về kiểu dữ liệu mảng trong các bài học tiếp theo.
Ngoài ra bạn cũng có thể sử dụng hàm
print_r()
(print readable) để hiển thị kết quả theo một định dạng dễ đọc hơn.
1 |
<span style="box-sizing: border-box; color: #999999; font-weight: bold;"><?php</span><br /> <span style="box-sizing: border-box; color: teal;">$arr</span> = [<span style="box-sizing: border-box; color: teal;">1</span>, <span style="box-sizing: border-box; color: teal;">2</span>, <span style="box-sizing: border-box; color: teal;">3</span>];<br /> print_r(<span style="box-sizing: border-box; color: teal;">$arr</span>);<br /><span style="box-sizing: border-box; color: #999999; font-weight: bold;">?></span> |
Bạn lưu ý vì
print_r()
là một hàm nên khi sử dụng bạn cần đặt dấu ngoặc đơn xunh quanh biến, chuỗi hoặc biểu thức… đưa vào.Chúng ta sẽ tìm hiểu về hàm trong các bài học tiếp theo.
Bài tiếp theo: [Học PHP cơ bản]: Bài 4 – Biến trong PHP
Bài trước: [Học PHP cơ bản]: Bài 2 – Cú pháp trong PHP