473,471 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Program flow question

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
4 2293
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Albert Ahtenberg | last post by:
Hello, I had some bad experience with code organization and script functionality in writing my php based applications. And as the applications get bigger in scale it gets even worse. Therefore,...
11
by: Robert Bowen | last post by:
Hello all. I have been given mock-ups (in static HTML) of some pages for a site I am working on. The client would like these pages to look exactly as they do now. The problem is that the content is...
54
by: bnp | last post by:
Hi, I took a test on C. there was an objective question for program output type. following is the program: main() { char ch; int i =2;
34
by: kevin.watters | last post by:
Hi all, I have a need for a short program: Given a drive letter, it would recursively search through all directories, "generating" each filename that it encounters (need to pass each filename...
1
by: Brett | last post by:
I'd like to have all of my documentation in one place. I use the following for documenting code: - attributes for certain types of documentation - use of the C# generated inline XML...
8
by: lovecreatesbea... | last post by:
K&R 2, sec 2.4 says: If the variable in question is not automatic, the initialization is done once only, conceptually before the program starts executing, ... . "Non-automatic variables are...
18
by: Andrew Gentile | last post by:
Hello, I would like to find a way of using scanf() and the Enter key to have user-controlled program flow. Currently, I have a couple of lines in my program which serves as a pause in the...
6
by: Crooter | last post by:
Hello colleagues, Could anybody tell me if there are existing open-source solutions to extract the program tree using a program source code? I'm aware that GCC has program flow information and...
3
by: 100grand | last post by:
Modify the Inventory Program to use a GUI. The GUI should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.