TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   DIRECTORY_SEPARATOR not necessary (http://www.talkphp.com/general/2487-directory_separator-not-necessary.html)

TerrorRonin 03-18-2008 12:51 AM

DIRECTORY_SEPARATOR not necessary
 
In attempting to write cross-platform, portable PHP engine, I was using PHP's DIRECTORY_SEPARATOR constant to write path strings(eg ".." . DIRECTORY_SEPARATOR . "foo.php", because the proper way to write it in Windows would be "\" while in Unix it would be "/".

Well, an engineer from php.net pointed out to me that, and a few other programmers confirmed that using that constant is completely unnecessary. As long as you use the forward slash (/), you'll be fine. Window's doesn't mind it, and it's the best for *nix operating systems.

(Note that DIRECTORY_SEPARATOR is still useful for things like exploding a path that the system gave you.)

Hope this is useful to people.

Wildhoney 03-18-2008 01:39 AM

Very useful. Thanks Robin! It leaves me astonished how PHP cannot stick to naming conventions even with their constants. I have to admit, I've not heard of the DIRECTORY_SEPARATOR before -- my bad! However, if they'd prepended it with PHP_, I may have stood a chance at finding it. That way it would have appeared in the auto-complete list on the day I found PHP_EOL.

sjaq 03-18-2008 07:31 PM

But can you use both of them in one request? Because I always use getcwd() which returns something like this in Windows: C:\Bla\Bla and if I append my path without using DIRECTORY_SEPARATOR I will get something like this: C:\Bla\Bla/linux/style/path but will that work?

xenon 03-19-2008 09:53 PM

Yes, it will. *nix paths (and mixed) are compatible with windows, but the inverse is not true. *nix systems will fail to resolve a path given as "\usr\bin", for instance.


All times are GMT. The time now is 03:12 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0