Sum of strings in PHP and Javascript

Asked

Viewed 60 times

0

I came across this problem and conflicted, when using '+' in Javascript it "joins" and in PHP it gives me a number (Hi?), well the intention is to adapt the Javascript with PHP, see:

alert(CryptoJS.SHA1('ABCDE' + 'ABCD'.substr(0, 32)));
result: 64b9885c4ab720cabee37f0011aeb06efa27f9b3

Available here.

In PHP I run the "same" thing, but returns something different:

echo SHA1('ABCDE' + substr('ABCD', 0, 32));
result: b6589fc6ab0dc82cf12099d1c2d40ab994e8410c

Why? What is the explanation for this and how to adapt Javascript?

1 answer

4

PHP and javascript have different operators for the operation of concatenating strings.
In javascript, use "+"; in PHP, use "."

echo SHA1('ABCDE' . substr('ABCD', 0, 32));
  • the intention is to adapt JS to PHP.. and why is this happening? I need to understand so as not to make more mistakes.. Obg!

  • It’s happening because each of these languages has a different operator to work with string concatenation. It’s not a matter of fitting, one uses the "dot" and the other the "plus sign", that’s all.

  • The concatenation operators are different for each language. php the operator is . while in js is the +

Browser other questions tagged

You are not signed in. Login or sign up in order to post.