Tên tệp đường dẫn đầy đủ hiện tại được giữ trong biến hệ thống $_SERVER['PHP_SELF']
. Bạn có thể sử dụng chức năng basename[]
để lấy tên tệp mà không cần đường dẫn; . Ví dụ,
Chuyển hướng đến trang khác
Sử dụng hàm header[]
, theo sau là lệnh exit
[không tiếp tục tập lệnh hiện tại]. Ví dụ,
Để tải lại [làm mới] trang hiện tại, bạn có thể
Mảng in
Sử dụng hàm có sẵn
$a = array[-1 => 5]; echo $a[-1]; var_dump[$a];0 để nối các phần tử của mảng thành một chuỗi, e. g. ,
Mặt khác,
$a = array[-1 => 5]; echo $a[-1]; var_dump[$a];1 tách một chuỗi thành một mảng
$str1 = 'apple,orange,banana'; var_dump[explode[',', $str1]]; $str2 = 'apple orange banana'; var_dump[explode[' ', $str2]];
Chỉ số mảng phủ định so với. Chỉ mục vị trí chuỗi phủ định
Bạn có thể sử dụng khóa mảng âm trong một mảng kết hợp. Ví dụ,
$a = array[-1 => 5]; echo $a[-1]; var_dump[$a];
Khóa mảng phủ định khác với chỉ mục vị trí phủ định cho chuỗi, chỉ mục này lập chỉ mục vị trí từ phía sau chuỗi. Ví dụ,
Just was curious to count files and lines in a project
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
{
$file_read = array[ 'php', 'html', 'js', 'css' ];
$dir_ignore = array[];
$scan_result = scandir[ $dir ];
foreach [ $scan_result as $key => $value ] {
if [ !in_array[ $value, array[ '.', '..' ] ] ] {
if [ is_dir[ $dir . DIRECTORY_SEPARATOR . $value ] ] {
if [ in_array[ $value, $dir_ignore ] ] {
continue;
}
$result = DirLineCounter[ $dir . DIRECTORY_SEPARATOR . $value, $result, false ];
}
else {
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
0
{
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
1
{
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
2
{
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
3
{
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
4
{
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
5
{
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
6
{
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
7
{
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
8
{
function DirLineCounter[ $dir , $result = array['lines_html' => false, 'files_count' => false, 'lines_count' => false ], $complete_table = true ]
9
{
$file_read = array[ 'php', 'html', 'js', 'css' ];
0
$dir_ignore = array[];
$file_read = array[ 'php', 'html', 'js', 'css' ];
1
$dir_ignore = array[];
$file_read = array[ 'php', 'html', 'js', 'css' ];
2
$dir_ignore = array[];
$file_read = array[ 'php', 'html', 'js', 'css' ];
3
$dir_ignore = array[];
A key problem to hierarchical include trees is that PHP processes include paths relative to the original file, not the current including file.
A solution to that, is to prefix all include paths with:
this will generate a base path relative to the current file, which will then allow an include behavior similar to C/C++.
thus, to include a file that is 1 in the parent directory:
to include a file that is in the same directory:
to include a file that is in a subdirectory:
Notice that all paths we reference must NOT begin with a /, and must be relative to the current file, in order to concatenate correctly.