[SOLVED] Access array inside a php function

Issue

Access array with same name $userinfo inside a php function

<?php 
    $userinfo['name'] = "bob";
    $userinfo['lastname'] = "johnson";

    function displayinfo() {
//not working 
    echo $userinfo['name']
//global also not working 
    echo global $userinfo['lastname'];

    }
    displayinfo();

?>

how to acess the arrays in the $userinfo var since it has more than one array in the same variable name?

echo $userinfo['name']
//global also not working 
echo global $userinfo['lastname'];

both do not working.

Solution

try this, for more details PHP Variable Scope

function displayinfo() {
  global $userinfo;
  echo $userinfo['lastname'];
}

Working example : https://3v4l.org/5l5NZ

Answered By – Rakesh Jakhar

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published.