我是否允许将单引号,双引号heredoc语法或nowdoc语法字符串直接放入使用字符串的函数参数中?

时间:2022-03-05 22:30:14

Am I allowed to put single quoted, double quoted heredoc syntax or nowdoc syntax strings directly into functions whose parameters require a string like for example strlen('string text') or strlen("some more string text") instead of including a variable for example strlen($str);?

我允许将单引号,双引号heredoc语法或nowdoc语法字符串直接放入其参数需要字符串的函数中,例如strlen('string text')或strlen(“some more string text”)而不是包含变量for示例strlen($ str);?

If not why?

如果不是为什么?

3 个解决方案

#1


3  

You are allowed to do that unless function expects string variable to be passed by reference:

除非函数需要通过引用传递字符串变量,否则您可以这样做:

// '&' means that argument is passed by reference
function requestStringAsVariable(&$str) {
    $str = '*' . $str . '*';
}

$str = 'test';
requestStringAsVariable($str);
echo $str; // outputs '*test*';

requestStringAsVariable('foo'); // won't work, as function expects variable

#2


1  

Yes, you can use any syntax for creating strings.

是的,您可以使用任何语法来创建字符串。

Note, however, that you have to be careful when using heredoc/nowdoc syntax with function calls: the final line of the string can't contain anything except the identifier:

但是请注意,在函数调用中使用heredoc / nowdoc语法时必须小心:字符串的最后一行除了标识符之外不能包含任何内容:

var_dump(<<<HERE
foo
HERE
);

#3


0  

Yes. You can. You do not need to store it in a variable

是。您可以。您不需要将其存储在变量中

#1


3  

You are allowed to do that unless function expects string variable to be passed by reference:

除非函数需要通过引用传递字符串变量,否则您可以这样做:

// '&' means that argument is passed by reference
function requestStringAsVariable(&$str) {
    $str = '*' . $str . '*';
}

$str = 'test';
requestStringAsVariable($str);
echo $str; // outputs '*test*';

requestStringAsVariable('foo'); // won't work, as function expects variable

#2


1  

Yes, you can use any syntax for creating strings.

是的,您可以使用任何语法来创建字符串。

Note, however, that you have to be careful when using heredoc/nowdoc syntax with function calls: the final line of the string can't contain anything except the identifier:

但是请注意,在函数调用中使用heredoc / nowdoc语法时必须小心:字符串的最后一行除了标识符之外不能包含任何内容:

var_dump(<<<HERE
foo
HERE
);

#3


0  

Yes. You can. You do not need to store it in a variable

是。您可以。您不需要将其存储在变量中