user2442178
user2442178

Reputation: 101

php Adding numbers in a variable and a string

I want to be able to add a number from a variable and from a string together. The variable $userinfo->pilotid will always be different depending on a User ID, for example it maybe 1.

I'd like to introduce an offset to this variable, by always adding 1000 to that number. How can I add the both with this?

Auth::$userinfo->code . '' . Auth::$userinfo->pilotid

Something along the lines of...

$offset = '1000';
echo Auth::$userinfo->code . '' . Auth::$userinfo->pilotid + $offset

Would the above work or am I mixing up things?

Upvotes: 0

Views: 383

Answers (2)

AlienHoboken
AlienHoboken

Reputation: 2800

When concatenating, you don't need to include the '', and you should wrap the addition operation in parenthesis.

$offset = 1000;
echo Auth::$userinfo->code . (Auth::$userinfo->pilotid + $offset);

The '' was superfluous as it added nothing. PHP has type juggling, so you can concatenate an integer directly onto a string. You just need to perform the addition separately, ergo the parenthesis.

Upvotes: 0

jh314
jh314

Reputation: 27802

You can use parenthesis so that you add the offset rather than concatenating the offset:

echo Auth::$userinfo->code . '' . (Auth::$userinfo->pilotid + $offset);

Upvotes: 2

Related Questions