1

I would like to know which is the best practice (and why) when declaring variables or constants that contain a path or URI: with trailing slash or not?

With:

define(MODULE_URL,'http://mysite.com/modules/');
$var = MODULE_URL . 'module-dir/file.ext';

Without:

define(MODULE_URL,'http://mysite.com/modules');
$var = MODULE_URL . '/module-dir/file.ext';
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
Naoise Golden
  • 8,793
  • 4
  • 49
  • 65
  • Duplicate I think... http://stackoverflow.com/questions/3888997/dynamic-urls-with-or-without-a-trailing-slash and here http://webmasters.stackexchange.com/questions/2498/does-it-make-a-difference-if-your-url-ends-in-a-trailing-slash-or-not – David May 18 '11 at 11:51
  • @dve the webmasters link is not relevant. This is about concatenation, not about SEO scores. – Mel May 18 '11 at 12:33

3 Answers3

3

I use this:

define(MODULE_URL,'http://mysite.com/modules');
$var = MODULE_URL . '/module-dir/file.ext';

Because it forces consistency, and gives me cleaner looking pathnames if I ever have to output them, since this:

$var = MODULE_URL . 'module-dir/file.ext';

won't work. whereas this:

define(MODULE_URL,'http://mysite.com/modules/');
$var = MODULE_URL . 'module-dir/file.ext';

Let's you get away with:

$var = MODULE_URL . 'module-dir/file.ext';
$var = MODULE_URL . '/module-dir/file.ext';

So if I went to output $var with the second example, I'd have this:

http://mysite.com/modules//module-dir/file.ext

Which looks pretty ugly.

onteria_
  • 68,181
  • 7
  • 71
  • 64
  • I see all answers are rather subjective more than based in experienced but I concur with the "force consistency" philosophy. Thank you – Naoise Golden May 23 '11 at 09:55
1

On the basis that // will be interpreted as / then I think that you should always have trailing slashes at the end of your directory names. So both

define(MODULE_URL,'http://mysite.com/modules/');
$var1= MODULE_URL . 'module-dir/file.ext';
$var2 = MODULE_URL . '/module-dir/file.ext';

$var1 and $var2 point to the same place. Without the trailing slash this is not the case.

I also think that is fairly standard practice throughout languages.

satnhak
  • 9,407
  • 5
  • 63
  • 81
  • thank you, I see this is a rather subjective issue. I feel more inclined to force consistency, although I see your reasoning. I'd like to see how this is a _standard practice_ but I guess it will come with experience. – Naoise Golden May 23 '11 at 09:54
  • It's not *that* subjective. If you have the slash on the end then you can guarantee that you are pointing to a directory, if not it might be a file without extension. Requiring the slash to be added before the file is not enforcing convention, it is just annoying; "the odd '//' is ugly" doesn't actually cut it as a reason to prefer no '/'. Generally you want the property `uri = directoryName + fileName`. but by losing the trailing slash you don't have either. – satnhak May 23 '11 at 12:20
0

I prefer without trailing slash, simply because %s/%s is more readable than %s%s and having double slashes counts towards the maximum path length. This isn't an issue with URI's and on most systems you will not ever hit the length, but I'd like to be consistent across all environments and cases. Additionally, I can do things like:

define(MODULE_URL, 'http://example.com/modules');
$mod['en'] = MODULE_URL . '_en/' . $mod_name;

As you don't always control how someone creates their hierarchy.

Mel
  • 6,077
  • 1
  • 15
  • 12