Bri*_*ian 155 php string constants concatenation
PHP中是否有一种方法可以在不连接的情况下在字符串中包含常量?
define('MY_CONSTANT', 42);
echo "This is my constant: MY_CONSTANT";
Run Code Online (Sandbox Code Playgroud)
Pek*_*ica 136
没有.
使用字符串,PHP无法将字符串数据与常量标识符区分开来.这适用于PHP中的任何字符串格式,包括heredoc.
constant()
是一种获取常量的替代方法,但函数调用也不能在没有连接的情况下放入字符串中.
Fel*_*ing 104
是的(以某种方式;)):
define('FOO', 'bar');
$test_string = sprintf('This is a %s test string', FOO);
Run Code Online (Sandbox Code Playgroud)
这可能不是你的目标,但我认为,从技术上讲,这不是连接而是替换,并且从这个假设中,它包含一个不带连接的字符串中的常量.
rav*_*ren 55
要在字符串中使用常量,可以使用以下方法:
define( 'ANIMAL', 'turtles' );
$constant = 'constant';
echo "I like {$constant('ANIMAL')}";
Run Code Online (Sandbox Code Playgroud)
可以将任何函数名称放在变量中,并使用双引号字符串中的参数调用它.也适用于多个参数.
$fn = 'substr';
echo "I like {$fn('turtles!', 0, -1)}";
Run Code Online (Sandbox Code Playgroud)
产生
我喜欢乌龟
如果您运行的是PHP 5.3+,也可以使用匿名函数.
$escape = function ( $string ) {
return htmlspecialchars( (string) $string, ENT_QUOTES, 'utf-8' );
};
$userText = "<script>alert('xss')</script>";
echo( "You entered {$escape( $userText )}" );
Run Code Online (Sandbox Code Playgroud)
按预期生成正确转义的html.
如果到现在你的印象是函数名可以是任何可调用的,那不是这种情况,因为传递给它时返回true的数组is_callable
在字符串中使用时会导致致命错误:
class Arr
{
public static function get( $array, $key, $default = null )
{
return is_array( $array ) && array_key_exists( $key, $array )
? $array[$key]
: $default;
}
}
$fn = array( 'Arr', 'get' );
var_dump( is_callable( $fn ) ); // outputs TRUE
// following line throws Fatal error "Function name must be a string"
echo( "asd {$fn( array( 1 ), 0 )}" );
Run Code Online (Sandbox Code Playgroud)
这种做法是不明智的,但有时候会产生更易读的代码,所以这取决于你 - 可能性就在那里.
小智 21
define( 'FOO', 'bar');
$FOO = FOO;
$string = "I am too lazy to concatenate $FOO in my string";
Run Code Online (Sandbox Code Playgroud)
the*_*iko 14
define('FOO', 'bar');
$constants = create_function('$a', 'return $a;');
echo "Hello, my name is {$constants(FOO)}";
Run Code Online (Sandbox Code Playgroud)
Jur*_*nka 13
如果你真的想在没有连接的情况下回显常量,那么解决方法是:
define('MY_CONST', 300);
echo 'here: ', MY_CONST, ' is a number';
Run Code Online (Sandbox Code Playgroud)
注意:在这个例子中,echo接受了许多参数(查看逗号),因此它不是真正的连接
Echo表现为一个函数,它需要更多参数,它比串联更有效,因为它不必连接然后回显,它只是回应所有内容而无需创建新的String连接对象:))
编辑
另外,如果你考虑连接字符串,传递字符串作为参数或用",The ,(逗号版本)编写整个字符串总是最快的,接下来就是.(用'单引号连接)和最慢的字符串构建方法是使用双引号",因为必须根据声明的变量和函数来评估以这种方式编写的表达式.
Sim*_*mon 11
你可以这样做:
define( 'FOO', 'bar' );
$constants = get_defined_constants(true); // the true argument categorizes the constants
$constants = $constants[ 'user' ]; // this gets only user-defined constants
echo "Hello, my name is {$constants['FOO']}";
Run Code Online (Sandbox Code Playgroud)
正如其他人所指出的那样,你无法做到这一点.PHP有一个函数constant()
不能直接在字符串中调用,但我们可以轻松地解决这个问题.
$constant = function($cons){
return constant($cons);
};
Run Code Online (Sandbox Code Playgroud)
以及它的用法的基本例子:
define('FOO', 'Hello World!');
echo "The string says {$constant('FOO')}";
Run Code Online (Sandbox Code Playgroud)
最简单的方法是
define('MY_CONSTANT', 42);
$my_constant = MY_CONSTANT;
echo "This is my constant: $my_constant";
Run Code Online (Sandbox Code Playgroud)
另一种使用方式 (s)printf
define('MY_CONSTANT', 42);
// Note that %d is for numeric values. Use %s when constant is a string
printf('This is my constant: %d', MY_CONSTANT);
// Or if you want to use the string.
$my_string = sprintf('This is my constant: %d', MY_CONSTANT);
echo $my_string;
Run Code Online (Sandbox Code Playgroud)