TRUE == FALSE ??

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

User avatar
bindermichi
Forum Newbie
Posts: 9
Joined: Mon Aug 21, 2006 9:17 am
Location: Germany
Contact:

TRUE == FALSE ??

Post by bindermichi »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi,

I encountered a prob that didn't popup this morning:

Code: Select all

static $executionType = TRUE;

function functionName(args)
{
[...]

//--------> Choose ExecutionType! <---------------------------------------------
  global $executionType;
  if($executionType==FALSE)
  {
    // Fake command execution for test and debugging
    $valueList = execFakeCommandLine($fakeReturnValue);
  }
  else
  {
    // Real command execution
    $valueList = execCommandLine($commandLine);
  }
//--------

[...]
}
$executionType is always FALSE... why? :?:


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Nothing in the code shows would seem to be the culprit as doing my own test works correctly. Therefore I can only assume the removed sections of code or code not even mentioned is probably at fault.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Looks to me like probably your static $executionType = TRUE; is a different scope from the global $executionType; you compare to later.
User avatar
bindermichi
Forum Newbie
Posts: 9
Joined: Mon Aug 21, 2006 9:17 am
Location: Germany
Contact:

Post by bindermichi »

That's why I'm puzzled... i didn't change anything at these passages... and I don't change the $executionType anywhere (which wouldn't even be possible if the php manual is accurate :wink: )

It's a simple ON/OFF switch for testing. If set to FALSE it will work with some dummy data and at TRUE the function will run a commandline execution...

The only way to get it to run the commandline is inserting $executionType = TRUE above the if clause and mark the global line as comment.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is this code in a class?
User avatar
bindermichi
Forum Newbie
Posts: 9
Joined: Mon Aug 21, 2006 9:17 am
Location: Germany
Contact:

Post by bindermichi »

sweatje wrote:Looks to me like probably your static $executionType = TRUE; is a different scope from the global $executionType; you compare to later.
How? I had it in the same file... moved it to an include but the behavior is still the same... and it the only variable with this name and is only declared once.
User avatar
bindermichi
Forum Newbie
Posts: 9
Joined: Mon Aug 21, 2006 9:17 am
Location: Germany
Contact:

Post by bindermichi »

feyd wrote:Is this code in a class?
No, only a function library.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If $executionType is in the global scope then I see no need for it to be static.
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;
}

?>
User avatar
bindermichi
Forum Newbie
Posts: 9
Joined: Mon Aug 21, 2006 9:17 am
Location: Germany
Contact:

Post by bindermichi »

I just ran this one on the testsystem, the production server is linux but setup on similar configuration and the return same TRUE/FALSE error.

Code: Select all

PHP Version: 5.1.4
PHP OS: WINNT
Error Reporting: 2047 (E_ALL)
Register Globals: Off
Short Tags: On
Display Errors: On
Magic Quotes GPC: On
Magic Quotes Runtime: Off
Magic Quotes Sybase: Off
Loaded Extensions:
   bcmath           calendar         com_dotnet       ctype         
   date             ftp              hash             iconv         
   odbc             pcre             Reflection       session       
   libxml           standard         tokenizer        zlib          
   SimpleXML        dom              SPL              wddx          
   xml              xmlreader        xmlwriter        apache2handler
   mbstring         curl             exif             fdf           
   gd               gettext          mcrypt           mhash         
   mime_magic       mysql            mysqli           PDO           
   pdo_mysql        pdo_sqlite       SQLite           tidy          
   xmlrpc           xsl
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Try using:

$GLOBALS['executionType'] = TRUE;

instead of
static $executionType = TRUE;
User avatar
bindermichi
Forum Newbie
Posts: 9
Joined: Mon Aug 21, 2006 9:17 am
Location: Germany
Contact:

Post by bindermichi »

sweatje wrote:Try using:

$GLOBALS['executionType'] = TRUE;

instead of
static $executionType = TRUE;
Works, but I don't really like using $GLOBALS, since they're most likely to be turned OFF in the live server.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You definitely have a scope issue if $GLOBALS works. How and where is the posted code run?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

bindermichi wrote:
sweatje wrote:Try using:

$GLOBALS['executionType'] = TRUE;

instead of
static $executionType = TRUE;
Works, but I don't really like using $GLOBALS, since they're most likely to be turned OFF in the live server.
You can't "turn off" globals, only the automatic registration of request variables. I would recommend changing your code to not use globals, but from your first post you show global $executionType;, so you are using globals.
User avatar
bindermichi
Forum Newbie
Posts: 9
Joined: Mon Aug 21, 2006 9:17 am
Location: Germany
Contact:

Post by bindermichi »

sweatje wrote: You can't "turn off" globals, only the automatic registration of request variables. I would recommend changing your code to not use globals, but from your first post you show global $executionType;, so you are using globals.
Hmm... as far as i read the manual, there's a difference between "$GLOBALS[''];" and "global $var;"
By declaring $a and $b global within the function, all references to either variable will refer to the global version.
Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal.
...and if I learned one thing in programming class... it's never to use superglobals, unless you have to :wink:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"global $foo" is identical to $GLOBALS['foo'], they're interchangable.

Code: Select all

[feyd@home]>php -r "$foo = true; function foo(){global $foo; $foo = 1234; $GLOBALS['foo'] = 'asdf'; var_dump($foo);} foo(); var_dump($foo);"
string(4) "asdf"
string(4) "asdf"
Post Reply