paul kendal23
paul kendal23

Reputation: 185

Add object to string in PHP

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";

Upvotes: 0

Views: 126

Answers (3)

Teddy Patriarca
Teddy Patriarca

Reputation: 109

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

Upvotes: 0

jd182
jd182

Reputation: 3260

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.

Upvotes: 0

Footniko
Footniko

Reputation: 2752

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

Upvotes: 1

Related Questions