【PHP】basename,require,require_once

Sep 4, 2020 PHP

I will summarize what I learned about PHP as a memorandum.

#basename basename is a function that returns the part of the name at the end of the path.

For example, if you write as follows, the output result will be’index.html’.


<? php
print basename ("/etc/index.html");
?>

#result
index.html

You can also cut characters from the end by setting a value in the second argument.


<? php
print basename ("/etc/index.html", ".html");
?>

#result
index

#requre, require_once Requre, require_once are used when loading libraries and other PHP files.

require

There is no problem with the description method as shown below. You can specify either an absolute path or a relative path.


require "file path";
require ("file path");

If you want to use a function in another file, write it like this.


# test1.php
require'./test2.php';
test2 ();

# test2.php
function test2 () {
print'test2';
}

#Execution result
test2

require_once

Unlike the repuire statement, the repuire_once statement does not reload the file if it has already been read. In the case of the repuire statement, if the library or configuration file is unintentionally reloaded, a function redefinition error or variable rewriting will occur. The require_once statement can prevent this.

#Reference URL https://www.php.net/manual/ja/function.basename.php https://uxmilk.jp/26454