0

I am currently learning php. I just coded this function and I was hoping for the function to take a name and check if the name is in the multi array. If the name is contained in it, it will give back the name and the salary and job.

I used a foreach to loop through the list. I created the list outside of the function in a global scope. But am getting this error when I run the code. It is saying "multy is undefined". I don't understand why because is defined.

The following message occurs:

Notice: Undefined variable: multy in C:\xampp\htdocs\projects\index.php on line 19

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\projects\index.php on line 19

Here's my code

<?php

$multy = [
    [
        'staff' => 'ben', 'job' => 'cooking', 'salary' => 1500,
    ],
    [
        'staff' => 'cy', 'job' => 'chef', 'salary' => 2000,
    ],
    [
        'staff' => 'sylva', 'job' => 'software engineer', 'salary' => 15000,
    ],
];

function checkingList($name){
  foreach($multy as $mult){
    if($mult['staff'] === $name){
      echo $mult['staff'] .', You are hired. Your job is ' . $mult['job'].' and your salary is'. $mult['salary'];
    }
  }
}
checkingList('sylva')
?>
miile7
  • 2,547
  • 3
  • 23
  • 38
Sylva
  • 11
  • 1
  • 4
    `$multy` is not defined inside the [scope](https://www.php.net/manual/en/language.variables.scope.php) of your function. You need to give it to the function as a parameter to access the array, i.e. `function checkingList($name, $multy){` and then invoke the function: `checkingList('sylva', $multy)` – lovelace Nov 06 '20 at 09:30

2 Answers2

0

The Problem is Variable Scopes.
Variables that are used and defined inside any function in PHP are by default, local variables; which means that they can only be interpreted inside the functions by the values that are given to them.
More on Variable Scope
More on PHP Variables

There are 2 ways to use a variable inside a function that is defined outside of its scope:
(1) [Common Way] Pass the variable to the function as an input.
(2) Using "global":

<?php

$multy = [
    [
        'staff' => 'ben', 'job' => 'cooking', 'salary' => 1500,
    ],
    [
        'staff' => 'cy', 'job' => 'chef', 'salary' => 2000,
    ],
    [
        'staff' => 'sylva', 'job' => 'software engineer', 'salary' => 15000,
    ],
];

function checkingList($name){

  //changed code here:
  global $multy;
  //end of change

  foreach($multy as $mult){
    if($mult['staff'] === $name){
      echo $mult['staff'] .', You are hired. Your job is ' . $mult['job'].' and your salary is'. $mult['salary'];
    }
  }
}
checkingList('sylva')
?>
  • It is good to avoid using global .. in function that loops through some array it is logical that this array var is declared as funciton var not fetched from global. Even if it is a global get it before calling the method and pass it to it. – Svetoslav Nov 06 '20 at 09:40
  • 1
    You are absolutely correct. My goal was to introduce all the possibilities. – Andrew Sharifikia Nov 06 '20 at 09:43
  • I agree with @AlirezaSharifikia to show this possibility. Just for the OP here is something why to avoid `global` in php: https://stackoverflow.com/questions/12445972/stop-using-global-in-php – miile7 Nov 06 '20 at 10:05
0

You defined $multy variable outside of the function, so you need to pass it as a second parameter. So your code will be like this :

$multy = [
    [
        'staff' => 'ben', 'job' => 'cooking', 'salary' => 1500,
    ],
    [
        'staff' => 'cy', 'job' => 'chef', 'salary' => 2000,
    ],
    [
        'staff' => 'sylva', 'job' => 'software engineer', 'salary' => 15000,
    ],
];

function checkingList($name, $multy){
  foreach($multy as $mult){
    if($mult['staff'] === $name){
      echo $mult['staff'] .', You are hired. Your job is ' . $mult['job'].' and your salary is '. $mult['salary'];
    }
  }
}
checkingList('cy', $multy); 
// cy, You are hired. Your job is chef and your salary is 2000

Above code tested here

STA
  • 30,729
  • 8
  • 45
  • 59
  • Thanks, for your quick response STA, i appreciate. I just implemented it and is working but when they function is passed a name that is not in the list, is false therefore it doesn't echo anything but i want it to echo ''$name, you are not on the list. Please how can i implement that? i have tried using else if on it but i didn't get the expected result. – Sylva Nov 06 '20 at 10:24
  • you can simply use a flag. initially it's false. if found in loop, make the flag true and do the regular thing to do. check the flag at the end of the loop. if it is false return not found. – zahid hasan emon Nov 06 '20 at 10:46