Variables not passing into an include

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Variables not passing into an include

Post by Citizen »

One my base php file, I define a variable, $sads = 1;.

Then I include a file /ads.php

In ads.php, I echo"$sads"; and nothing comes up. I've been googling this for about a half hour and cant find any reason whe the variable wouldnt transfer.

Any ideas?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Are you defining the variable in a function?
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Nope, should i be?

Edit: here's the exact code:

Code: Select all

<?
$sads = 1;
include('newend.php') 
?>
in newend.php:

Code: Select all

echo"$sads";
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

First of all, you need a semicolon after your inlcude statement.

Also, you shouldn't be putting quotes around your variable.

Check out the man pages for each function your using and check out the examples.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

try making $sads = '1'; (notice the quotes)
mattbee
Forum Newbie
Posts: 4
Joined: Wed Nov 22, 2006 11:17 am

Post by mattbee »

Edit (missed the bleeding obvious!)

You say you want to include

/ads.php

but your code says you are including

newend.php

Check the file names first!

/Edit

As long as you are enclosing the echo statement in php tags I see no reason that it wouldn't work.

Try using

Code: Select all

<?php
instead of

Code: Select all

<?
You may not have short tags enabled.
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

New code:

Code: Select all

<?php
$sads = '1';
include('newend.php');
?>
on newend.php...

<?php
echo "$sads";
?>

With nothing showing still.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

when I mentioned not putting quotes around your variable, I meant:

Code: Select all

<?php
echo $sads;
?>
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

New code:

Code: Select all

<?php
$sads = '1';
include('newend.php');
?>
on newend.php...

Code: Select all

<?php
echo $sads;
?>
Nothing being echo'd still.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

That's... wierd.

Does this work:

Code: Select all

<?php
$sads = '1';
include('newend.php');
echo $sads;
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Run the following in a new file and tell us the results please.

Code: Select all

<?php

$neg = array('off', 0, false, '', null);
$flags = array(
	'Register Globals' => 'register_globals',
	'Short Tags' => 'short_open_tag',
	'Display Errors' => 'display_errors',
	'Magic Quotes GPC' => 'magic_quotes_gpc',
	'Magic Quotes Runtime' => 'magic_quotes_runtime',
	'Magic Quotes Sybase' => 'magic_quotes_sybase',
);
$ve = phpversion();
$os = PHP_OS;
$er = intval(error_reporting());
foreach ($flags as $n => $v)
{
	$flags[$n] = (in_array(strtolower(ini_get($v)), $neg) ? 'Off' : 'On');
}
$cli = (php_sapi_name() == 'cli');
$eol = "\n";

$gle = get_loaded_extensions();
$rows = array();
$le = '';
$wide = 4;
$j = count($gle);
$pad = $wide - $j % $wide;
$len = max(array_map('strlen', $gle));
$func = create_function('$a', 'return str_pad($a, ' . intval($len) . ');');
$gle = array_map($func, $gle);
for($i = 0; $i < $j; $i += $wide)
{
	$le .= '   ' . implode('   ', array_slice($gle, $i, $wide)) . $eol;
}

$ec = array(
	'E_STRICT' => 2048, 'E_ALL' => 2047, 'E_USER_NOTICE' => 1024,
	'E_USER_WARNING' => 512, 'E_USER_ERROR' => 256, 'E_COMPILE_WARNING' => 128,
	'E_COMPILE_ERROR' => 64, 'E_CORE_WARNING' => 32, 'E_CORE_ERROR' => 16,
	'E_NOTICE' => 8, 'E_PARSE' => 4, 'E_WARNING' => 2, 'E_ERROR' => 1,
);

$e = array();
$t = $er;
foreach ($ec as $n => $v)
{
	if (($t & $v) == $v)
	{
		$e[] = $n;
		$t ^= $v;
	}
}
if (ceil(count($ec) / 2) + 1 < count($e))
{
	$e2 = array();
	foreach ($ec as $n => $v)
	{
		if (!in_array($n, $e) and $n != 'E_ALL')
		{
			$e2[] = $n;
		}
	}
	$er = $er . ' ((E_ALL | E_STRICT) ^ ' . implode(' ^ ', $e2) . '))';
}
else
{
	$er = $er . ' (' . implode(' | ', $e) . ')';
}

if (!$cli)
{
	echo '<html><head><title>quick info</title></head><body><pre>', $eol;
}

echo 'PHP Version: ', $ve, $eol;
echo 'PHP OS: ', $os, $eol;
echo 'Error Reporting: ', $er, $eol;
foreach ($flags as $n => $v)
{
	echo $n, ': ', $v, $eol;
}
echo 'Loaded Extensions:', $eol, $le, $eol;

if (!$cli)
{
	echo '</pre></body></html>', $eol;
}

?>
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Grim... wrote:That's... wierd.

Does this work:

Code: Select all

<?php
$sads = '1';
include('newend.php');
echo $sads;
?>
Yup, that works.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Run the script feyd posted. That tells us a lot. Then run this...

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);

$sads = 1;

require_once 'newend.php';
?>
newend.php

Code: Select all

<?php
if (isset($sads))
{
    echo $sads;
}
else
{
    die('I am dying inside of the include!');
}
?>
Citizen
Forum Contributor
Posts: 300
Joined: Wed Jul 20, 2005 10:23 am

Post by Citizen »

Results from feyd's
PHP Version: 5.1.4
PHP OS: Linux
Error Reporting: 2039 ((E_ALL | E_STRICT) ^ E_STRICT ^ E_NOTICE))
Register Globals: Off
Short Tags: On
Display Errors: On
Magic Quotes GPC: On
Magic Quotes Runtime: Off
Magic Quotes Sybase: Off
Loaded Extensions:
xmlwriter libxml dom xmlreader
xml tokenizer standard SimpleXML
SPL session sockets PDO
SQLite Reflection posix mysqli
mysql iconv hash gd
ftp date ctype calendar
bcmath zlib pcre apache
Zend Optimizer

I'll test that other page now.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Citizen wrote:Edit: here's the exact code:

Code: Select all

<?
$sads = 1;
include('newend.php') 
?>
in newend.php:

Code: Select all

echo"$sads";
Let's make it a littlebit more interesting

Code: Select all

<?php // main.php
error_reporting(E_ALL);
ini_set('display_errors', true);
$sads = 1;

echo '<div>file: ', __FILE__, "</div>\n";
require 'newend.php';
echo '<hr /><div>sads: ', $sads, "</div>\n";
?>

Code: Select all

<?php // newend.php
echo '<div>file: ', __FILE__, "</div>\n";
echo '<pre>callstack: '; debug_print_backtrace(); echo "</pre>\n";
echo "<pre>defined vars: \n";
$v = get_defined_vars();
ksort($v, SORT_STRING);
foreach($v as $name=>$var) {
	echo $name, '=>', gettype($var), "\n";
}
echo "</pre>\n";

echo '<hr /><div>sads: ', $sads, "</div>\n";
?>
what does that print?
Post Reply