cuprasteve Posted August 31, 2006 Share Posted August 31, 2006 HiIf 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?thankssteve Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/ Share on other sites More sharing options...
Orio Posted August 31, 2006 Share Posted August 31, 2006 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]<?phpinclude("page1.php");echo $var;?>[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83407 Share on other sites More sharing options...
cuprasteve Posted August 31, 2006 Author Share Posted August 31, 2006 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 Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83413 Share on other sites More sharing options...
cuprasteve Posted August 31, 2006 Author Share Posted August 31, 2006 [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]<?phpinclude("page1.php");echo $var;?>[/code]Orio.[/quote]that didnt seem to work for me, any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83466 Share on other sites More sharing options...
cuprasteve Posted August 31, 2006 Author Share Posted August 31, 2006 even better if someone knows how to make it some form of global variable that can be read anywhere? is that poss? Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83485 Share on other sites More sharing options...
Jenk Posted August 31, 2006 Share Posted August 31, 2006 Not without accessing page1 in some form or another, no it's not. Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83489 Share on other sites More sharing options...
Clarisse Posted August 31, 2006 Share Posted August 31, 2006 [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]<?phpinclude("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. Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83493 Share on other sites More sharing options...
Jenk Posted August 31, 2006 Share Posted August 31, 2006 Easiest way to get your head around include's is to picture them as cut-and-pastes.page2 includes page1it achieves exactly the same functionality as if you were to cut and paste the contents of page1 into page2. (bar open and close tags) Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83504 Share on other sites More sharing options...
coldkill Posted August 31, 2006 Share Posted August 31, 2006 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]<?PHPinclude ( '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 Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83514 Share on other sites More sharing options...
cuprasteve Posted August 31, 2006 Author Share Posted August 31, 2006 well im doing the include to the previous page which has $globtest = "test";then on the new page i output and nothing appears Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83521 Share on other sites More sharing options...
cuprasteve Posted August 31, 2006 Author Share Posted August 31, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83523 Share on other sites More sharing options...
zero118 Posted August 31, 2006 Share Posted August 31, 2006 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.htmlThis 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. Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83526 Share on other sites More sharing options...
cuprasteve Posted August 31, 2006 Author Share Posted August 31, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83531 Share on other sites More sharing options...
cuprasteve Posted August 31, 2006 Author Share Posted August 31, 2006 nevermind its working now, it was the positioning of the $var in the included page :) thanks for all your excellent help Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83535 Share on other sites More sharing options...
cuprasteve Posted August 31, 2006 Author Share Posted August 31, 2006 new problem, anyone know how to get it to read a variable set inside a Function from the previous page? Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83599 Share on other sites More sharing options...
wildteen88 Posted August 31, 2006 Share Posted August 31, 2006 make the variable global or return itExamples:[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] Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83600 Share on other sites More sharing options...
cuprasteve Posted August 31, 2006 Author Share Posted August 31, 2006 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 $varBut i get nothing. As you can see iv added it to the Global list Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83605 Share on other sites More sharing options...
wildteen88 Posted August 31, 2006 Share Posted August 31, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83613 Share on other sites More sharing options...
sasa Posted August 31, 2006 Share Posted August 31, 2006 call function before echo, thet set variable Quote Link to comment https://forums.phpfreaks.com/topic/19252-php-and-variables-help/#findComment-83649 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.