Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. How to fix this error $_SESSION:Notice: Undefined offset: 0

How to fix this error $_SESSION:Notice: Undefined offset: 0

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
helpphptutorial
3 Posts 2 Posters 20 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ELMAGLAYA
    wrote on last edited by
    #1

    Hi sorry I am just a baby in PHP. I want to get the value of $_SESSION['username'] but it gives me this error : Notice: Undefined offset: 0. Please see my code below. Your help will be greatly appreciated. Thanks. "; echo $_SESSION[0]; //I want an output "andrew" but it fails.

    J 1 Reply Last reply
    0
    • E ELMAGLAYA

      Hi sorry I am just a baby in PHP. I want to get the value of $_SESSION['username'] but it gives me this error : Notice: Undefined offset: 0. Please see my code below. Your help will be greatly appreciated. Thanks. "; echo $_SESSION[0]; //I want an output "andrew" but it fails.

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      $_SESSION (see PHP: $_SESSION - Manual[^] ) is an associative array containing key value pairs (see PHP: Arrays - Manual[^]). These are not accessed by an index but by their keys which might be integers or strings. Because you have not assigned a value with the key '0' you will get the error. Just use the key instead:

      echo $_SESSION['username'];

      E 1 Reply Last reply
      0
      • J Jochen Arndt

        $_SESSION (see PHP: $_SESSION - Manual[^] ) is an associative array containing key value pairs (see PHP: Arrays - Manual[^]). These are not accessed by an index but by their keys which might be integers or strings. Because you have not assigned a value with the key '0' you will get the error. Just use the key instead:

        echo $_SESSION['username'];

        E Offline
        E Offline
        ELMAGLAYA
        wrote on last edited by
        #3

        Thank you sir.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups