By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,089 Members | 1,361 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,089 IT Pros & Developers. It's quick & easy.

Program flow question

P: n/a
A scripting newbie question... I'm trying to understand some code I found.
This script conducts a poll and writes the results to a text file. The
following statement is part of the source file. The exact code is not important
to my question so don't wrack your brain on this.

if (isset($votingstep)) {
function ShowTheStuff($item, $itemvoted, $graph_width, $graph_height) {
$hector=count($itemvoted);$totalvotes=0;$in=0;$ste pstr='';
$totalvotes=SumArray($itemvoted);
$in=0;
if ($totalvotes==0) { $totalvotes=0.0001; }
while ($in<$hector) {
$stepstr=$stepstr.stripslashes($item[$in]).':
'.(int)(($itemvoted[$in]/$totalvotes)*100).'%<br>';
$timesred=(int)((($itemvoted[$in]/$totalvotes))*$graph_width);
$stepstr=$stepstr.'<img height='.$graph_height.'
width='.$timesred.' src="lp_1.gif"><img
height='.$graph_height.' width='.($graph_width-$timesred).'
src="lp_0.gif"><br><br>';
$in++;
}
return $stepstr;
}
}

My question is this. A function is contained with an 'if' statement. How does
this work? I've never seen this before in any non-scripting language. This
function 'ShowTheStuff' is called further down in the file.

In the flow of the program as it falls thru line by line, if it meets the 'if'
condition then it hits the function, which it can't call because it doesn't have
the parameters. What's the point?

Further along in the program when the 'ShowTheStuff' function gets called, how
is it able to call it when it's another block of code (the 'if' block)? Or is
it able to call it but it must meet the 'if' condition?

This makes no sense to me. What is the flow of execution here?

Thanks for your help.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Bruce W...1 wrote:
A scripting newbie question... I'm trying to understand some code I found.
This script conducts a poll and writes the results to a text file. The
following statement is part of the source file. The exact code is not important
to my question so don't wrack your brain on this.

if (isset($votingstep)) {
function ShowTheStuff($item, $itemvoted, $graph_width, $graph_height) {
$hector=count($itemvoted);$totalvotes=0;$in=0;$ste pstr='';
$totalvotes=SumArray($itemvoted);
$in=0;
if ($totalvotes==0) { $totalvotes=0.0001; }
while ($in<$hector) {
$stepstr=$stepstr.stripslashes($item[$in]).':
'.(int)(($itemvoted[$in]/$totalvotes)*100).'%<br>';
$timesred=(int)((($itemvoted[$in]/$totalvotes))*$graph_width);
$stepstr=$stepstr.'<img height='.$graph_height.'
width='.$timesred.' src="lp_1.gif"><img
height='.$graph_height.' width='.($graph_width-$timesred).'
src="lp_0.gif"><br><br>';
$in++;
}
return $stepstr;
}
}

My question is this. A function is contained with an 'if' statement. How does
this work? I've never seen this before in any non-scripting language. This
function 'ShowTheStuff' is called further down in the file.


Aaargghh! I'm sure that's a 'really bad idea(tm)'. I'm not sure what
they were thinking of to allow that sort of thing. It would certainly
not be possible in a compiled language.

It appears that you can conditionally define functions (and classes) at
execution time allowing, for example:

$adding = $_REQUEST['adding'];

if ($adding) {
function calc($a,$b) {
return $a+$b;
}
} else {
function calc($a,$b) {
return $a-$b;
}
}

echo calc(1,4);

I really don't recommend doing it though. A function should really have
one and only one definition. It's too confusing otherwise.

Jul 17 '05 #2

P: n/a
Bruce W...1 wrote...
A scripting newbie question... I'm trying to understand some code I found. [...] if (isset($votingstep)) {
function ShowTheStuff($item, $itemvoted, $graph_width, $graph_height) { [...] return $stepstr;
}
}

My question is this. A function is contained with an 'if' statement.
How does this work?
Eh! this is fun :-)

The function declared within the if only gets defined after execution
goes through the if block.

In the flow of the program as it falls thru line by line, if it meets
the 'if' condition then it hits the function, which it can't call
because it doesn't have the parameters. What's the point?
The function does not get called when it is defined.
Before the execution gets there, there is no function named "ShowTheStuff";
after the if gets executed that function is defined.

Further along in the program when the 'ShowTheStuff' function gets
called, how is it able to call it when it's another block of code
(the 'if' block)?
Once the function gets defined, it can be called from anywhere.
The problem is if the function didn't get defined because the
if condition failed: in that case you'll get a "undefined function"
error.
Or is it able to call it but it must meet the 'if' condition?
The function must be defined before it gets called.

This makes no sense to me. What is the flow of execution here?


Must be a code obfuscation technique :-)
I think the way php works is this:
1. read all the script defining all "properly" declared functions
2. execute instructions from the top
3. if another function is found define it and continue

On step 3 one bad thing may happen:
+ the function may have been defined previously, which
will trigger an error
I made this smallish script to find out how functions
inside code blocks work:

<?php
$funs = get_defined_functions();
echo '<pre>1st pass $funs[\'user\'] '; print_r($funs['user']); echo '</pre>';

define('DEBUGGING', 'browser');

function normalway($p) {
function internal1() {} // empty function
switch (DEBUGGING) {
case 'browser':
echo $p, ' in normalway()<br />'; break;
case 'logfile':
error_log("$p in normalway()\n", 3, '/var/log/debug.log'); break;
default:
// do nothing :-)
}
}

$funs = get_defined_functions();
echo '<pre>2nd pass $funs[\'user\'] '; print_r($funs['user']); echo '</pre>';

// unusual way :-)
switch (DEBUGGING) {
case 'browser':
function unusualway($p) {
function internal2() {} // empty function
echo $p, ' in unusualway()<br />';
}
break;
case 'logfile':
function unusualway($p) {
function internal3() {} // empty function
error_log("$p in unusualway()\n", 3, '/var/log/debug.log');
}
break;
default:
function unusualway($p) {
function internal4() {} // empty function
// do nothing :-)
}
}

$funs = get_defined_functions();
echo '<pre>3rd pass $funs[\'user\'] '; print_r($funs['user']); echo '</pre>';

if (!isset($UNVAR)) {
normalway('UNVAR is unset'); // also defines internal1()
unusualway('UNVAR is unset'); // also defines internalN()
}

$funs = get_defined_functions();
echo '<pre>4th pass $funs[\'user\'] '; print_r($funs['user']); echo '</pre>';

function lastfunction() {} // another empty function
?>
Jul 17 '05 #3

P: n/a
hexkid wrote:

Bruce W...1 wrote...
A scripting newbie question... I'm trying to understand some code I found.

[...]
if (isset($votingstep)) {
function ShowTheStuff($item, $itemvoted, $graph_width, $graph_height) {

[...]
return $stepstr;
}
}

My question is this. A function is contained with an 'if' statement.
How does this work?


Eh! this is fun :-)

The function declared within the if only gets defined after execution
goes through the if block.
In the flow of the program as it falls thru line by line, if it meets
the 'if' condition then it hits the function, which it can't call
because it doesn't have the parameters. What's the point?


The function does not get called when it is defined.
Before the execution gets there, there is no function named "ShowTheStuff";
after the if gets executed that function is defined.
Further along in the program when the 'ShowTheStuff' function gets
called, how is it able to call it when it's another block of code
(the 'if' block)?


Once the function gets defined, it can be called from anywhere.
The problem is if the function didn't get defined because the
if condition failed: in that case you'll get a "undefined function"
error.
Or is it able to call it but it must meet the 'if' condition?


The function must be defined before it gets called.
This makes no sense to me. What is the flow of execution here?


Must be a code obfuscation technique :-)
I think the way php works is this:
1. read all the script defining all "properly" declared functions
2. execute instructions from the top
3. if another function is found define it and continue

On step 3 one bad thing may happen:
+ the function may have been defined previously, which
will trigger an error

I made this smallish script to find out how functions
inside code blocks work:

<?php
$funs = get_defined_functions();
echo '<pre>1st pass $funs[\'user\'] '; print_r($funs['user']); echo '</pre>';

define('DEBUGGING', 'browser');

function normalway($p) {
function internal1() {} // empty function
switch (DEBUGGING) {
case 'browser':
echo $p, ' in normalway()<br />'; break;
case 'logfile':
error_log("$p in normalway()\n", 3, '/var/log/debug.log'); break;
default:
// do nothing :-)
}
}

$funs = get_defined_functions();
echo '<pre>2nd pass $funs[\'user\'] '; print_r($funs['user']); echo '</pre>';

// unusual way :-)
switch (DEBUGGING) {
case 'browser':
function unusualway($p) {
function internal2() {} // empty function
echo $p, ' in unusualway()<br />';
}
break;
case 'logfile':
function unusualway($p) {
function internal3() {} // empty function
error_log("$p in unusualway()\n", 3, '/var/log/debug.log');
}
break;
default:
function unusualway($p) {
function internal4() {} // empty function
// do nothing :-)
}
}

$funs = get_defined_functions();
echo '<pre>3rd pass $funs[\'user\'] '; print_r($funs['user']); echo '</pre>';

if (!isset($UNVAR)) {
normalway('UNVAR is unset'); // also defines internal1()
unusualway('UNVAR is unset'); // also defines internalN()
}

$funs = get_defined_functions();
echo '<pre>4th pass $funs[\'user\'] '; print_r($funs['user']); echo '</pre>';

function lastfunction() {} // another empty function
?>

================================================== =========

You may have hit the nail on the head, i.e. not defining a function unless it is
going to be used. I'll need to study the code further to see if this is the
case.

If this is the case then would any increased speed be realized? Is this a
worthwhile practice?

Thanks.
Jul 17 '05 #4

P: n/a
Bruce W...1 wrote:
You may have hit the nail on the head, i.e. not defining a function unless it is
going to be used. I'll need to study the code further to see if this is the
case.

If this is the case then would any increased speed be realized? Is this a
worthwhile practice?


hmmm ... I don't think so.
However, I do think that in

<?php
define('DEBUGGING', 'browser');

switch (DEBUGGING) {
case 'browser':
function debug_1($x) {
echo 'DEBUG: x = [', $x, ']');
} break;
case 'log':
funtion debug_1($x) {
error_log('DEBUG: ' . $x, 3, '/var/log/debug.log');
} break;
case 'mail':
funtion debug_1($x) {
mail('admin', 'debug', 'x = ' . $x);
} break;
}

function debug_2($x) {
switch (DEBUGGING) {
case 'browser':
echo 'DEBUG: x = [', $x, ']'); break;
case 'log':
error_log('DEBUG: ' . $x, 3, '/var/log/debug.log'); break;
case 'mail':
mail('admin', 'debug', 'x = ' . $x); break;
}
}
?>

debug_1() will be faster than debug_2(), even accounting for the
processing PHP must do to define it and especially if you call
debug_1() a lot.
--
I have a spam filter working.
To mail me include "urkxvq" (with or without the quotes)
in the subject line, or your mail will be ruthlessly discarded.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.