프로그래밍/PHP
PHP Magic Constant
Developer_hong
2021. 5. 12. 16:19
반응형
dirname(__FILE__)
echo __FILE__; // 파일명을 포함한 경로
echo dirname(__FILE__); //파일명을 제외한 경로
상황 : dirname(__FILE__)이라고 입력된 소스코드(현재 소스코드)파일명은 index.php이고 위치는 folder/path/sehee이다
따라서 folder/path/sehee/index.php 이렇다고 칠때,
__FILE__은 현재 소스코드가 위치하고있는 경로를 말한다.
따라서 echo __FILE__; 하면 folder/path/sehee/index.php이 출력된다.
dirname(__FILE__)은 파일명을 제외한 경로이므로
따라서 echo dirname(__FILE__); 하면 folder/path/sehee이 출력된다.
php내부예약어이므로 자세한 내용은 php.net에서 확인 가능
출처: https://88240.tistory.com/109
추가)
- __LINE__: Indicate current line number of the file.
- __FILE__: This constant is used to indicate the current directory in which the file is present.
- __FUNCTION__: This constant is used to indicates the path of the file.
- __CLASS__: This constant Returns the class name.
- __METHOD__: Returns the function name.
- __DIR__: This constant is used to indicate the current directory in which the file is present.
- __NAMESPACE__: Displays the namespace in which we are working.
<?php
function fun_magic(){
echo __LINE__."<BR/>";
echo __FILE__."<BR/>";
echo __DIR__."<BR/>";
ECHO __FUNCTION__."<BR/>";
ECHO __METHOD__."<BR/>";
ECHO __NAMESPACE__."<BR/>";
}
fun_magic();
?>
출처: https://www.c-sharpcorner.com/UploadFile/d9da8a/five-magic-constant-use-in-php/
반응형