0

I've just started self-study of PHP. I could not understand the following question:

 The Output for this
 $eur = "5euros";
 $e = 10;
 $e += $eur;
 echo ($e);

which is 15. Why?

I suppose "5euros" is a string and 10 is an integer. So I wrote the answer like 105euros. That's wrong. If $eur=5, it would be much easy to understand. How could this 5euros plus 10 proceeded?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
daw
  • 17
  • 1
  • 1
    for concat use `.` instead of plus. this is not javascript – Muhammad Raheel Aug 20 '14 at 07:12
  • did you check the PHP documentation on the plus operator? – John Dvorak Aug 20 '14 at 07:12
  • 2
    http://php.net/manual/en/language.types.string.php#language.types.string.conversion – billyonecan Aug 20 '14 at 07:13
  • Plus operator does not concatenate, it is always trying to do a math operation, so it's int parsing the strings – Royal Bg Aug 20 '14 at 07:13
  • Here's an excellent list of all [PHP operators](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php?rq=1) on StackOverflow. I'm not suggesting you read through them all as you're just getting into PHP but it's a good reference. :) – ljacqu Aug 20 '14 at 07:14

3 Answers3

6

+ is an arithmetic operator in PHP (basically, there's an exception when both operands are arrays, see below). That means, it will implicitly cast operands to numeric type if they are not numeric. Thus, your string will be cast to numeric, getting 5 in the result.

Getting deeper, you may check the implementation of +:

if (!converted) {
   zendi_convert_scalar_to_number(op1, op1_copy, result);
   zendi_convert_scalar_to_number(op2, op2_copy, result);
   converted = 1;
} else {
   zend_error(E_ERROR, "Unsupported operand types");
   return FAILURE; /* unknown datatype */
}

This part of code stands for case. When operands are not numeric data type, they will be converted to numerics.

Side note: + is also available for arrays (where it will mean hash-table merging):

case TYPE_PAIR(IS_ARRAY, IS_ARRAY): {
   zval *tmp;

   if ((result == op1) && (result == op2)) {
      /* $a += $a */
      return SUCCESS;
   }
   if (result != op1) {
      *result = *op1;
      zval_copy_ctor(result);
   }
   zend_hash_merge(Z_ARRVAL_P(result), Z_ARRVAL_P(op2), (void (*)(void *pData)) zval_add_ref, (void *) &tmp, sizeof(zval *), 0);
   return SUCCESS;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alma Do
  • 37,009
  • 9
  • 76
  • 105
  • Ok, let's summarize the answer of this question, the variable $eur is casted to integer 5, that's why. Tks for everybody. I will read the casted to integer part a little bit. – daw Aug 20 '14 at 07:21
2

PHP uses . to concatenate strings.

 $e .= $eur;

If you use +, "5euros" is cast to integer 5, so the result is 15.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
xdazz
  • 158,678
  • 38
  • 247
  • 274
  • Yes, I read that part. Yes, it's not concatenation in this case, right? My question is how could a string plus an integer? – daw Aug 20 '14 at 07:13
  • @daw `+` is NEVER concatenating, even if you try to `'asd' + 'bla'` – Royal Bg Aug 20 '14 at 07:15
  • @daw PHP does its best to do what you're asking it to do. So if you wish to add a string and an integer PHP will cast the string into an integer. It will take the number at the beginning of the string and ignore all following characters in order to cast it into an integer. And then it will add those two integers as if you're doing a simple int+int. This is very similar to how JavaScript works too, there's scripting languages for you. Of course, adding a string and an integer wouldn't work in most other non-scripting languages. – Alternatex Aug 20 '14 at 07:43
  • Re *""5euros" is cast to integer 5"*: Isn't there some kind of warning or error detection that could catch that? – Peter Mortensen May 16 '21 at 10:33
1

The + operator simply adds and shows the output value.

If you want output you can use:

$e .= $eur;
echo ($e);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131