0

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?

2
  • yes why not use correct concatenation that's all Commented Jul 31, 2013 at 0:03
  • It echoes 10001000. $userinfo->pilotid when printed is 1. Commented Jul 31, 2013 at 0:05

2 Answers 2

2

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

echo Auth::$userinfo->code . '' . (Auth::$userinfo->pilotid + $offset);
Sign up to request clarification or add additional context in comments.

Comments

0

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.

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.