

$rest = substr("abcdef", -3, 1) // returns "d" Send mail from PHP $rest = substr("abcdef", -2) // returns "ef" The realpath() function gives you a canonicalized absolute path Kind of useful, but still based on the current working directory. $rest = substr("abcdef", -1) // returns "f" The file path of the file that we want to move.

Using PHP, we are going to move sample-file.txt from directorya to directoryb. Negative array key is difference from negative positional index for string, which indexes position from the rear of the string. In directorya, we have a text file called sample-file.txt. which means go back to the previous directory, so now the path is C:\xampp\htdocs, as. You can use negative array key in an associative array. As in the above code, the current absolute pathname is printing with the help of realpath( ) function as realpath( NULL ) if the value is NULL, then the realpath( ) function returns the absolute path of the current directory, which is C:\xampp\htdocs\programs, next the path run is. Var_dump(explode(' ', $str2)) Negative Array Index vs. On the other hand, the explode() splits a string into an array. Use built-in function implode() to join the elements of the array into a string, e.g., I need to dynamically get the URL (not the absolute path) to the directory the Class is being loaded so I can pass this URL to enqueuescript and enqueuestyle for asset loading. To reload (refresh) the current page, you may: I have a PHP Class written for use in WordPress themes or plugins that loads some JS and CSS using enqueuescript and enqueuestyle when the Class is initialized. We can apply this function with the result of the above two functions. echo dirname(FILE) Output: /var/We can use the basename() function to get the current working directory name without the path in PHP. Use the header() function, followed by an exit command (not too continue the current script). Paths and Directories in PHP Get the Absolute Path of a File. For example, write the dirname() function in the index.php file with the FILE constant as the parameter. Using SERVER‘SCRIPTNAME’: SERVER is an array of stored information such as headers, paths, and script locations. You can use function basename() to get the filename without the path or basename(path, suffix) to get the filename without the extension. Since we are using PHP, we can easily get the current file name or directory name of the current page by using SERVER‘SCRIPTNAME’.

For example, currently I know to get the current directory path instead of the url. Both the above syntaxes will return the same. I have a php file that needs to know it's current directory url to be able to link to something relative to itself. The current full-path filename is kept in system variable $_SERVER. In order to get the root directory path, you can use DIR or dirname(). Let’s take some examples of using the pathinfo() function.
