views:

53

answers:

2

What is the difference between realpath($path) and is_dir($path)?

I know realpath follows symbolic links, but is there a performance difference between the two?

+6  A: 

Realpath returns the canonicalized actual pathname of a file on success, is_dir returns a boolean value of whether or not the file is a directory.

http://php.net/manual/en/function.is-dir.php
http://php.net/manual/en/function.realpath.php

Rob
Yes but does `realpath` stat more folders than `is_dir` does? I know realpath returns a string if it finds the folder, which is the same as saying TRUE.
WarmWaffles
`is_dir("./") -> true` but `realpath("./") -> "/var/www"`. Realpath probably needs slightly more resources than is_dir, just because it has to find the whole path to your directory.
svens
If you just need to know whether the file is a directory, I'd go with `is_dir`. I would suspect the performance difference to be negligible and it seems to be more conventional to use is_dir in that situation.
Rob
Even if there is a performance difference, I'm sure it wouldn't be a huge difference, like a second or something. It would be tiny. I'm going to take a stab and say that is_dir is better, performance wise, because it just checks to see if the directory exists or not. You could even through file_exists into the mix.
Jason Lewis
`realpath("./") -> "/var/www"` However if you do `if(realpath("./")` it will return TRUE alsoEDIT: Just saw the previous comments, seems to make more sense
WarmWaffles
@WarmWaffles: But `if(realpath("../../index.php")) != is_dir("../../index.php")`. The two commands just have different purposes.
Felix Kling
A: 

How about looking at the manual.

realpath — Returns canonicalized absolute pathname

is_dir — Tells whether the filename is a directory

Jason Lewis
Already looked at it and nothing about performance was stated
WarmWaffles
Probably because the two do different enough things that it doesn't make sense to compare them performancewise. If you need the real path, use realpath(). If you need to know whether a path is a dir or not, use is_dir(). Choosing based solely on one or the other being slightly faster is called "premature optimization", which is the root of all evil. And BTW, did you ever even try to measure the difference? Unless you're doing it a million times in a loop, even an extra millisecond wouldn't matter.
cHao