4

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)

3 Answers 3

7

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);
Sign up to request clarification or add additional context in comments.

Comments

3
$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);

Comments

2

Yes, you can concat variables as they are being passed into a function like that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.