Bạn được phép trả lại một và chỉ một giá trị từ các hàm và bạn làm điều này bằng cách sử dụng câu lệnh return. Trong ví dụ của chúng tôi, chúng tôi có thể đã sử dụng "return 'foo';"
Bạn có thể trả về bất kỳ biến nào bạn muốn, miễn là nó chỉ là một biến - nó có thể là số nguyên, chuỗi, kết nối cơ sở dữ liệu, v.v. Từ khóa "return" thiết lập giá trị trả về của hàm là bất kỳ biến nào bạn sử dụng với nó, sau đó thoát khỏi hàm ngay lập tức. Bạn cũng có thể chỉ cần sử dụng "return;", có nghĩa là "thoát mà không gửi lại giá trị. "
Hãy xem xét kịch bản này
Điều đó sẽ xuất ra "Đang hoạt động", theo sau là "1", và sau đó tập lệnh sẽ kết thúc. Lý do chúng ta không bao giờ thấy "Rời chức năng. " là do dòng "return 1" quay lại một dòng rồi thoát ngay lập tức - không bao giờ đạt được câu lệnh in thứ hai trong foo[]
Nếu bạn muốn trả lại nhiều hơn một giá trị, bạn cần sử dụng một mảng - điều này sẽ sớm được đề cập
Một điều phổ biến cần làm là trả về giá trị của một câu lệnh có điều kiện, e. g
return $i > 10;
Nếu $i thực sự lớn hơn 10, toán tử > sẽ trả về 1, do đó, nó giống như việc "trả về 1", nhưng nếu $i nhỏ hơn hoặc bằng 10, thì nó giống như "trả về 0"
Bạn muốn học PHP 7?
Hacking with PHP đã được cập nhật đầy đủ cho PHP 7 và hiện có sẵn dưới dạng PDF có thể tải xuống. Nhận hơn 1200 trang học PHP thực hành ngay hôm nay
Nếu điều này hữu ích, vui lòng dành chút thời gian để nói với những người khác về Hacking với PHP bằng cách tweet về nó
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 105
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1061
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1062
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1063
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1064
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1063
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1066
Factors of 24 are: 1 2 3 4 6 8 122
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1068
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1063
Factors of 24 are: 1 2 3 4 6 8 1250
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 105
Factors of 24 are: 1 2 3 4 6 8 1252
Factors of 24 are: 1 2 3 4 6 8 1253
Factors of 24 are: 1 2 3 4 6 8 1252
Factors of 24 are: 1 2 3 4 6 8 1255
Factors of 24 are: 1 2 3 4 6 8 1252
Factors of 24 are: 1 2 3 4 6 8 1257
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1062
Factors of 24 are: 1 2 3 4 6 8 122
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1050
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1063
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1052
_______553____554____362____23____557
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1063
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1059
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 105
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1051
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 105
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 105
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1054
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 105
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1056
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 103
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1068
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1051
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1030
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1031
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1032
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1033
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1034
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1035
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1031____559
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1038
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1039
Factors of 24 are: 1 2 3 4 6 8 1220
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1031
Factors of 24 are: 1 2 3 4 6 8 1221
Factors of 24 are: 1 2 3 4 6 8 1224
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1068
Factors of 24 are: 1 2 3 4 6 8 1226______362____734
Factors of 24 are: 1 2 3 4 6 8 1229
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1030
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 103
________ 35 ________ 739 ________ 930 ________ 935
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1051
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1037
đầu ra.
Factors of 24 are: 1 2 3 4 6 8 12
Ví dụ 2. Ví dụ này sử dụng chức năng danh sách để lưu trữ giá trị hoán đổi của biến. Nó được sử dụng để gán giá trị mảng cho nhiều biến cùng một lúc. Nhiều giá trị được trả về trong mảng từ hàm có thể được gán cho các biến tương ứng bằng list[].
php
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 106
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1039
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 105
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1051____930
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1057
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1054
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 103
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 105
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1056
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 105_______362____354
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1057
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1030
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1059
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1051
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1005
________ 206 ________ 207
________ 208 ________ 209
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1039
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1011____368
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1013
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1039
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1015
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1006
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1019
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1021
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1008
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1019
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1068
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1027
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1028
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1006_______557____208
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1032
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1006____557
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1008
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1059
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1039
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1038____368
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1013
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1039
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1015
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1006
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1019
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1021
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1008
Factors of 24 are: 1 2 3 4 6 8 1221
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1019
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1068
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 1037
đầu ra.
Before swapping the elements a = 10 b = 20 After swapping the elements a = 20 b = 10
PHP là ngôn ngữ kịch bản phía máy chủ được thiết kế dành riêng cho phát triển web. Bạn có thể học PHP từ đầu bằng cách làm theo Hướng dẫn PHP và Ví dụ về PHP này