What does $url= $url[1] do?

Discussion in 'PHP' started by geomark, Oct 15, 2009.

  1. #1
    I'm trying to debug a Drupal template and don't understand what this line of code does:

    if(is_array($url)) $url = $url[1];
     
    geomark, Oct 15, 2009 IP
  2. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If the $url variable is an array it will convert it to whatever is in the second element of the array (position 1) - probably a string.
     
    premiumscripts, Oct 15, 2009 IP
  3. sunchiqua

    sunchiqua Peon

    Messages:
    47
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It'll convert url to a variable, containing second element of it's current array.
     
    sunchiqua, Oct 16, 2009 IP
  4. sodevrom

    sodevrom Member

    Messages:
    51
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    if(is_array($url)) //tests to see if the varaible is an array

    $url = $url[1]; //if $url is an array, than $url will be converted to string with the value from the $url array from position 1.

    Eg.:

    $url[0]="hello"
    $url[1]="man"
    $url[2]="what are you doing?"

    if the array looks like that, after you execute the piece of code from above, you will get :
    $url=$url[1] -> $url="man"

    Hope it helps,
    Vlad
     
    sodevrom, Oct 16, 2009 IP
  5. geomark

    geomark Peon

    Messages:
    924
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok, got it. It confused me seeing an array set equal to an element of the array. But the result is it converts the array to a string. Thanks guys.
     
    geomark, Oct 16, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    premiumscripts, Oct 16, 2009 IP