Forum Moderators: coopster

Message Too Old, No Replies

Simple Question

         

jp_css

3:49 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



This might seem silly and simple but I have a script that grabs a string, does some formatting, and then I would like to include this string in the script that was processing it. Is this possible? After the formatting it is valid php code, but I wouldn't know any other way to do then to just write to a file and then "include" it into the script. This seems inefficient and could cause some problems.

mykel79

4:11 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



You could change the string changing script into a function and put it in a seperate php file. Then in the main script, use include() to include the function and use it. That way there is no need to pass a string between scripts.

Timotheos

4:17 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not sure but the eval function [php.net] might be useful in your case.

paybacksa

4:25 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Sounds like you need to access the string as a variable, which is a natural PHP concept.

Note that variables have "scope", which defines the places where the variable persists. A variable filled with your string in one script will appear empty when checked in another place - the basic scope of a PHP variable is inside it's own script.

Amateur coders used to declare every variable as "global" in scope, which made it easy to program, but caused all sorts of security problems.

Today variables are specifically "passed around" between scripts, using various methods, and (hopefully) with security checks all along the way.

Sounds like you would be well served with a function as suggested, but there is no need to put that function in a separate file for including... that just complicates things and introduces scope and nesting issues. Just place it inside your PHP file and call it as necessary.

If you canpost a sample code fragment it might be easier to help you out.

ergophobe

6:16 pm on Mar 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I agree with Timotheos - eval.

Make sure, though, that you do something to protect yourself against the string having system calls and shell commands in it.

Tom