简体   繁体   English

我不明白为什么这个 php 函数不起作用

[英]I don't understand why this php function is not working

I am currently learning php.我目前正在学习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.我使用 foreach 循环遍历列表。 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')
?>

The Problem is Variable Scopes.问题是变量范围。
Variables that are used and defined inside any function in PHP are by default, local variables; PHP 中任何函数中使用和定义的变量默认为局部变量; 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更多关于 PHP 变量

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. (1) 【常用方式】将变量作为输入传递给函数。
(2) Using "global": (2) 使用“全局”:

<?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')
?>

You defined $multy variable outside of the function, so you need to pass it as a second parameter.您在函数外部定义了$multy变量,因此您需要将其作为第二个参数传递。 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上面的代码在这里测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM