0

I am a bit confused about how to add an object to a string.

I am trying to add the $this->getUserId(); (which contains the userId of the user).

$fileName = "user_.$this->getUserId()".".$fileExt";
2
  • Read about string concatenation - there is nothing special with an object (or its method) Commented Apr 1, 2014 at 17:33
  • 2
    $fileName = "user_".$this->getUserId().$fileExt; Commented Apr 1, 2014 at 17:35

3 Answers 3

1
$fileName = 'user_'.$this->getUserId().'.'.$fileExt;
Sign up to request clarification or add additional context in comments.

1 Comment

No pro;) Don't forget, that string concatanation is . (dot) and usual string is '.' (string with quotes).
0

This should work:

$fileName = "user_{$this->getUserId()}.$fileExt";

It should give you:

user_1.ext

See the PHP manual on strings, more specifically the part about curly braces and 'complex' parsing.

For normal variables you can place them in double quoted strings and they will be parsed. For more complex things like expressions you need to wrap them in {} so that they will be parsed.

1 Comment

hey jd182. thank u so much. i was always confused about that; you have now cleared it up for me
0

You can concatenate the object in string using dot (.) operator

$fileName = "user_".$this->getUserId().".".$fileExt";

For more info about PHP string concatenation. You can check the PHP documentation.

http://php.net/manual/en/language.operators.string.php

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.