Hướng dẫn sử dụng hàm php

Hàm PHP [thường gọi là hàm], có rất nhiều hàm khác nhau được khai báo sẵn trong php, chúng ta chỉ gọi và sử dụng, tuy nhiên chúng ta cũng có thể viết những hàm dành riêng cho một tính năng bất kỳ nào

  • Điểm mạnh của chức năng là khả năng tái sử dụng nhiều lần
  • Chức năng sẽ không thực thi khi tải trang, mà được thực thi thông qua cuộc gọi của chúng

Constructor

function tên_hàm[] {
    Code được thực thi;
}
tên_hàm[]; // Gọi function
?>

  • tên_hàm có thể dùng chữ hoặc số, viết thường hay viết hoa, dấu gạch dưới, tuy nhiên chữ cái đầu không được là số
  • Cách gọi hàm khá đơn giản, chỉ cần viết lại tên_hàm[] là được

Ví dụ

PHP không phân biệt gọi hàm trước và hàm sau khai báo, cả 2 cách gọi đều giống nhau
Calling on the future as after

PHP là ngôn ngữ kịch bản máy chủ và là công cụ mạnh mẽ để tạo các trang Web động và tương tác

PHP là một giải pháp thay thế được sử dụng rộng rãi, miễn phí và hiệu quả cho các đối thủ cạnh tranh như ASP của Microsoft

Bắt đầu học PHP ngay bây giờ »

Học dễ dàng với "PHP Tryit"

Với trình chỉnh sửa "PHP Tryit" trực tuyến của chúng tôi, bạn có thể chỉnh sửa mã PHP và nhấp vào nút để xem kết quả

Thí dụ



echo "Tập lệnh PHP đầu tiên của tôi. “;
?>


Tự mình thử »

Nhấp vào nút "Tự dùng thử" để xem nó hoạt động như thế nào

Bài tập PHP

Kiểm tra bản thân với các bài tập

Bài tập

Chèn phần còn thiếu của mã bên dưới để xuất "Xin chào thế giới"


Cung cấp câu trả lời "

Ví dụ PHP

Tìm hiểu bằng các ví dụ. Hướng dẫn này bổ sung tất cả các giải thích với các ví dụ rõ ràng

Xem tất cả các ví dụ về PHP

Bài kiểm tra PHP

Học bằng cách làm bài kiểm tra. Bài kiểm tra này sẽ cung cấp cho bạn tín hiệu về mức độ bạn biết hoặc không biết về PHP

Bắt đầu bài kiểm tra PHP

Việc học của tôi

Theo dõi tiến trình của bạn với chương trình "Học tập của tôi" miễn phí tại W3Schools

Đăng nhập vào tài khoản của bạn và bắt đầu kiếm điểm

Đây là một tính năng tùy chọn. Bạn có thể học W3Schools mà không cần sử dụng My Learning


Tài liệu tham khảo PHP

Tài liệu tham khảo PHP của W3Schools chứa các danh mục khác nhau của tất cả các hàm, từ khóa và hằng số PHP, cùng với các ví dụ

If you're trying to use exec in a script that uses signal SIGCHLD, [i.e. pcntl_signal[SIGCHLD,'sigHandler'];] it will return -1 as the exit code of the command [although output is correct!]. To resolve this remove the signal handler and add it again after exec. Code will be something like this:

...
pcntl_signal[SIGCHLD, 'sigHandler'];
...
...
[more codes, functions, classes, etc]
...
...
// Now executing the command via exec
// Clear the signal
pcntl_signal[SIGCHLD, SIG_DFL];
// Execute the command
exec['mycommand',$output,$retval];
// Set the signal back to our handler
pcntl_signal[SIGCHLD, 'sigHandler'];
// At this point we have correct value of $retval.

Same solution can apply to system and passthru as well.

The verbal descriptions take a while to read through to get a feel for the expected results for fopen modes. This csv table can help break it down for quicker understanding to find which mode you are looking for:

Mode,Creates,Reads,Writes,Pointer Starts,Truncates File,Notes,Purpose
r,,y,,beginning,,fails if file doesn't exist,basic read existing file
r+,,y,y,beginning,,fails if file doesn't exist,basic r/w existing file
w,y,,y,beginning+end,y,,"create, erase, write file"
w+,y,y,y,beginning+end,y,,"create, erase, write file with read option"
a,y,,y,end,,,"write from end of file, create if needed"
a+,y,y,y,end,,,"write from end of file, create if needed, with read options"
x,y,,y,beginning,,fails if file exists,"like w, but prevents over-writing an existing file"
x+,y,y,y,beginning,,fails if file exists,"like w+, but prevents over writing an existing file"
c,y,,y,beginning,,,open/create a file for writing without deleting current content
c+,y,y,y,beginning,,,"open/create a file that is read, and then written back down"

After lots of research and testing, I'd like to share my findings about my problems with Internet Explorer and file downloads.

  Take a look at this code, which replicates the normal download of a Javascript:

________số 8_______

Now let me explain:

  I start out by checking for IE, then if not IE, I set Content-type [case-sensitive] to JS and set Content-Disposition [every header is case-sensitive from now on] to inline, because most browsers outside of IE like to display JS inline. [User may change settings]. The Content-Length header is required by some browsers to activate download box. Then, if it is IE, the "application/force-download" Content-type is sometimes required to show the download box. Use this if you don't want your PDF to display in the browser [in IE]. I use it here to make sure the box opens. Anyway, I set the Content-Disposition to attachment because I already know that the box will appear. Then I have the Content-Length again.

  Now, here's my big point. I have the Cache-Control and Pragma headers sent only if not IE. THESE HEADERS WILL PREVENT DOWNLOAD ON IE!!! Only use the Expires header, after all, it will require the file to be downloaded again the next time. This is not a bug! IE stores downloads in the Temporary Internet Files folder until the download is complete. I know this because once I downloaded a huge file to My Documents, but the Download Dialog box put it in the Temp folder and moved it at the end. Just think about it. If IE requires the file to be downloaded to the Temp folder, setting the Cache-Control and Pragma headers will cause an error!

I hope this saves someone some time!
~Cody G.

Chủ Đề