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”.

Read More

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)

Related posts

Leave a Reply

3 comments

  1. 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);
    
  2. $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);