Home Backend Development PHP Tutorial The role of DIRECTORY_SEPARATOR and PATH_SEPARATOR constants in php

The role of DIRECTORY_SEPARATOR and PATH_SEPARATOR constants in php

Jul 25, 2016 am 08:58 AM

This article introduces the functions of the two constants DIRECTORY_SEPARATOR and PATH_SEPARATOR in PHP. Friends in need can refer to it.

php constant 1, DIRECTORY_SEPARATOR DIRECTORY_SEPARATOR: path separator, which is ‘/’ on Linux and ‘’ on Windows

php constant 2,PATH_SEPARATOR PATH_SEPARATOR: Use to include multiple paths. Under Windows, when you need to include multiple paths, use ";" to separate them. Use ":" to separate them under Linux.

Note: The use of these two constants can avoid compatibility issues on different platforms.

For example, in zend framework, include setting index.php

<?php
set_include_path('.' . PATH_SEPARATOR . '../library/' . PATH_SEPARATOR . './application/models/'
. PATH_SEPARATOR . './application/lib/'
. PATH_SEPARATOR . get_include_path());
?>
Copy after login

get_include_path gets the current existing environment variables, plus the previous settings, it is the new system include.

Through this example, you can understand the usage of PATH_SEPARATOR.

Look again, DIRECTORY_SEPARATOR. PHP's internal constant, used to display the system separator command, can be used directly without any definition or inclusion.

The path separator under Windows is / (of course / can also run normally on some systems), and the path separator on Linux is. This will cause problems when deploying programs across platforms. For example, if the development machine is Windows and there is an image upload program, the designated upload file saving directory on the debugging machine is:

define(ROOT, dirname(__FILE__)."/upload");
Copy after login

The local debugging is normal, but after uploading to the Linux server, errors are found.

At this point, the DIRECTORY_SEPARATOR constant needs to come into play, it can be like this:

define(ROOT, dirname(__FILE__).DIRECTORY_SEPARATOR."upload");
Copy after login

DIRECTORY_SEPARATOR is a PHP built-in command that returns the path separator related to the operating system. It returns on Windows and returns / on Linux or Unix-like. Usually used when defining include file paths or upload saving directories.

For example:

<?php
const DIR_SEP = DIRECTORY_SEPARATOR;// 路径分割 win下\ linux下/  
private function __construct()  
{  
$this->_options = array(  
template_dir => templates . self::DIR_SEP, //模板文件所在目录  
cache_dir => templates . self::DIR_SEP . cache . self::DIR_SEP, //缓存文件存放目录  
auto_update => false, //当模板文件改动时是否重新生成缓存  
cache_lifetime => 0, //缓存生命周期(分钟),为 0 表示永久  
suffix => .html //模板文件后缀  
);  
}
?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

11 Best PHP URL Shortener Scripts (Free and Premium)

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Working with Flash Session Data in Laravel

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Introduction to the Instagram API

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Simplified HTTP Response Mocking in Laravel Tests

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

Build a React App With a Laravel Back End: Part 2, React

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

cURL in PHP: How to Use the PHP cURL Extension in REST APIs

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

12 Best PHP Chat Scripts on CodeCanyon

Notifications in Laravel Notifications in Laravel Mar 04, 2025 am 09:22 AM

Notifications in Laravel

See all articles