Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating strings in arguments (php)

In Flash (AS2) you can add strings like so:

variable = "a"+"b"

and the result will be a a string with the value of "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?

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. (I'm trying to fetch a bunch of variables called 'locationForDay1", "...2" and so on in a loop)

(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)

like image 334
Marcus Avatar asked Dec 16 '22 16:12

Marcus


1 Answers

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. 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);
like image 128
Samuel Cook Avatar answered Dec 18 '22 06:12

Samuel Cook