Using eval with invalid code

Hello all,
I wonder if anyone might have an answer to this . . .
I’m writing some code which has blocks which are repeated hundreds of times, The user (i.e.me) manually enters ‘tags’ in the program code. To make things clearer and easier to use, I’ve ‘split’ the PHP code so that it’s super easy to enter the tags in notepad.
Everything works fine . . . but . . .
I’d like to be able to ‘call’ some bits of the code (as it’s used so many times) but I can’t use eval() because the code has been split andso is PHP invalid. May thatnks for any ideas!
This code should make clear what I’m tryning to say

<?php

// beginning of very often repeated code block . . .

// this is the first (invalid PHP) line I need to be able to repeatedly call
$quite_long_and_very_often_repeated_string="

apple orange banana


";echo($quite_long_and_very_often_repeated_string);
// this is the second (invalid PHP) line I need to be able to repeatedly call

// end of block

?>

Sounds like you need to use a data-driven design, where you have a data structure (array, database table) that you then loop over using some general-purpose code to produce the result. Using arrays, rather than discrete variables might help as well.

Sounds like you need to be using user-written function(s).

Nope. Contains no useful information demonstrating what you are doing, what these tags are or how they are being used, and what result you are trying to produce.

Thanks for the reply,
Yes it’s a bit difficult to explain. And very non-standard.
I’d like to use some method to ‘call’ the very frequently used lines.
Unfortunaelty, I can’t put them in a function, because they’re not valid lines of PHP code.

As the saying goes, a working example of your code is worth a thousand words (the actual saying is a “picture is worth a thousand words”, but a picture of code is worthless since it cannot be executed, modified, and posted in order to help with it.) Post a working example or two showing what you are doing.

Again, this means nothing without a working example or two showing what you mean by ‘calling’ a string assignment statement and an echo statement.

Want to bet? The following is a ‘tricky’ example of dynamically building a usort call-back function at runtime based on inputs given at the time of the call. Perhaps using an indirect/dynamic technique like this might solve whatever problem you are having, if you would only post a working example or two showing what you are actually doing.

function build_sorter($key, $direction = 'asc') {
    return function ($a, $b) use ($key, $direction) {
		if($direction == 'asc')
		{
			return strnatcmp($a[$key], $b[$key]);
		} else {
			return strnatcmp($b[$key], $a[$key]);
		}
    };
}

Also, treating the strings as an instance of a class/object, with methods and properties may be of help.

Thanks again for the reply.
I knew I’d have trouble explaining this.
If you run the example block of code which I posted, you’ll find it’ll run without errors. Here it is again with the comments removed for clarity . . .

$quite_long_and_very_often_repeated_string="

apple orange banana

";echo($quite_long_and_very_often_repeated_string);

As you can see, it’s formatted in a non-standard way. It’s done that way so that it’s very easy and clear for coders to put in different ‘tag-words’ without creating accidental errors. This block of code (or actually, in reality, a more complicated version of it) is repeated hundreds of times in my prog. but with thousands of different ‘tag-words’.
Thus, for example, the first line appears, repeated exactly, many hundreds of times. If it was a ‘normal’ parseable, valid line of PHP I could simply put it into a function (say f1() ) and then call that function each time. The program would then be much smaller and clearer.
However . . . I can’t do that because the line is not valid PHP code. If I put it in a function and call it, it will cause a PHP error.
The same goes for the PHP eval() function. As the manual says, the code evaluated has to be valid and end with a semicolon.
I don’t know PHP well enough to know whether there’s a way of remotely ‘calling’ some code that’s invalid. I suspect not. But if there is a way, I could make my program much more elegant, smaller, and hopefully faster.

The php code you are posting IS valid php code. You said so yourself -

The only issue with the posted php code is that echo is NOT a function and the () you have around the variable are unnecessary clutter.

I give up.

errrr, , , I didn’t say the code was invalid . . .
I said THE LINE was invalid.
D’ya see? There’s a difference.
Re “I give up”
Yes, that’s prob. a good idea.
Maybe try gardening instead?
Might help with your attitude problem?

There’s no way to partially evaluate code, or to evaluate partial code.

function thingWeDoALot($tags)
{
    echo $tags ;
}

thingWeDoALot("

    apple orange banana

");

That will have the same effect as your example, and still only have to change the apple orange banana part. Would that help? If not, can you give us a more concrete example of what you’re trying to do?

Thanks Skawid . . .
That was the info I needed !
I did think of ‘reversing’ the problem as you suggested, by putting the tags into a function . . . but I think I left it too late (big prog) and now I’ve painted meself into a corner. D’oh!
Thanks again anyway.

It’s never too late! Try having a play around with a few PHP focused IDE such as PHPStorm. Their refactoring capabilities have gotten pretty decent, you might find it can suggest and automatically do the change for you.

Exchanging one defining/calling syntax for another, repeated 100’s of times, won’t gain anything. Literally doing this conversion, even if automated, will (likely) use more memory (php won’t be able to use references to variables) and will run slower (the overhead of a function call just to call another php function.) You will still end up with a system where you have 100’s of things that you are manually managing, it will just be in a different format.

This brought to mind how some of the early php applications worked, by going through and brute-force including every file in a number of folders, even if the code in those files wasn’t used for anything on the current page. This caused initial 2+ second page loading times. Are you defining all these 100’s of things in one file, even though you are only using some of them on a page? If so, you need to switch to only getting/producing the content you need for a page, not producing everything, and only using some of the result. This will save on parsing time, memory, and execution time.

Sponsor our Newsletter | Privacy Policy | Terms of Service