Home > Blockchain >  Create a variable and make it available globally
Create a variable and make it available globally

Time:12-18

how can I define my own "global" variable for MyBB?

Problem statement: User-A has a "1" in the "sick" column; if he is not sick, he has a "0". An icon is defined for the "1" state.

if $mybb-user('ill') == 1 {
$icon_ill = '<img src="./images/ill.png" />')
} else {
$icon_ill = '';
}

If User-B now calls up a page where User-A is also displayed, the variable $icon_ill should be displayed after the user name.

I am aware that I have to manually integrate the variable everywhere in the templates and individual PHP files as well as plug-ins. But first you have to have them.

I can't find a solution to implement this at the moment and ask the PHP professionals for support.


Edit: to clarify, displaying the actual variable is not my problem, but that it is only displayed if there is also a "1" in the User-A column.

But how does the definition have to be executed if someone calls up member profiles or the user list, for example?

if (???? && mybb->user['ill'] == 1) {
echo 'Show icon';
} else {
ech 'no icon';
}

So that he is informed about the state of health of the user where in the

column "ill" == 1

Example

User-X > Show icon > User-A (because ill is 1)
User-X > no icon > User-B - Users-J (because ill is 0)
User-X > Show icon > User-K (because ill is 1)
User-X > no icon > Users-L - Users Z (because ill is 0)

CodePudding user response:

Thanks for the support Moeez.

Displaying the actual variable is not my problem, but that it is only displayed if there is also a "1" in the User-A column.

But how does the definition have to be executed if someone calls up member profiles or the user list, for example?

if (???? && mybb->user['ill'] == 1) {
echo 'Show icon';
} else {
ech 'no icon';
}

So that he is informed about the state of health of the user where in the

column "ill" == 1

Example

User-X > Show icon > User-A (because ill is 1)
User-X > no icon > User-B - Users-J (because ill is 0)
User-X > Show icon > User-K (because ill is 1)
User-X > no icon > Users-L - Users Z (because ill is 0)

CodePudding user response:

you can use the global keyword.

Here is an example of how you might do this:

<?php

// Declare the variable as global
global $myVariable;

// Set the value of the variable
$myVariable = 'Hello, world!';

// Use the variable in a function
function printMyVariable() {
global $myVariable;
echo $myVariable;
}

// Call the function
printMyVariable();

This code will create a global variable called $myVariable and set its value to 'Hello, world!'. The variable will be available to all functions and scripts in the PHP script.

In this example, the printMyVariable function uses the global keyword to access the $myVariable variable from within the function. When the function is called, it will print the value of the `$myVariable`` variable.

Keep in mind that using global variables can make your code more difficult to maintain and debug. It is generally recommended to avoid using global variables if possible and to use alternative approaches such as dependency injection or passing variables as arguments to functions instead.

  • Related