1

I am stuck with the issue as currently the result is quite unexpected. I am calculating a hashed keyword length and it is surely giving me an unexpected result.

echo strlen("$2a$08$MphfRBNtQMLuNro5HOtw3Ovu20cLgC0VKjt6w7zrKXfj1bv8tNnNa");

Output - 6

Let me know the reason for this and why it is outputting 6 as a result.

Codepad Link - http://codepad.org/pLARBx6F

4
  • 2
    Your string is double-quoted. Commented Jul 1, 2014 at 11:54
  • @cHao ok..got it but how 6 ? the result Commented Jul 1, 2014 at 11:54
  • try this :echo strlen('$2a$08$MphfRBNtQMLuNro5HOtw3Ovu20cLgC0VKjt6w7zrKXfj1bv8tNnNa'); it takes parameter to a function as php variable and throws notice as variable not defined Commented Jul 1, 2014 at 11:56
  • 2
    echo or var_dump your string. The reason should become clearer. Commented Jul 1, 2014 at 11:57

3 Answers 3

5

You must use single quotes '. With the double quotes ("), due to the $ in your string, parts of it get interpreted as variables.

Generally, it's not a bad idea to get accustomed to using single quotes unless you specifically need doubles.

Look at the "variables" contained here. They would be $2a, $08, and $MphfRBNtQM...... The first two couldn't be variables as they start with a number, thus, the 6 characters. The third one indeed could be a proper variable, but since it isn't set, it's empty.

Sign up to request clarification or add additional context in comments.

2 Comments

Look at the "variables" contained here. They would be $2a, $08, and $MphfRBNtQM...... The first two couldn't be variables as they start with a number, thus, the 6 characters. The third one indeed could be a proper variable, but since it isn't set, it's empty.
Please add this comment to your answer and I wil accept it..awesome :)
1

Use the below code to calculate the string length -

echo strlen('$2a$08$MphfRBNtQMLuNro5HOtw3Ovu20cLgC0VKjt6w7zrKXfj1bv8tNnNa');

You need to use single quotes, as at the third occurrence of the symbol $, a alphabet is starting after it and it get treated as a new variable. So before this third occurrence of $ only 6 character were there and you were getting string length as 6

Comments

-2

Try following

<?php

echo strlen('$2a$08$MphfRBNtQMLuNro5HOtw3Ovu20cLgC0VKjt6w7zrKXfj1bv8tNnNa');

?>

If you change your string and remove rest of '$' signs except the first one, then this will work fine because by adding $ it gets a special meaning in PHP.

2 Comments

@Negative Marker, can you please explain the reason?
Probably because you don't explain what you fixed and why. The little bit of explanation, "adding $ it gets a special meaning in PHP", makes no sense.

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.