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

Is there any sort of preprocessor functionality in PHP4? (redux)

P: n/a
Continuing from the thread of last February (2008) started here:

http://tinyurl.com/6pvbgk (Google Groups link)

Setup: I have a big tree of "if" statements (imagine the PHP code
version of a choose-your-own-adventure story) and I want to be able to
tell which end-branch I've reached. Right now, l have all the end-
branches numbered with integers, and the integer of the end branch is
returned.

Problem: If I insert a new end-branch somewhere in the middle, I have
to laboriously re-number all the end branches that come after it. As
the tree gets larger and larger, this becomes more and more of a
hassle.

The application in question is at
http://www.j-archive.com/wageringcalculator.php

In my original post I wondered if PHP had any functionality that would
allow me to have the engine auto-insert the numbering for me on a pass
of the script that would occur prior to execution. That way, I could
make the script just have some sort of ++ deal at each end branch,
rather than assigning the numbers in the script by hard-coding them
in.

The replies indicated that PHP had no such pre-parser or pre-processor
functionality. A couple of replies suggested using the __LINE__
directive but when I tried to implement that, I realized it would not
work (for reasons that I now don't clearly remember).

Returning to the problem months later, I now learn about the PHP
tokenizer. Example 2 at the tutorial posted here looks promising:

http://c7y.phparch.com/c/entry/1/art...uses_tokenizer

However, examining how the example works, it seems like it is only
capable of "switch"-like functionality and I am left unclear as to
whether or how incrementing would be performed to leave each end
branch of a big if tree with a unique integer.

I have also heard about something called "Smarty" but smarty.net
appears to be experiencing problems at the moment.

Thoughts appreciated.

Cheers,
Robert K S
Oct 21 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On Oct 21, 4:55*pm, Robert K S <rober...@gmail.comwrote:
Continuing from the thread of last February (2008) started here:

http://tinyurl.com/6pvbgk(Google Groups link)

Setup: I have a big tree of "if" statements (imagine the PHP code
version of a choose-your-own-adventure story) and I want to be able to
tell which end-branch I've reached. *Right now, l have all the end-
branches numbered with integers, and the integer of the end branch is
returned.

Problem: If I insert a new end-branch somewhere in the middle, I have
to laboriously re-number all the end branches that come after it. *As
the tree gets larger and larger, this becomes more and more of a
hassle.

The application in question is athttp://www.j-archive.com/wageringcalculator.php

In my original post I wondered if PHP had any functionality that would
allow me to have the engine auto-insert the numbering for me on a pass
of the script that would occur prior to execution. *That way, I could
make the script just have some sort of ++ deal at each end branch,
rather than assigning the numbers in the script by hard-coding them
in.

The replies indicated that PHP had no such pre-parser or pre-processor
functionality. *A couple of replies suggested using the __LINE__
directive but when I tried to implement that, I realized it would not
work (for reasons that I now don't clearly remember).

Returning to the problem months later, I now learn about the PHP
tokenizer. *Example 2 at the tutorial posted here looks promising:

http://c7y.phparch.com/c/entry/1/art...uses_tokenizer

However, examining how the example works, it seems like it is only
capable of "switch"-like functionality and I am left unclear as to
whether or how incrementing would be performed to leave each end
branch of a big if tree with a unique integer.

I have also heard about something called "Smarty" but smarty.net
appears to be experiencing problems at the moment.

Thoughts appreciated.

Cheers,
Robert K S
If I was writing this and I really needed this line number bad enough,
I would have my code look something like this (based on your saying it
is a bunch of "if" statements):

if ($this == $thatThing){$lineNumber = 010;// do some stuff here;}
if ($this == $thatOtherThing){$lineNumber = 010;// do some other stuff
here;}
if ($this == $anotherThing){$lineNumber = 010;// do another stuff
here;}

(Note I am using 010 as a placeholder here that will be changed below)

Then I would preprocess it on my end using a perl script (or php if
you wanted) to create the final script.

$lineNumber = 0;
open(FILE,"original.php");
open(OFILE,">numbered.php");
while(<FILE>)
{
$line = $_;
$line =~ s/010/$lineNumber/gi;
print OFILE $line;
$lineNumber++;
}
close(FILE);
close(OFILE);

when done the code in numbered.php would look like:

if ($this == $thatThing){$lineNumber = 0;// do some stuff here;}
if ($this == $thatOtherThing){$lineNumber = 1;// do some other stuff
here;}
if ($this == $anotherThing){$lineNumber = 2;// do another stuff here;}

This way, if you need to look at the code based on something happening
when $lineNumber = 1 you would know what "if" it was in. Then you
would just have to go to your original and find the same code, make
changes, preprocess it to get new numbered code to use.

This is a simple, brute force approach to doing it, just be sure that
what you use as your placeholder is not used anywhere else in real
code, else it will be changed to the current line number also.
Bill H
Oct 21 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.