简体   繁体   English

在参数中连接字符串(php)

[英]Concatenating strings in arguments (php)

In Flash (AS2) you can add strings like so: 在Flash(AS2)中,您可以添加如下字符串:

variable = "a"+"b"

and the result will be aa string with the value of "ab". 结果将是一个值为“ab”的字符串。

I understand this (concatenation of strings) is done with the "." 我理解这一点(字符串的连接)是用“。”完成的。 operator in php, but I'm wondering whether it's possible to do this when you pass an argument? php中的运算符,但我想知道在传递参数时是否可以执行此操作?

Specifically, what I'm trying to do is this: 具体来说,我想要做的是:

$o = get_post_meta($id, 'locationForDay'.$i, true);

Where "get_post_meta" is a wordpress function for getting custom data attached to a blog post. 其中“get_post_meta”是一个wordpress函数,用于获取附加到博客文章的自定义数据。 (I'm trying to fetch a bunch of variables called 'locationForDay1", "...2" and so on in a loop) (我试图在一个循环中获取一堆名为'locationForDay1',“... 2”等变量的变量)

(I have tried it out and got an error, but I'm not sure whether it's based on this or other mistakes in my amateur-ish php) (我已经尝试过并得到了一个错误,但我不确定它是基于我的业余爱好者的这个或其他错误)

your following statement will work fine: 您的以下声明将正常工作:

$o = get_post_meta($id, 'locationForDay'.$i, true);

Although, if you are unsure you can always throw parenthesis around a string: 虽然,如果你不确定你总是可以在字符串周围抛出括号:

$o = get_post_meta($id, ('locationForDay'.$i), true);

Edit: It should be worth noting that it is possible to concatenate strings using a comma (,). 编辑:值得注意的是,可以使用逗号(,)连接字符串。 Therefore the following statement would NOT work: 因此下面的语句是行不通的:

$o = get_post_meta($id, 'locationForDay',$i, true);

Whereas, the above statement would call the function get_post_meta and contain 4 arguments. 然而,上面的语句将调用函数get_post_meta并包含4个参数。 In this instance it would be crucial to include the parenthesis in order to achieve your string concatenation: 在这种情况下,包含括号以实现字符串连接至关重要:

$o = get_post_meta($id, ('locationForDay',$i), true);
$o = get_post_meta($id, ('locationForDay'.$i), true);

or 要么

$o = get_post_meta($id, ('locationForDay'.$i.''), true);

or 要么

$o = get_post_meta($id, ('locationForDay',$i,''), true);

是的,您可以将变量连接到这样的函数中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM