Page 1 of 2

TRUE == FALSE ??

Posted: Mon Aug 21, 2006 9:24 am
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]

Posted: Mon Aug 21, 2006 9:31 am
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.

Posted: Mon Aug 21, 2006 9:35 am
by sweatje
Looks to me like probably your static $executionType = TRUE; is a different scope from the global $executionType; you compare to later.

Posted: Mon Aug 21, 2006 9:38 am
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.

Posted: Mon Aug 21, 2006 9:39 am
by feyd
Is this code in a class?

Posted: Mon Aug 21, 2006 9:45 am
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.

Posted: Mon Aug 21, 2006 9:45 am
by bindermichi
feyd wrote:Is this code in a class?
No, only a function library.

Posted: Mon Aug 21, 2006 9:48 am
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;
}

?>

Posted: Mon Aug 21, 2006 9:51 am
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

Posted: Mon Aug 21, 2006 9:59 am
by sweatje
Try using:

$GLOBALS['executionType'] = TRUE;

instead of
static $executionType = TRUE;

Posted: Mon Aug 21, 2006 10:26 am
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.

Posted: Mon Aug 21, 2006 10:29 am
by feyd
You definitely have a scope issue if $GLOBALS works. How and where is the posted code run?

Posted: Mon Aug 21, 2006 10:31 am
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.

Posted: Mon Aug 21, 2006 10:56 am
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:

Posted: Mon Aug 21, 2006 11:03 am
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"