Jump to content

How to use variable as global variable ?


arif_shariati

Recommended Posts

The global keyword does not make a variable global between pages and in fact forget that ever saw the global keyword as using it where it does do something, results in bad programming.

 

To allow a php variables to exist between pages, you would use $_SESSION variables. See this introduction link - http://us.php.net/manual/en/features.sessions.php and the session reference link on that page - http://us.php.net/manual/en/book.session.php

 

Setting a session variable on a page -

<?php
session_start(); // start or resume a session
$_SESSION['some_name'] = 'your data';
?>

 

Referencing a session variable on a different page -

<?php
session_start(); // start or resume a session
echo $_SESSION['some_name'];
?>

Link to comment
Share on other sites

Actually i knew about the session. But i am developing a board game in php. I am using a variable array which is used for 2 dimensional array. for good programming i have placed every functions in seprate pages say init_board (), fill_board () etc. Now in different pages when i want to use array, it is not called or accessed.

Link to comment
Share on other sites

Perhaps if you posted your code that demonstrates the problem. However, just wrapping function definitions around pieces of your main application code does not necessarily result in well designed and usable functions and if you have data and code that is related and is always used together, you should be using a class instead of functions.

Link to comment
Share on other sites

This is my index.php

<?php

 

include_once("init_board.php");

include_once("board.php");

include_once("form.php");

init_board();

show_board();

show_form();

 

?>

 

and this is my board.php

 

<!-- Game Board -->

 

<?php

include_once("variable.php");

global $board_value;

function show_board ()

{

global $board_value;

 

echo "<table border='1' bordercolor='#CCCCCC' id='board'>";

echo "<tr>";

    echo "<td id='td0'><center>".$board_value['0']."</center></td>";

        echo "<td id='td1'><center> ".$board_value['1']."</center></td>";

        echo "<td id='td2'><center> ".$board_value['2']."</center></td>";

        echo "<td id='td3'><center>".$board_value['3']."</center></td>";

        echo "<td id='td4'><center>".$board_value['4']."</center></td>";

    echo "</tr>";

   

    echo "<tr>";

    echo "<td id='td5'><center>".$board_value['5']."</center></td>";

        echo "<td id='td6'><center>".$board_value['6']."</center></td>";

        echo "<td id='td7'><center> ".$board_value['7']."</center></td>";

        echo "<td id='td8'> <center>".$board_value['8']."</center></td>";

        echo "<td id='td9'> <center>".$board_value['9']."</center></td>";

    echo "</tr>";

   

    echo "<tr>";

    echo "<td id='td10'> <center>".$board_value['10']."</center></td>";

        echo "<td id='td11'> <center>".$board_value['11']."</center></td>";

        echo "<td id='td12'> <center>".$board_value['12']."</center></td>";

        echo "<td id='td13'><center> ".$board_value['13']."</center></td>";

        echo "<td id='td14'> <center>".$board_value['14']."</center></td>";

    echo "</tr>";

   

    echo "<tr>";

    echo "<td id='td15'><center> ".$board_value['15']."</center></td>";

        echo "<td id='td16'> <center>".$board_value['16']."</center></td>";

        echo "<td id='td17'><center> ".$board_value['17']."</center></td>";

        echo "<td id='td18'> <center>".$board_value['18']."</center></td>";

        echo "<td id='td19'> <center>".$board_value['19']."</center></td>";

    echo "</tr>";

   

    echo "<tr>";

    echo "<td id='td20'> <center>".$board_value['20']."</center></td>";

        echo "<td id='td21'> <center>".$board_value['21']."</center></td>";

        echo "<td id='td22'><center> ".$board_value['22']."</center></td>";

        echo "<td id='td23'> <center>".$board_value['23']."</center></td>";

        echo "<td id='td24'> <center>".$board_value['24']."</center></td>";

    echo "</tr>";

 

echo "</table>";

}

?>

 

This is my init_board.php

 

<?php

include_once ("variable.php");

global $board_value;

global $selected_index;

function init_board ()

