4.6.x bootstrap.inc arg($index)
4.7.x path.inc arg($index)
5.x path.inc arg($index)
6.x path.inc arg($index = NULL, $path = NULL)
7.x bootstrap.inc arg($index = NULL, $path = NULL)

Return a component of the current Drupal path.

When viewing a page at the path "admin/content/types", for example, arg(0) would return "admin", arg(1) would return "content", and arg(2) would return "types".

Avoid use of this function where possible, as resulting code is hard to read. Instead, attempt to use named arguments in menu callback functions. See the explanation in menu.inc for how to construct callbacks that take arguments.


$index: The index of the component, where each component is separated by a '/' (forward-slash), and where the first component has an index of 0 (zero).

$path: A path to break into components. Defaults to the path of the current page.

Return value

The component specified by $index, or NULL if the specified component was not found. If called without arguments, it returns an array containing all the components of the current path.

17 calls to arg()
aggregator_page_category in modules/aggregator/aggregator.pages.inc
Menu callback; displays all the items aggregated in a particular category.
aggregator_page_last in modules/aggregator/aggregator.pages.inc
Menu callback; displays the most recent items gathered from any feed.
aggregator_page_rss in modules/aggregator/aggregator.pages.inc
Menu callback; generate an RSS 0.92 feed of aggregator items or categories.
aggregator_page_source in modules/aggregator/aggregator.pages.inc
Menu callback; displays all the items captured from a particular feed.
block_admin_display_form in modules/block/block.admin.inc
Generate main blocks administration form.

... See full list


includes/path.inc, line 162
Functions to handle paths in Drupal, including path aliasing.


function arg($index = NULL, $path = NULL) {
  static $arguments;
  if (!isset($path)) {
    $path = $_GET['q'];
  if (!isset($arguments[$path])) {
    $arguments[$path] = explode('/', $path);
  if (!isset($index)) {
    return $arguments[$path];
  if (isset($arguments[$path][$index])) {
    return $arguments[$path][$index];


LittleViking’s picture

If you're using Path or Pathauto and configure node 123123 to have the path http://yoursite.com/publication/book/number/123123, the arg() function will still return the internal node path.

For example, even though your address bar shows http://yoursite.com/publication/book/number/123123
print arg(0); // prints 'node'
print arg(1); // prints '123123'

ayalsule’s picture

Because publication ,book ,number and all this words are alias path not really argument
the argument are o => node and 1=> 123123

mtz’s picture

I am showing a block view in my sidebar filtering the contents by taxonomy term. My node is at host.com/events/new-york/some-event ... How can I get my view to use "new-york" as an argument in this case?

("new-york" is inserted in the URL based on the taxonomy term of the node which has the content type "events")



Edit: Managed to get it working using the code below:

if (arg(0) == 'node' && is_numeric(arg(1))) {
    $node = node_load(arg(1));

	if (count($node->taxonomy) > 0)  {
	  foreach ($node->taxonomy as $term) {
		$term = $term->name;

	$term = strtolower($term); // for some reason needed in my case

else {
	$term = arg(1);
	$term = str_replace('-', ' ', $term); // for some reason needed in my case

return $term;

Shai’s picture


I think drupal_get_path_alias() may be your friend.

Possibly helpful reference on this topic is the discussion at and near this comment:


heyshiv’s picture

I am trying to use the arg function to get the node id, however, it returns me the name of the function in which I am using the arg().

function firstName_lastName () {

drupal_set_message(arg(1), 'check', FALSE);


This returns-> 'lastName'

Any ideas why it is not returning the node id?


PrplPplEtr’s picture

...just a guess, but does your path look something like:

http://www.your.site.com/drupal/lastName/1 ...?

Because, from your question, it seems like you're just grabbing the wrong part of the path.

Did you try:

function firstName_lastName () {

drupal_set_message(arg(2), 'check', FALSE);// note the "2" in arg()

cptX’s picture

Hi, what about this kind of argument "node/594#comment-60" . How can I read the #comment-60 from the url?

Jimmed’s picture

The part of the URL you're referring to is called the 'hash' or 'fragment', and I'm afraid you won't be able to access it from the server-side at all, as most browsers do not send this to the server.

You can, however, use JavaScript to get it on the client-side, using the value of location.hash, which returns everything after (and including) the # symbol in the requested URL.

mikeytown2’s picture

$arg = arg();
if (isset($arg[0]) && isset($arg[1]) && $arg[0] == 'node' && is_numeric($arg[1])) {
  // Do stuff with the node id: $arg[1]
timofey’s picture

You saved my life. I was stuck with isset(arg(0)).

Ayesh’s picture

These values are cached already for a single page load so using thing multiple times will not cause huge performance problems.

memyselfandm’s picture

Thus, site.com/cool-page and site.com/Cool-Page will return a different result for arg(0). Make sure you strtolower(arg(0)) if this is not desired (probably should get this in core, as I think most people dont desire that).

drupalfan81’s picture

I tried this but without luck:
$ref = arg(1, drupal_get_path_alias());
echo $ref;

It seems to still return the internal drupal path args, so like arg(0) would be node, and arg(1) would be the nodeid on a node view page.

@MTZ, your code works, but why not just use PHP REQUEST_URI to get the URL and then search for values and act on them.

for example I use this:

$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (false !== strpos($url,'united-kingdom')) {
display stuff that pertains to the united-kingdom
else {
Display some general information

My paths look something like sitename.com/content/united-kingdom/(node-title). I have various content and the path changes based on the taxonomy term (country) that is selected.

Ankit Agrawal’s picture

For getting the node alias instead of the node id try this:
$arg = arg();
if (isset($arg[0]) && isset($arg[1]) && $arg[0] == 'node' && is_numeric($arg[1])) {
$system_path = 'node/' . arg(1);
$current_path = drupal_get_path_alias($system_path);