PHP 函数,总是 returns 相同的输出
PHP function, always returns same output
下面是我在我的网站(这是一个 MyBB 论坛)上经常使用的代码,还有一个特定的网页,我已经写了 10 多次了。
为了使代码更易于阅读,我决定将它变成一个函数并每次使用不同的参数调用它。不幸的是它不工作,由于某种原因我一直得到 $fail 代码,而没有函数我得到 $success.
这是我第一次使用 PHP 函数,但从我在 w3schools 上读到的内容来看,理论上我应该做得很好。来练习的时候不多。介意检查我的语法并告诉我是否有误吗?
<?php
//function code
function canviewcheck($allowedgroups)
{
$groups = $mybb->user['usergroup'];
if ($mybb->user['additionalgroups']) {
$groups .= "," . $mybb->user['additionalgroups'];
}
$explodedgroups = explode(",", $groups);
$canview = 0;
foreach ($explodedgroups as $group) {
if (in_array($group, $allowedgroups)) {
$canview = 1;
echo $success;
break;
}
}
if (!$canview) {
echo $fail;
}
}
//Parameters that I want to use
$allowedgroups = [4, 19];
$pid = 'URL';
$success = "<a href=URL><span style='color:green; font-size:20px;'>SUCCESS</span></a>";
$fail = "<a href=" . $pid . "><span style='color:#DE1603; font-size:20px;'>FAIL</span>;</a>";
//Call to function
canviewcheck($allowedgroups, $pid, $success, $fail);
您需要将所有必要的参数添加到函数中,$mybb
也需要作为参数传递。
$pid
未在函数中使用,因此您无需将其作为参数传递。
<?php
//function code
function canviewcheck($allowedgroups, $success, $fail, $mybb)
{
$groups = $mybb->user['usergroup'];
if ($mybb->user['additionalgroups']) {
$groups .= "," . $mybb->user['additionalgroups'];
}
$explodedgroups = explode(",", $groups);
$canview = 0;
foreach ($explodedgroups as $group) {
if (in_array($group, $allowedgroups)) {
$canview = 1;
echo $success;
break;
}
}
if (!$canview) {
echo $fail;
}
}
//Parameters that I want to use
$allowedgroups = [4, 19];
$pid = 'URL';
$success = "<a href=URL><span style='color:green; font-size:20px;'>SUCCESS</span></a>";
$fail = "<a href=" . $pid . "><span style='color:#DE1603; font-size:20px;'>FAIL</span>;</a>";
//Call to function
canviewcheck($allowedgroups, $success, $fail, $mybb);
下面是我在我的网站(这是一个 MyBB 论坛)上经常使用的代码,还有一个特定的网页,我已经写了 10 多次了。
为了使代码更易于阅读,我决定将它变成一个函数并每次使用不同的参数调用它。不幸的是它不工作,由于某种原因我一直得到 $fail 代码,而没有函数我得到 $success.
这是我第一次使用 PHP 函数,但从我在 w3schools 上读到的内容来看,理论上我应该做得很好。来练习的时候不多。介意检查我的语法并告诉我是否有误吗?
<?php
//function code
function canviewcheck($allowedgroups)
{
$groups = $mybb->user['usergroup'];
if ($mybb->user['additionalgroups']) {
$groups .= "," . $mybb->user['additionalgroups'];
}
$explodedgroups = explode(",", $groups);
$canview = 0;
foreach ($explodedgroups as $group) {
if (in_array($group, $allowedgroups)) {
$canview = 1;
echo $success;
break;
}
}
if (!$canview) {
echo $fail;
}
}
//Parameters that I want to use
$allowedgroups = [4, 19];
$pid = 'URL';
$success = "<a href=URL><span style='color:green; font-size:20px;'>SUCCESS</span></a>";
$fail = "<a href=" . $pid . "><span style='color:#DE1603; font-size:20px;'>FAIL</span>;</a>";
//Call to function
canviewcheck($allowedgroups, $pid, $success, $fail);
您需要将所有必要的参数添加到函数中,$mybb
也需要作为参数传递。
$pid
未在函数中使用,因此您无需将其作为参数传递。
<?php
//function code
function canviewcheck($allowedgroups, $success, $fail, $mybb)
{
$groups = $mybb->user['usergroup'];
if ($mybb->user['additionalgroups']) {
$groups .= "," . $mybb->user['additionalgroups'];
}
$explodedgroups = explode(",", $groups);
$canview = 0;
foreach ($explodedgroups as $group) {
if (in_array($group, $allowedgroups)) {
$canview = 1;
echo $success;
break;
}
}
if (!$canview) {
echo $fail;
}
}
//Parameters that I want to use
$allowedgroups = [4, 19];
$pid = 'URL';
$success = "<a href=URL><span style='color:green; font-size:20px;'>SUCCESS</span></a>";
$fail = "<a href=" . $pid . "><span style='color:#DE1603; font-size:20px;'>FAIL</span>;</a>";
//Call to function
canviewcheck($allowedgroups, $success, $fail, $mybb);