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

Returns a component of the current Drupal path.

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

Avoid use of this function where possible, as resulting code is hard to read. In menu callback functions, attempt to use named arguments. See the explanation in menu.inc for how to construct callbacks that take arguments. When attempting to use this function to load an element from the current path, e.g. loading the node on a node page, use menu_get_object() instead.

Parameters

$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.

29 calls to arg()
aggregator_form_category_submit in modules/aggregator/aggregator.admin.inc
Form submission handler for aggregator_form_category().
aggregator_form_feed_submit in modules/aggregator/aggregator.admin.inc
Form submission handler for aggregator_form_feed().
aggregator_page_category in modules/aggregator/aggregator.pages.inc
Page callback: Displays all the items aggregated in a particular category.
aggregator_page_last in modules/aggregator/aggregator.pages.inc
Page callback: Displays the most recent items gathered from any feed.
aggregator_page_rss in modules/aggregator/aggregator.pages.inc
Page callback: Generates an RSS 0.92 feed of aggregator items or categories.

... See full list

File

includes/bootstrap.inc, line 2870
Functions that need to be loaded on every Drupal request.

Code

function arg($index = NULL, $path = NULL) {
  // Even though $arguments doesn't need to be resettable for any functional
  // reasons (the result of explode() does not depend on any run-time
  // information), it should be resettable anyway in case a module needs to
  // free up the memory used by it.
  // Use the advanced drupal_static() pattern, since this is called very often.
  static $drupal_static_fast;
  if (!isset($drupal_static_fast)) {
    $drupal_static_fast['arguments'] = &drupal_static(__FUNCTION__);
  }
  $arguments = &$drupal_static_fast['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];
  }
}

Comments

I have begun to avoid using this function as it does not always work as you expect (at least from the description above).

Example:
Current page URL: /moduleX/doSomething
This URL refers to a function in ModuleX. In ModuleX arg(0) will be "moduleX" and arg(1) will be "doSomething".

But if ModuleX calls a function in AnotherModuleY, for some reason arg(0) and arg(1) don't return the same as when they are in ModuleX - they seem to return a URL that might have (theoretically) been used to call that module directly, such as "AnotherModuleY" and "doSomethingDifferent".

So now I just use:
$url_components = explode('/', request_uri());

Does request_uri() return the displayed url ?
(i mean, changed by pathauto for example or does it return the internal one ?)

Since I was using PathAuto, arg() was not returning the URL path, but the internal arguments. Using your approach allowed me to get the segments of the URL as printed in the browser with request_uri() and find my path that way.. You rule!

As an aside, when you do this,

$url_comp = explode('/', request_uri());

$url_comp[0] = null;
$url_comp[1] = what you would think arg(0) would return;
etc; etc;

I have spent over five hours trying to figure out how to get the variable into php with blocks. This did it for me. Had I only found your post sooner......

Thank you!!

Robert

You can also pass the drupal alias path into the arg() function.

arg(1, drupal_get_path_alias());

will return the argument from the alias path instead of the drupal route.

Hi Daggerhart,

This didn't seem to work for me. Can you advise how I can get this working. I am using Drupal 6.

$ref = arg(1, drupal_get_path_alias());
echo ref$;

If I use arg 0 I get an output of node, if I use 1, I get the nid, something like 1233

The above code seems to work the same as:

$ref = arg(0);
echo $ref;

but my path alias is something like sitename.com/europe/listing/united-kingdom/food/(node-title)

So it's not returning the path alias value. Any idea how to do this?

You almost definitely want top use request_path() , not request_uri() in the above.

Also, see this comment for differences between :

base_path()
request_uri()
request_path()
current_path()

http://api.drupal.org/comment/40018#comment-40018

F.e. url: /node/100
$args = explode('/', current_path());
$args[0]; // node
$args[1]; // 100

my url is www.mysite.com/list/23/book

i want to pass the argument 23 into a view in the page.tpl.php

print views_embed_view('commerce_cart_block', 'picklist_page', $view_arg);

How to i extract the 23 from the page url in drupal 7

Thanks

Try this docans :
print views_embed_view('commerce_cart_block', 'picklist_page', arg(1));