Of course! Let's break down the global keyword and $GLOBALS array in the context of DedeCMS and its runphp feature. This is a very common point of confusion for developers new to PHP within the Dede ecosystem.

The Core Concept: global vs. $GLOBALS
First, it's crucial to understand the difference between the global keyword and the $GLOBALS array in standard PHP, as this is the foundation of your question.
$GLOBALS Array
-
What it is:
$GLOBALSis a special, "superglobal" PHP array that contains all variables available in the global scope of the script. -
How it works: You can access and modify global variables directly using
$GLOBALS['variable_name']from anywhere in your script (inside a function, class method, etc.) without needing theglobalkeyword. -
Example:
(图片来源网络,侵删)$my_var = "Hello World"; // This is a global variable function test() { // We can access and modify the global variable directly $GLOBALS['my_var'] = "Goodbye World"; echo $GLOBALS['my_var']; // Outputs: Goodbye World } test(); echo $my_var; // Outputs: Goodbye World (the original variable was changed)
global Keyword
-
What it is: The
globalkeyword is used to import a global variable into the local scope of a function. -
How it works: When you use
global $my_var;inside a function, you are creating a local reference (a pointer) to the global variable$my_var. Any changes you make to this local reference will affect the original global variable. -
Example:
$my_var = "Hello World"; function test() { // Import the global variable into this function's scope global $my_var; $my_var = "Goodbye World"; // This changes the global variable echo $my_var; // Outputs: Goodbye World } test(); echo $my_var; // Outputs: Goodbye World
In simple terms:

$GLOBALSis like a master list of all global variables. You can reach into it from anywhere.globalis like creating a shortcut to a specific global variable inside a function.
The Practical Use Case in DedeCMS runphp
In DedeCMS, you will most often encounter this inside a template file (.htm) when using the {dede:field name='xxx' runphp='yes'} or {dede:global name='xxx' runphp='yes'} tags.
The code inside a runphp block is executed within a function-like scope. This means that by default, variables from the main script's global scope are not available. You must explicitly import them.
When and Why to Use global in runphp
You use the global keyword inside a runphp block to access DedeCMS's core global variables, such as:
$dsql: The main database connection object. This is the most common use case.$env: An array containing environment information like the current ID ($env['id']), channel ID ($env['channelid']), etc.$cfg_cmspath: The installation path of DedeCMS.$cfg_basedir: The absolute path to the DedeCMS data directory.
When to Use $GLOBALS in runphp
You can technically use $GLOBALS to access the same variables. However, it's generally considered less clean and more prone to accidental modification of the entire global scope.
Example: Accessing $dsql to get an article's title
Let's say you want to display the title of another article in your current article's detail page.
Method 1: Using global (Recommended)
This is the most common and readable way.
{dede:field name='id' runphp='yes'}
// Import the global database object and environment variables
global $dsql, $env;
// Get the current article's ID
$aid = @me; // @me holds the value of the field, which is the current article ID
// Let's say we want to get the title of article with ID = 10
$target_id = 10;
$query = "SELECT title FROM `#@__archives` WHERE id = $target_id";
$row = $dsql->GetOne($query);
// Check if a result was found and assign it to @me
if(is_array($row)) {
@me = $row['title'];
} else {
@me = "Article not found";
}
{/dede:field}
Method 2: Using $GLOBALS (Functional but less common)
This works perfectly fine but can be seen as slightly more verbose.
{dede:field name='id' runphp='yes'}
// Access the global database object directly from the $GLOBALS array
$dsql = &$GLOBALS['dsql'];
$env = &$GLOBALS['env'];
// Get the current article's ID
$aid = @me;
// Let's say we want to get the title of article with ID = 10
$target_id = 10;
$query = "SELECT title FROM `#@__archives` WHERE id = $target_id";
$row = $dsql->GetOne($query);
// Check if a result was found and assign it to @me
if(is_array($row)) {
@me = $row['title'];
} else {
@me = "Article not found";
}
{/dede:field}
(Note: The & in $dsql = &$GLOBALS['dsql']; creates a reference, which is more memory-efficient than a copy.)
Best Practices and Common Pitfalls
-
Use
globalfor Readability: When working inside arunphpblock, usingglobal $dsql;is standard practice and makes your code easier for other Dede developers to understand. -
Always Sanitize Input: When using variables like
$env['id']in queries, ensure they are integers to prevent SQL injection.// GOOD $aid = (int)$env['id']; // BAD (Vulnerable to SQL Injection) $aid = $env['id'];
-
Remember
@me: The@mevariable is special inrunphp. It holds the initial value of the field. You must assign your final result to@mefor it to be displayed in the template. -
Don't Overuse: Only import the global variables you actually need. Importing the entire global scope with
global $GLOBALS;is incorrect and will not work as expected.
Summary Table
| Method | Syntax | Use Case in runphp |
Pros | Cons |
|---|---|---|---|---|
global Keyword |
global $dsql; |
Recommended. To import specific global variables (like $dsql, $env) into the local scope of the runphp block. |
- Clean and readable. - Industry standard for this context. - Clearly states intent. |
- Requires a line for each variable you need. |
$GLOBALS Array |
$dsql = $GLOBALS['dsql']; |
To access a global variable without importing it. Can be useful if you only need one variable and want to avoid the global line. |
- Direct access. - No need to "import" first. |
- Slightly more verbose. - Can be seen as less clean. - Risk of accidentally modifying other global variables if not careful. |
Final Answer: In DedeCMS runphp, you will use the global keyword to import core variables like $dsql and $env so you can use them inside the runphp block's local scope. While you can use $GLOBALS, using global is the more common and conventional approach.