{

global $board_value;

$board_value['6']="S";

$board_value['8']="S";

$board_value['16']="S";

$board_value['18']="S";

$random_array_value =array (0,1,2,3,4,5,7,9,10,11,13,14,15,17,19,20,21,22,23,24);

$selected_index=array();

$rand=array();

$count=0;

while ($count!=2)

{

$v=array_rand($random_array_value);

array_push ($rand,$v);//find random value from the array

array_push($selected_index,$v); // push the random vlue in the selected array

//echo "now pushed ".$v."<br>";

$random_array_value=array_diff($random_array_value,$rand);// delete the random selected array from the array

$count++;

}

$random_array_value=array_values($random_array_value);  // reset the indexes

for ($k=0;$k<sizeof($selected_index);$k++)

{

$board_value[$selected_index[$k]]="L"; //insert the positions of lions

}

/*

echo "Count Value is ".$count."<br>";

echo "Size of selected_index array is =".sizeof($selected_index)."<br>";

for ($i=0;$i<sizeof($rand);$i++)

{

echo "random value ".$i." is = ".$rand[$i]."<br>";

}

 

 

for ($j=0;$j<sizeof($random_array_value);$j++)

{

echo "random array value index ".$j." is = ".$random_array_value[$j]."<br>";

}*/

 

}

 

 

?>

Link to comment
Share on other sites

THis is my variable.php file

 

<?php

// Variables

$num_sheep_init=20;

$num_lion_init=2;

// Initial Array

$board_value=array(0=>'',

  1=>'',

  2=>'',

  3=>'',

  4=>'',

  5=>'',

  6=>'',

  7=>'',

  8=>'',

  9=>'',

  10=>'',

  11=>'',

  12=>'',

  13=>'',

  14=>'',

  15=>'',

  16=>'',

  17=>'',

  18=>'',

  19=>'',

  20=>'',

  21=>'',

  22=>'',

  23=>'',

  24=>'');

 

?>

 

and this is my temp.php file

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Lion and sheep</title>

<link rel="stylesheet" href="style.css" type="text/css" />

</head>

 

<body>

<?php

global $board_value;

//include_once("init_board.php");

if (!include_once("variable.php"))

{

echo "variable.php was not included !";

}

if (!include_once("board.php"))

{

echo "board.php was not included !";

}

if (!isset($board_value))

{

echo "This file does not know what us $board_value !";

}

//global $board_value;

show_board();

 

?>

</body>

</html>

 

 

Here in temp.php when i am trying to access $board_value array, it is not accessed.

Link to comment
Share on other sites

Now i tried to work with classes. But i am having error. This is my code.

 

<?php

class board

