PHP increment a variable in a function. The function runs in a forloop. resets to 0?

Hi I have a very simple PHP function which when runs always returns the value of 1. I need to increment the value of the variable $board to 1,2,3,4,5,6 and so on. Cannot catch the error here ?

function poste() {

            $board++;
            echo $board;

            global $sourcedir;

            require_once($sourcedir . '/Subs-Post.php');

            $msgoptions = array(
                    'id' => 0,
                    'body' => 'Welcome',
                    'subject' => 'Welcome To The Boards',
            );
          $topicoptions = array(
                    'id' => 0,
                    'board' => $board,
                    'poll' => null,
                    'lock_mode' => 0,
                    'sticky_mode' => 0,
                    'mark_as_read' => false,
            );


            $posteroptions = array(
                    'update_post_count' => 1,
            );

            createPost($msgoptions, $topicoptions, $posteroptions);
    }

    for($board = 1; $board <= 3; $board++ ){
    $board++;
    echo $board;
    poste();
    }

Answers


$board in the for loop is not the same in your function. function is "subprogram" so it's a different variable.

Use something like poste($board).

function poste($board) {

        $board++;
        echo $board;

        global $sourcedir;

        require_once($sourcedir . '/Subs-Post.php');

        $msgoptions = array(
                'id' => 0,
                'body' => 'Welcome',
                'subject' => 'Welcome To The Boards',
        );
      $topicoptions = array(
                'id' => 0,
                'board' => $board,
                'poll' => null,
                'lock_mode' => 0,
                'sticky_mode' => 0,
                'mark_as_read' => false,
        );


        $posteroptions = array(
                'update_post_count' => 1,
        );

        createPost($msgoptions, $topicoptions, $posteroptions);
}

for($board = 1; $board <= 3; $board++ ){
$board++;
echo $board;
poste($board);
}

This should work.


Need Your Help

Setting JFrame Defaults

java swing jframe

There seems to be three lines where I'm wondering why do I need them. To me, it just seems like Java could eliminate this by setting it as the default and letting you turn off.

Central sources of NinePatch Images?

android user-interface resources nine-patch

Are there any central spots or collection in the net from created NinePatches?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.