Jump to content


Photo

PHP and variables help!!


  • Please log in to reply
18 replies to this topic

#1 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 11:53 AM

Hi

If I have a PHP page called page1.php thats sets a variable, ie

$myvariable=1;

and i have a PHP page call page2.php that has an include(page1.php);

will it be able to know the value of the variable that is created in the other page?

If not is there a way i can get it to do this?

thanks
steve

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 31 August 2006 - 12:01 PM

page2.php will know that $myvariable=1.


You can test it:
page1.php will contain:
<?php
$var="test";
?>

page2.php will be:
<?php
include("page1.php");
echo $var;
?>


Orio.
Think you're smarty?

(Gone until 20 to November)

#3 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 12:15 PM

is there a way of doing it without the include? like make it accessible just for that variable and now have to try and access the whole page?

#4 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 01:51 PM

page2.php will know that $myvariable=1.


You can test it:
page1.php will contain:

<?php
$var="test";
?>

page2.php will be:
<?php
include("page1.php");
echo $var;
?>


Orio.



that didnt seem to work for me, any ideas?

#5 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 02:19 PM

even better if someone knows how to make it some form of global variable that can be read anywhere? is that poss?

#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 31 August 2006 - 02:24 PM

Not without accessing page1 in some form or another, no it's not.

#7 Clarisse

Clarisse
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 31 August 2006 - 02:31 PM

is there a way of doing it without the include? like make it accessible just for that variable and now have to try and access the whole page?


<?php
include("page1.php");
echo $var;
?>

I may be wrong but you aren't getting all of the information and/or variables on the entire page.  Only the variable mentioned after 'echo' *found* on "page1.php"--where the variable was defined.


#8 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 31 August 2006 - 02:47 PM

Easiest way to get your head around include's is to picture them as cut-and-pastes.

page2 includes page1

it achieves exactly the same functionality as if you were to cut and paste the contents of page1 into page2. (bar open and close tags)

#9 coldkill

coldkill
  • Members
  • PipPipPip
  • Advanced Member
  • 159 posts
  • LocationUnited Kingdom

Posted 31 August 2006 - 03:00 PM

Why don't you try using the empty or isset functions. These are designed to test if a variable is empty/exists respectively.

Try this:
<?PHP
include ( 'page1.php' );

if( ! isset( $var ) )
{
    echo ' Variable isn't set!';
}
else
{
   echo ' Variable $var is '.$var;
}
?>


Normally a variable defined in one page which is included or required into another still exists and can be used in the page which it is included into. As Jenk said above consider it like a copy and paste. All the code from page 1 - except the opening and closing tags (<?PHP ?>) - is copied and used in page 2 excatly as it appears in page 1.

Hope that helps ;)
Cold


You can learn alot from reading the manual

#10 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 03:13 PM

well im doing the include to the previous page which has

$globtest = "test";

then on the new page i output and nothing appears

#11 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 03:18 PM

I have this code in a page called album_common

if (!isset($album_root_path) || empty($album_root_path))
{
	$album_root_path = $phpbb_root_path . 'album_mod/';
}

include($album_root_path . 'album_functions.' . $phpEx);
include($album_root_path . 'album_hierarchy_functions.' . $phpEx);
include($album_root_path . 'clown_album_functions.' . $phpEx);

and then further down

$template->assign_vars(array(
	'GLOBTEST'=> $testvar,
)
);

then in album_hierarchy_functions.php which you can see it including in that previous bit of code i Have $testvar="test";

But GLOBTEST in my template outputs nothing, if i stick $testvar="test"; in the same page it works fine

#12 zero118

zero118
  • Members
  • PipPipPip
  • Advanced Member
  • 40 posts

Posted 31 August 2006 - 03:22 PM

If you want to use arrays in your site I highly suggest you read up on them..  http://www.oreilly.c...apter/ch05.html

This will explain a lot.  I hope.  Other than that I can only assume you're not including the right page or you're mispelling the variable.

#13 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 03:25 PM

the array is working fine as if i set $testvar in the same page then it pulls through the value to my template, but not if i try and set the variable in a page that is being included, so it seems, i havent mispelled anything

#14 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 03:30 PM

nevermind its working now, it was the positioning of the $var in the included page :) thanks for all your excellent help

#15 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 05:02 PM

new problem, anyone know how to get it to read a variable set inside a Function from the previous page?

#16 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 August 2006 - 05:10 PM

make the variable global or return it

Examples:

Global variable
function foobar($bar)
{
    global $foo;

    $foo = 'You passed ' . $bar . ' to foobar()';
}

foobar('Hello World');

echo $foo;
// returns 'You passed Hello World to foobar()'


Return variable
function foobar($bar)
{
    global $foo;

    return 'You passed ' . $bar . ' to foobar()';
}

echo foobar('Hello world');
// returns 'You passed Hello World to foobar()'


#17 cuprasteve

cuprasteve
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 31 August 2006 - 05:24 PM

hmm iv messed around with what i think will work from your examples but cant seem to get anywhere, let me show you.

The top of the function is as follows

function album_display_admin_index($cur = ALBUM_ROOT_CATEGORY, $level = 0, $max_level = -1, $column_offset=1)

{
global $db, $template, $phpEx, $lang, $images, $album_data, $userdata, $user_id, $cat_id, $var;
$var = "Dfg";
static $username = '';

// display 'the' level
$AH_this = isset($album_data['keys'][$cur]) ? $album_data['keys'][$cur] : ALBUM_ROOT_CATEGORY; //-1;

if (defined('IN_ADMIN'))
{
$admin_url = "admin_album_cat." .$phpEx;
$is_root = false;
}
else
{
$admin_url = "album_personal_cat_admin." .$phpEx;
$is_root = (($AH_this == ALBUM_ROOT_CATEGORY || $AH_this == 0)) ? true : false;
}

// root level
if ($AH_this == ALBUM_ROOT_CATEGORY)
{
$level = ALBUM_ROOT_CATEGORY;


you can see a few lines down where I have tried to make $var= something.

below the function I have

echo $var

But i get nothing. As you can see iv added it to the Global list

#18 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 31 August 2006 - 05:44 PM

That looks like like half of the code of the function to me. Where are echoiuing $var too. Variables can be used on the same page they are created on. they cannot be used in another file.

#19 sasa

sasa
  • Staff Alumni
  • Advanced Member
  • 2,804 posts
  • LocationHrvatska

Posted 31 August 2006 - 07:13 PM

call function before echo, thet set variable





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users