Forum Moderators: coopster
$last = substr [ca.php.net]("12345", -1);
$last = $str{strlen($str)-1};
Isn't that so much simpler than substr()? And yes, I'm joking (about it being simpler, it *is* from the manual section on strings [php.net]).
$string = '12345';How disappointing, eh?
$number = 12345;
print $string{strlen($string)-1} . '<br />';
// This won't work, $number is not a string:
print $number{strlen($number)-1} . '<br />';
// Now cast it as a string and it will:
$number = (string)$number;
print $number{strlen($number)-1} . '<br />';
Exactly. I thought the same, my friend.
substr()works this way, why wouldn't
strlen()? On the same exact page that you linked you will find the following statement...
String conversion is automatically done in the scope of an expression for you where a string is needed. This happens when you use theecho()orprint()functions, or when you compare a variable value to a string.
Hmmm? You don't say!
string substr [php.net] ( string string, int start [, int length])
int strlen [php.net] ( string string)
What is so different here? Nothing. Both functions expect string arguments and PHP should supposedly perform the conversion, but as the previous code shows, it won't do so on the latter function as it does with
substr().
Alas, yet another unsolved mystery.
$string = "Definitely a string";
$string{5}
is perfectly legit and returns the 5th character, namely "n".
The problem in your example comes in later.
$number = 12345;
If I don't cast $number as a string, PHP apparently does not understand that when I say $number{3} that I want to use string. Based on the line you quote, that's not so mysterious because the string access by character is neither an expression or a function call that expects a string as an argument.
So the following code
$num = 34567;
echo "<br>string length is " . strlen($num) . "<br>";
// outputs: string length is 5
$last_char_idx = strlen($str) - 1;
echo "The last char in \$num is " . $num{$last_char_idx} . "<br>";
// outputs: The last char in $num is
$last_char = $num{$last_char_idx};
var_dump($last_char);
// outputs: null
$str = (string) $num;
echo "<br>Now the last char in \$str is " . $str{strlen($num)-1} . "<br>";
// outputs Now the last char in $str is 7
Note that in the last line, we're still using the integer $num in the strlen() expression.
Tom
I should have read more clearly...
Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string in curly braces
strings! not integers. Lesson learned. Thanks ergophobe for the collaboration. We sure hammered bobnew32's thread here. ;)
I'm used to worrying about the opposite problem because this situation (strings that you want treated as integers) is the one that I come across the most (post, get and mysql queries). Coopster gives a good heads up on being careful with your script to watch for the opposite situation.
Anyway, I think everyone is agreed that the first solution (from Jatar_K) is the one that we would all use. I was just amused by the alternative solution and actually a bit surprised that you can't just do "$string{-1}", but I bet it's coming in PHP 5.4 or so.
A friend says I'm like a terrier - won't let go of a question until I have chewed it over pretty well. Coopster is a pit bull! (that's an unqualified compliment by the way, without negative connotation).
Tom