{

public $num_sheep_init=20;

public $num_lion_init=2;

public $board_value=array(0=>'',

  1=>'',

  2=>'',

  3=>'',

  4=>'',

  5=>'',

  6=>'',

  7=>'',

  8=>'',

  9=>'',

  10=>'',

  11=>'',

  12=>'',

  13=>'',

  14=>'',

  15=>'',

  16=>'',

  17=>'',

  18=>'',

  19=>'',

  20=>'',

  21=>'',

  22=>'',

  23=>'',

  24=>'');

public  $random_array_value=array (0,1,2,3,4,5,7,9,10,11,13,14,15,17,19,20,21,22,23,24);

public  $selected_index=array();

public  $rand=array();

public  $count=0;

 

function __construct () {}

function init_board ()

{

$this->$board_value['6']="S";

$this->$board_value['8']="S";

$this->$board_value['16']="S";

$this->$board_value['18']="S";

 

while ($count!=2)

{

$v=array_rand($this->$random_array_value);

array_push ($this->$rand,$v);//find random value from the array

array_push($this->$selected_index,$v); // push the random vlue in the selected array

//echo "now pushed ".$v."<br>";

$this->$random_array_value=array_diff($this->$random_array_value,$this->$rand);// delete the random selected array from the array

$this->$count++;

}

for ($k=0;$k<sizeof($this->$selected_index);$k++)

{

$this->$board_value[$this->$selected_index[$k]]="L"; //insert the positions of lions

}

}

function show_board ()

{

echo "<table border='1' bordercolor='#CCCCCC' id='board'>";

echo "<tr>";

echo "<td id='td0'><center>".$this->$board_value['0']."</center></td>";

echo "<td id='td1'><center> ".$this->$board_value['1']."</center></td>";

echo "<td id='td2'><center> ".$this->$board_value['2']."</center></td>";

echo "<td id='td3'><center>".$this->$board_value['3']."</center></td>";

echo "<td id='td4'><center>".$this->$board_value['4']."</center></td>";

echo "</tr>";

 

echo "<tr>";

echo "<td id='td5'><center>".$this->$board_value['5']."</center></td>";

echo "<td id='td6'><center>".$this->$board_value['6']."</center></td>";

echo "<td id='td7'><center> ".$this->$board_value['7']."</center></td>";

echo "<td id='td8'> <center>".$this->$board_value['8']."</center></td>";

echo "<td id='td9'> <center>".$this->$board_value['9']."</center></td>";

echo "</tr>";

 

echo "<tr>";

echo "<td id='td10'> <center>".$this->$board_value['10']."</center></td>";

echo "<td id='td11'> <center>".$this->$board_value['11']."</center></td>";

echo "<td id='td12'> <center>".$this->$board_value['12']."</center></td>";

echo "<td id='td13'><center> ".$this->$board_value['13']."</center></td>";

echo "<td id='td14'> <center>".$this->$board_value['14']."</center></td>";

echo "</tr>";

 

echo "<tr>";

echo "<td id='td15'><center> ".$this->$board_value['15']."</center></td>";

echo "<td id='td16'> <center>".$this->$board_value['16']."</center></td>";

echo "<td id='td17'><center> ".$this->$board_value['17']."</center></td>";

echo "<td id='td18'> <center>".$this->$board_value['18']."</center></td>";

echo "<td id='td19'> <center>".$this->$board_value['19']."</center></td>";

echo "</tr>";

 

echo "<tr>";

echo "<td id='td20'> <center>".$this->$board_value['20']."</center></td>";

echo "<td id='td21'> <center>".$this->$board_value['21']."</center></td>";

echo "<td id='td22'><center> ".$this->$board_value['22']."</center></td>";

echo "<td id='td23'> <center>".$this->$board_value['23']."</center></td>";

echo "<td id='td24'> <center>".$this->$board_value['24']."</center></td>";

echo "</tr>";

 

echo "</table>";

}

 

}

?>

 

and this is the index.php

 

<?php

 

include_once("init_board1.php");

/*include_once("board.php");

include_once("form.php");

init_board();

show_board();

show_form();

*/

$board=new board();

$board->init_board();

$board->show_board();

?>

 

I am having the following error ///////////////////////////////////////////

Notice: Undefined variable: board_value in C:\wamp\www\init_board1.php on line 39

 

Fatal error: Cannot access empty property in C:\wamp\www\init_board1.php on line 39

 

 

Link to comment
Share on other sites

I think u have a misconception of how include works. as I see multiple includes of the same file all over.

 

include inserts file at that position. so the included file already has access to the global variables from the main file.

 

file1.php

<?php
$var=10;
include('file2.php');
echo $var;
?>

file2.php

<?php
$var=$var+25;
?>

 

in effect file1.php becomes

<?php
$var=10;
$var=$var+25;
echo $var;
?>

 

I also don't see any $_POST/$_GET processing

Link to comment
Share on other sites

I found the answer to the above my question. that was i was using $this->$var  but the correct syntax is $this->var.

 

But unfortunately i am getting series of errors.

 

class board
{
public $num_sheep_init=20;
public $num_lion_init=2;
public static $board_value=array(0=>'',
								  1=>'',
								  2=>'',
								  3=>'',
								  4=>'',
								  5=>'',
								  6=>'',
								  7=>'',
								  8=>'',
								  9=>'',
								  10=>'',
								  11=>'',
								  12=>'',
								  13=>'',
								  14=>'',
								  15=>'',
								  16=>'',
								  17=>'',
								  18=>'',
								  19=>'',
								  20=>'',
								  21=>'',
								  22=>'',
								  23=>'',
								  24=>'');
public  static $random_array_value=array (0,1,2,3,4,5,7,9,10,11,13,14,15,17,19,20,21,22,23,24);
public  $selected_index=array();
public  $randd=array();
public  $count=0;

function __construct () {}
function init_board ()
{
	$this->board_value['6']="S";
	$this->board_value['8']="S";
	$this->board_value['16']="S";
	$this->board_value['18']="S";

	while ($this->count!=2)
	{
		$v=array_rand($this->random_array_value);

 

// ERROR is " Undefined property:board::$random_array_value"

 

 

This is the part of code.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.