Jump to content

Forward slash or back slash


Xu Wei Jie

Recommended Posts

I am not sure if this is a PHP question. Generally, files and directories in Windows use backslash in their paths. Unix and others use forward slash. Correct me if I am wrong.

 

However, I wish to know how php functions(especially the file functions) interpret and handle the slashes. Is it platform independent? Sometimes, I do get errors when using the wrong slash.

 

Please advise me on this.

Link to comment
https://forums.phpfreaks.com/topic/151354-forward-slash-or-back-slash/
Share on other sites

Thanks Mark. This snippet is very neat. However, there are many values PHP_OS can take. This means I need to find out all the operating systems on how they handle slashes.

 

However, I want php to figure out the slash to use instead of hardcoding based on PHP_OS. Is there such a feature?

 

Anyone has a lot of experience with such file handling in PHP?

I have read this

 

http://sg.php.net/manual/kr/function.dirname.php

 

It states that

 

On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).

 

So is it safe that I can assume that forward slash / can be handled on any OS in PHP?

the way i do it is use the predifined constant called DIRECTORY_SEPARATOR

i would redefine it to something like DS to avoid having RLY long paths lol.

 

<?php
define( 'DS'	, DIRECTORY_SEPARATOR );
define( 'BASE' 	, __FILE__ );
$fs_path_array = explode( DS, BASE );
?>

 

works on both unix and windows systems.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.