Jump to content

Archived

This topic is now archived and is closed to further replies.

cuprasteve

PHP and variables help!!

Recommended Posts

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

Share this post


Link to post
Share on other sites
page2.php will know that $myvariable=1.


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

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


Orio.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote author=Orio link=topic=106371.msg425299#msg425299 date=1157025692]
page2.php will know that $myvariable=1.


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

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


Orio.
[/quote]


that didnt seem to work for me, any ideas?

Share this post


Link to post
Share on other sites
even better if someone knows how to make it some form of global variable that can be read anywhere? is that poss?

Share this post


Link to post
Share on other sites
Not without accessing page1 in some form or another, no it's not.

Share this post


Link to post
Share on other sites
[quote author=cuprasteve link=topic=106371.msg425306#msg425306 date=1157026510]
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?
[/quote]

[code]<?php
include("page1.php");
echo $var;
?>[/code]

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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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:
[code]<?PHP
include ( 'page1.php' );

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

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

Share this post


Link to post
Share on other sites
well im doing the include to the previous page which has

$globtest = "test";

then on the new page i output and nothing appears

Share this post


Link to post
Share on other sites
I have this code in a page called album_common

[code]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);[/code]

and then further down

[code]$template->assign_vars(array(
'GLOBTEST'=> $testvar,
)
);[/code]

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

Share this post


Link to post
Share on other sites
If you want to use arrays in your site I highly suggest you read up on them..  http://www.oreilly.com/catalog/progphp/chapter/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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
nevermind its working now, it was the positioning of the $var in the included page :) thanks for all your excellent help

Share this post


Link to post
Share on other sites
make the variable global or return it

Examples:

[b]Global variable[/b]
[code=php:0]function foobar($bar)
{
    global $foo;

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

foobar('Hello World');

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


[b]Return variable[/b]
[code=php:0]function foobar($bar)
{
    global $foo;

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

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

Share this post


Link to post
Share on other sites
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

[quote]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;[/quote]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
call function before echo, thet set variable

Share this post


Link to post
Share on other sites

×

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.