MyBB Community Forums

Full Version: Variable error
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was editing the Hellow world plugin as a template, and basically instead of writing hello world, I want it to write a variable, $i so I defined $i=1;

And then I edited the line in the post template so it wrote $i, but instead of echoing the number "1", it just echoes nothing.

Quote:<?php
/**
* MyBB 1.4
* Copyright © 2008 MyBB Group, All Rights Reserved
*
* Website: http://www.mybboard.net
* License: http://www.mybboard.net/about/license
*
* $Id: hello.php 4304 2009-01-02 01:11:56Z chris $
*/

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}

$plugins->add_hook("pre_output_page", "hello_world");
$plugins->add_hook("postbit", "hello_world_postbit");

function hello_info()
{
/**
* Array of information about the plugin.
* name: The name of the plugin
* description: Description of what the plugin does
* website: The website the plugin is maintained at (Optional)
* author: The name of the author of the plugin
* authorsite: The URL to the website of the author (Optional)
* version: The version number of the plugin
* guid: Unique ID issued by the MyBB Mods site for version checking
* compatibility: A CSV list of MyBB versions supported. Ex, "121,123", "12*". Wildcards supported.
*/
return array(
"name" => "Hello World!",
"description" => "A sample plugin that prints hello world and prepends the content of each post to 'Hello world!'",
"website" => "http://www.mybboard.net",
"author" => "MyBB Group",
"authorsite" => "http://www.mybboard.net",
"version" => "1.0",
"guid" => "",
"compatibility" => "*"
);
}

//loooooooooooooooooooool

$i=1;
function hello_world($page)
{
$page = str_replace("<div id=\"content\">", "hi", $page);
return $page;
}

function hello_world_postbit($post)

{
$post['message'] = "<strong>$i</strong><br /><br />{$post['message']}";
}
?>

Here is the edited file, with the two lines which are significant highlighted in red.
This isn't general MyBB Support /moving
The $i variable you are defining has to be defined inside the function where you're going to use it. If it can't be defined inside the function you need to either pass it to the function or declare it as a global variable in the start of the function. Also you need to return the contents of $post at the end of your function.

So you can do either of these

function hello_world_postbit($post)
{
$i=1;
$post['message'] = "<strong>$i</strong><br /><br />{$post['message']}";
return $post;
}
OR
function hello_world_postbit($post)
{
global $i;
$post['message'] = "<strong>$i</strong><br /><br />{$post['message']}";
return $post;
}