-1

below there's a code that I use very often on my website (it's a MyBB forum) and there's a specific web page where I have written it 10+ times.

To make code easier to read I decided to turn it into a function and call it with different arguments each time. Unfortunately it isn't working, for some reason I keep getting the $fail code while without the function I get $success.

It's the first time I use PHP functions but from what I read at w3schools I should be doing good, theoretically. Not much when coming to practice. Mind checking my syntax and tell me if it is wrong?

<?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);
michalhosna
  • 7,327
  • 3
  • 21
  • 40
Viktor
  • 9
  • 7
  • 1
    The variable `$mybb` isn't set in the function. You should be getting an error from everything that uses that variable. – Barmar Oct 26 '20 at 21:11
  • why do you call the function with 4 parameters(`$allowedgroups, $pid, $success, $fail`), while the function is defined to only take in 1 parameter (`$allowedgroups`)? – jibsteroos Oct 26 '20 at 21:11
  • @Barmar $mybb is a global variable on MyBB forums, I can use it in all pages of the board without issues – Viktor Oct 26 '20 at 21:16
  • @jibsteroos I have edited the text of my question, when I posted it I accidentally pasted the code used during my last test – Viktor Oct 26 '20 at 21:17
  • Global variables aren't available inside functions unless you declare them with `global $mydb;` in the function. – Barmar Oct 26 '20 at 21:17
  • See https://stackoverflow.com/questions/2531221/giving-my-function-access-to-outside-variable – Barmar Oct 26 '20 at 21:17

1 Answers1

0

You need to add all the necessary parameters to the function, and $mybb also needs to be passed as an argument.

$pid isn't used in the function, so you don't need to pass that as an argument.

<?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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Looks strange but let me try in a second, I'm convinced $mybb can be used globally without further declarations – Viktor Oct 26 '20 at 21:18