简体   繁体   English

我可以在php中为传递的函数参数添加变量值吗?

[英]Can I add a variable value to a passed function parameter in php?

I have the following variable: 我有以下变量:

$argument = 'blue widget';

Which I pass in the following function: 我通过以下函数传递:

widgets($argument);

The widgets function has two variables in it: 小部件函数有两个变量:

$price = '5';
$demand ='low';

My questions is how can I do the following: 我的问题是如何做到以下几点:

 $argument = 'blue widget'.$price.' a bunch of other text';
 widgets($argument);
 //now have function output argument with the $price variable inserted where I wanted.
  • I don't want to pass $price to the function 我不想将$ price传递给该函数
  • price is made available once inside the function 价格在功能内部可用

Is there any sound way I can do this or do I need to rethink my design? 有什么声音可以做到这一点,还是我需要重新考虑我的设计?

Off the top of my head, there are two ways to do this: 在我的头顶,有两种方法可以做到这一点:

  1. Pass in two arguments 传递两个参数

     widget($initText, $finalText) { echo $initText . $price . $finalText; } 
  2. Use a placeholder 使用占位符

     $placeholder = "blue widget {price} a bunch of other text"; widget($placeholder); function widget($placeholder) { echo str_replace('{price}',$price,$placeholder); } // within the function, use str_replace 

Here's an example: http://codepad.org/Tme2Blu8 这是一个例子: http//codepad.org/Tme2Blu8

Use some sort of placeholder, then replace it within your function: 使用某种占位符,然后在函数中替换它:

widgets('blue widget ##price## a bunch of other text');

function widgets($argument) {
    $price = '5';
    $demand = 'low';

    $argument = str_replace('##price##', $price, $argument);
}

See it here in action: http://viper-7.com/zlXXkN 请在此处查看: http//viper-7.com/zlXXkN

Create a placeholder for your variables like this: 为您的变量创建一个占位符,如下所示:

$argument = 'blue widget :price a bunch of other text';

in your widget() function, use a dictionary array and str_replace() to get your result string: 在你的widget()函数中,使用字典数组和str_replace()来获取结果字符串:

function widgets($argument) {
  $dict = array(
    ':price'  => '20',
    ':demand' => 'low',
  );
  $argument = str_replace(array_keys($dict), array_values($dict), $argument);
}

I would encourage preg_replace_callback . 我会鼓励preg_replace_callback By using this method, we can easily use the captured values as a lookup to determine what their replacement should be. 通过使用此方法,我们可以轻松地将捕获的值用作查找,以确定其替换应该是什么。 If we come across an invalid key, perhaps the cause of a typo, we can respond to this as well. 如果我们遇到一个无效的密钥,也许是拼写错误的原因,我们也可以对此作出回应。

// This will be called for every match ( $m represents the match )
function replacer ( $m ) {
    // Construct our array of replacements
    $data = array( "price" => 5, "demand" => "low" );
    // Return the proper value, or indicate key was invalid
    return isset( $data[ $m[1] ] ) ? $data[ $m[1] ] : "{invalid key}" ;
}

// Our main widget function which takes a string with placeholders
function widget ( $arguments ) {
    // Performs a lookup on anything between { and }
    echo preg_replace_callback( "/{(.+?)}/", 'replacer', $arguments );
}

// The price is 5 and {invalid key} demand is low.
widget( "The price is {price} and {nothing} demand is {demand}." );

Demo: http://codepad.org/9HvmQA6T 演示: http//codepad.org/9HvmQA6T

Yes, you can. 是的你可以。 Use global inside your function. 在函数中使用全局。

$global_var = 'a';
foo($global_var);

function foo($var){
    global $global_var;

    $global_var = 'some modifications'.$var;
}

Consider changing the argument and then returning it from your widget function rather than simply changing it within the function. 考虑更改参数,然后从widget函数返回它,而不是简单地在函数内更改它。 It will be more clear to people reading your code that $argument is being modified without having to read the function as well. 对于阅读代码的人来说,更清楚的是$ argument被修改而不必阅读该函数。

$argument = widget($argument);

function widget($argument) {
    // get $price;
    return $argument . $price;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在PHP中,如何获取函数调用中传入的变量名? - In PHP, how can I get the variable name that passed in in a function call? 如何设置传递给函数的变量的值? - How can I set the value of a variable passed into a function? 如何使用传递给JavaScript函数的输入参数值到此JS函数打开的弹出窗口中? - How can I use the input parameter value passed to a JavaScript function into the popup opened by this JS function? 在PHP中:我如何使用include()函数哪个参数是可变的? - In PHP :how can I use include() Function which parameter is variable? 我无法在函数中获取配置变量值作为参数 - I can not get config variable value in a function as a parameter 我如何将参数作为动态数组添加到php中的函数 - how can I add a parameter as dynamic array to the function in php PHP function 变量作为参数的默认值 - PHP function with variable as default value for a parameter 向函数中的传递参数分配值 - assigning a value to a passed parameter in a function 如何使用传递的参数数据在 Ajax 代码中添加查询? - How can I add a query in Ajax code with the parameter data passed? Laravel,将 php 变量传递给 JS function 显示为键而非值 - Laravel, passed php variable to JS function showing as Key not Value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM