dongying3744 2011-03-06 19:53
浏览 41

将PHP语法存储在变量中

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 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

  • 写回答

6条回答 默认 最新

  • douxiangbiao1899 2011-03-06 19:56
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法