10/10/2018, 09:33
[Q] Variable in PHP????
Về nguyên tắc, biến được dùng trong PHP không cần phải định nghĩa, chương trình sẽ tự động gán giá trị thích hợp. Nhưng mỗi khi mình dùng biến chưa được khởi tạo thì lập tức sẽ bị báo lỗi khi chạy chương trình. Những chương trình như vậy không hề bị lỗi nếu chạy trên server nhưng lại luôn bị lỗi khi chạy trên localhost. Bạn nào biết nguyên nhân xin chỉ dùm.
Thanks.
Thanks.
Bài liên quan
error_reporting(<gia tri>);
trong PHP Documentation có thể down tại www.php.net
Nếu bạn install PHP tại máy bạn chỉ đề theo dạng custom rồi chọn cách hiện thị error và warning chỉ là display all error thì sẽ giống trên server ngay thôi.
Bạn nên biết được, thế nào là error, thế là là warning và thế nào là notice trong PHP nhé.
Thân,
error_reporting -- set which PHP errors are reported
Description
int error_reporting ( [int level])
The error_reporting() function sets the error_reporting directive at runtime. PHP has many levels of errors, using this function sets that level for the duration (runtime) of your script.
error_reporting() sets PHP's error reporting level, and returns the old level. The level parameter takes on either a bitmask, or named constants. Using named constants is strongly encouraged to ensure compatibility for future versions. As error levels are added, the range of integers increases, so older integer-based error levels will not always behave as expected.
Some example uses:
Example 1. error_reporting() examples
<?php
// Turn off all error reporting
error_reporting(0);
// Report simple running errors
error_reporting (E_ERROR | E_WARNING | E_PARSE);
// Reporting E_NOTICE can be good too (to report uninitialized
// variables or catch variable name misspellings ...)
error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// Report all errors except E_NOTICE
// This is the default value set in php.ini
error_reporting (E_ALL ^ E_NOTICE);
// Report all PHP errors (bitwise 63 may be used in PHP 3)
error_reporting (E_ALL);
// Same as error_reporting(E_ALL);
ini_set ('error_reporting', E_ALL);
?>
The available error level constants are listed below. The actual meanings of these error levels are described in the error handling section of the manual.
Table 1. error_reporting() level constants and bit values
value constant
1 E_ERROR
2 E_WARNING
4 E_PARSE
8 E_NOTICE
16 E_CORE_ERROR
32 E_CORE_WARNING
64 E_COMPILE_ERROR
128 E_COMPILE_WARNING
256 E_USER_ERROR
512 E_USER_WARNING
1024 E_USER_NOTICE
2047 E_ALL
See also the display_errors directive and ini_set().
- Mình tạo một file html thế này :
<form action = "processorder.php" method = "get">
<table border = 0>
<tr>
<td>number</td>
<td align = center ><input type = "text" name = "number" size = 3></td>
</tr>
<tr>
<td align = center colspan = 2 bgcolor = #cccccc>
<input type = "submit" value = "Submit Order" ></td>
</tr>
</table>
</form>
- Và mình tạo file processorder.php dưới đây để xử lý nó :
<html>
<head>
</head>
<body>
<?php
echo $number;
?>
</body>
</html>
- Khi chạy, tức là khi gọi file php từ file html thì nó báo lỗi như thế này :
Notice: Undefined variable: number in c:\www\practice\processorder1.php on line 6
- Thư hơi dài nhưng mình nghĩ là đọc nó cũng nhanh thôi. Nhờ các bạn chi giùm nhé. Cảm ơn các bạn.
Để cho chắc ăn, bạn nên dùng $HTTP_POST_VARS["number"] hơn là dùng $number.
Riêng bạn java, bạn đã sửa lại như thế nào, mong bạn nói rõ cách giải quyết để các bạn khác sau này gặp trường hợp tương tự thì có cách giải quyết.
Thân,
Trường hợp của mình, mình cài php để chạy trên localhost chứ không làm server.
Có một vấn đề nữa là, nếu dùng method = "get" trong form tag thì thay vì truy cập biến là $HTTP_POST_VARS["number"] , mình truy cập biến bằng $HTTP_GET_VARS["number"], kết quả vẫn được như vậy. Mình đưa method = "get" là để muốn kiểm tra xem những biến nào được chuyển cho file .php để xử lý.
Quả thực là được các bạn góp ý và giúp đỡ, mình vỡ ra nhiều điều, khi mà nhiều sách vở hiện nay chỉ chú trọng đến những mảng nổi của php mà không thực sự có những chỉ dẫn sâu sắc và có tính chiều sâu. Một lần nữa cảm ơn các bạn và hy vọng sẽ được mọi người góp ý trong những lần tới.
Friend in need is friend indeed.
Thân,