开发者

Storing PHP syntax in variables

Is it possible to store PHP syntax in variables for later use and repetition like this:

$ifStart = "if(";
$ifEnd = "){ echo 'Test'; }";

$ifStart 'A' == 'B' $ifEnd;

Edit: What I'm trying to accomplish is this:

I have 3 form fields, and when the PHP script is loaded, any of the three can be set. None can be set, two, one... So I need some way to determine how many are set and what to output according to that. That's why.

Edit: Right, so I have one HTML Select and two text input fields. My script checks if those fields are set (isset) and does some code accordingly, putting information into arrays etc. What I want to do now though, is to check if the variables have been set one by one, so I can output the correct results which I have stored in arrays.

New edit: This is obviously so hard to explain. But imagine a search engine where you decide which fields you'd like to fill out and then the script checks which fields are set and loops through the array with all the results, only gathering out the elements with sub-elements corresponding to the requested search, that's what I'm trying to achieve.

Here's the array design with AGE and COUNTY selected/set in the POST (hence why there's no [city] elements:

Array

( [1] => Array ( [id] => 1 [age] => 19 [county] => 4353 )

[2] => Array
    (
        [id] => 2
        [age] => 20
        [county] => 4020
    )

[3] => Array
    (
        [id] => 3
        [age] => 30
        [county] => 4020
    )

)


Still trying开发者_如何学Go to figure out how to only select out a specific array element depending on -its- contents. For example, I have an array like this:

Array ( 1: [age][county], 2: [age][county], 3: [age], 4: [county], 5: [age][county] )

I'd then like to only select the IDs containing both age and county, in this example ID 1, 2 and 5.

Edit: It'll be similar to a SQL query: WHERE age AND county, only this is in an array


It is possible...

BUT

if you have to do it, there's definitely something wrong with your design!

[Edit after your edit] Your edit shows me that I was right. What you're trying to do, can be accomplished in a better way!

So if I understand you correctly, you want to alter your output according to which form fields have been filled/answered by the user. So far you are storing some values from the $_POST array in another array. In order to generate your output, it would be best to loop over that array and concatenate strings, depending on what has been filled.

foreach ($myArray as $formField => $value)
{
    //do something for each $formField, depending on the $value
}

If that still leaves you puzzled, comment here.


The way you wrote it, it would not work, you would need to use eval(). The use of eval() , is in most cases bad practice. That would not be the main problem though, the main problem is, that such code is hard to read, hard to debug as well as maintain and hard to document. All it all, it is bad practice and will lead to a bad solution and more problems.


One clean way (clean because it avoids eval()) to do relatively dynamic code would be to store either a function name or, after php 5.3, a function reference.

E.g. something like:

$callback = "truth_check";

$condition_result = ($a == $b);

if(is_callable($callback)){
    $callback($condition_result);
}

See a running example here: http://ideone.com/1SBYS

In your case the callback could be a result to run, e.g. "print_message_on_true_input" as some comparison function and the input could be the result of a conditional tested anywhere in your code, e.g. $print = (false || $a == $b);

Give your specific use cases, though, because 90% of the time intended behavior can be acheived much less fragily without resorting to any dynamic code at all.


Is it possible? Yes, using eval.

Should you do it? NO NO NO NO PLEASE DON'T.


No. You can use functions instead.

function my_compare($a, $b, $symbol = '==') {
  eval('$result = $a ' . $symbol . ' $b;');
  if ($result) echo 'Test';
}
// ...

my_compare('A', 'B');


Keep in mind the comments above that warn you about this idea!

But you could probably do this:

$ifStart = "if(";
$ifEnd = "){ echo 'Test'; }";

eval( "  {$ifStart} 'A' == 'B' {$ifEnd} " );


Ok, so what you need has little relation to the dynamic code involved in your original question, so I'm going to start over and propose a different approach entirely:

//INITIALIZE
//get your input variables, defaulting to null
$inputs = array('select'=>@$_REQUEST['select_name'], 'input1'=>@$_REQUEST['input_1_name'], 'input2'=>@$_REQUEST['input_2_name']);
// initialize your output variables
$out1 = $out2 = $out3 = null;

//MANIPULATIONS
//perform actions based on the presence of variables in the array
if($inputs['select'] == 'whatever'){
    $out1 = 'select was '.htmlentities($inputs['select']);
}
// .... perform more manipulations, setting output strings ...

// OUTPUT SECTION
echo $out1.$out2.$out3;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