Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 向函数传递动态参数_Php_Arguments_User Defined Functions - Fatal编程技术网

Php 向函数传递动态参数

Php 向函数传递动态参数,php,arguments,user-defined-functions,Php,Arguments,User Defined Functions,我有一个用户定义的函数,如下所示: function char_replace($line1){ $line1= str_ireplace("Snippet:", "", $line1); // First, replace UTF-8 characters. $line1= str_replace( array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x9

我有一个用户定义的函数,如下所示:

function char_replace($line1){
    $line1= str_ireplace("Snippet:", "", $line1);
    // First, replace UTF-8 characters.
    $line1= str_replace(
    array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
    array("'", "'", '"', '"', '-', '--', '...'),
    $line1);
    // Next, replace their Windows-1252 equivalents.
    $line1= str_replace(
    array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
    array("'", "'", '"', '"', '-', '--', '...'),
    $line1);
}
我正在替换我分解的多行上的字符,除了我想对函数char\u replace应用一个动态参数,其中
$line
很可能是
$line2
$line3
,所以我将以这种方式转换字符:
$line1=char\u replace($line1)

我想让函数参数和str_replace/str_ireplace参数成为一个动态变量,我可以这样转换另一行:
$random\u line=char\u replace($random\u line)

这是可能的吗?

假设您以返回$line1结束函数您可以这样称呼它:

$line1 = char_replace($line1);
$line2 = char_replace($line2);
$line3 = char_replace($line3);

如何调用函数定义中的参数并不重要,它们是该函数的本地参数,在函数之外可以有不同的名称。

假设以
return$line1您可以这样称呼它:

$line1 = char_replace($line1);
$line2 = char_replace($line2);
$line3 = char_replace($line3);

如何调用函数定义中的参数并不重要,它们是该函数的本地参数,可以在函数之外有不同的名称。

是否只想将return语句添加到函数中:

function char_replace($line1){
    $line1= str_ireplace("Snippet:", "", $line1);
    // First, replace UTF-8 characters.
    $line1= str_replace(
    array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
    array("'", "'", '"', '"', '-', '--', '...'),
    $line1);
    // Next, replace their Windows-1252 equivalents.
    $line1= str_replace(
    array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
    array("'", "'", '"', '"', '-', '--', '...'),
    $line1);
    return $line1;
}

是否只想将return语句添加到函数中:

function char_replace($line1){
    $line1= str_ireplace("Snippet:", "", $line1);
    // First, replace UTF-8 characters.
    $line1= str_replace(
    array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
    array("'", "'", '"', '"', '-', '--', '...'),
    $line1);
    // Next, replace their Windows-1252 equivalents.
    $line1= str_replace(
    array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
    array("'", "'", '"', '"', '-', '--', '...'),
    $line1);
    return $line1;
}

如果我读对了,只需在函数中添加一个返回。因此:

function char_replace($string){
  $string= str_ireplace("Snippet:", "", $string);
  // First, replace UTF-8 characters.
  $string= str_replace(
  array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
  array("'", "'", '"', '"', '-', '--', '...'),
  $string);
  // Next, replace their Windows-1252 equivalents.
  $string= str_replace(
  array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
  array("'", "'", '"', '"', '-', '--', '...'),
  $string);

  return $string;
}

这将允许您向函数传递任何字符串,并将修改后的字符串取回。

如果我读对了,只需向函数添加一个返回。因此:

function char_replace($string){
  $string= str_ireplace("Snippet:", "", $string);
  // First, replace UTF-8 characters.
  $string= str_replace(
  array("\xe2\x80\x98", "\xe2\x80\x99", "\xe2\x80\x9c", "\xe2\x80\x9d", "\xe2\x80\x93", "\xe2\x80\x94", "\xe2\x80\xa6"),
  array("'", "'", '"', '"', '-', '--', '...'),
  $string);
  // Next, replace their Windows-1252 equivalents.
  $string= str_replace(
  array(chr(145), chr(146), chr(147), chr(148), chr(150), chr(151), chr(133)),
  array("'", "'", '"', '"', '-', '--', '...'),
  $string);

  return $string;
}

这将允许您将任何字符串传递给函数,并获取修改后的字符串。

谢谢,所有答案都是相似的,并且都有效,但出于学习目的,我想知道为什么添加
返回
会允许它是“动态的”?函数定义了运行代码的独立上下文;函数参数定义函数的输入(它们可以用作局部变量),而
return
定义输出。您可以从PHP手册中了解更多信息-嗯,我部分理解您的说法。谢谢,所有答案都很相似,而且都很有效,但出于学习目的,我想知道为什么添加
return
允许它是“动态的”?函数定义了运行代码的独立上下文;函数参数定义函数的输入(它们可以用作局部变量),而
return
定义输出。你可以从PHP手册中阅读更多关于这方面的内容——嗯,我部分理解你的意思。