Forum Moderators: coopster

Message Too Old, No Replies

Get last number

         

bobnew32

5:58 pm on Jun 23, 2004 (gmt 0)

10+ Year Member



Say I have the number 12345.

I want to be able to select the number 5 out of it as its the last number from that sequence.

jatar_k

6:03 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what about

$last = substr [ca.php.net]("12345", -1);

ergophobe

7:42 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



How about this gem from the manual


$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]).

coopster

8:28 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Actually, that won't work if $str is an integer. It's odd, but PHP won't convert it to string automagically. You would have to cast it as a string first.
$string = '12345'; 
$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 />';
How disappointing, eh?

ergophobe

8:44 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I hadn't thought about that. I assumed that once you tried to reference it as a string, it would automatically convert. Of course if the number comes from a post request or a mysql query it will be a string already.

Anyway, I always use the substr() method personally.

Tom

coopster

9:34 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I assumed that once you tried to reference it as a string, it would automatically convert.

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 the
echo()
or
print()
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.

ergophobe

9:57 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't think there's any unsolved mystery per se. I believe you make a mistake by focussing on the return value of strlen(). That is and should be an integer.

$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

coopster

10:33 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I follow you. During testing, I did the same exact thing you did here, but you are correct, I was focusing on the wrong part of the expression, strlen(), as opposed to how PHP might handle (or obviously does NOT handle) integers the same as strings UNTIL you cast them specifically as such.

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. ;)

ergophobe

11:11 pm on Jun 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So often the threads here remind me of the old adage:

Nothing is so simple that, if looked at from the right perspective, it can't be made extremely complex.

bobnew32

5:20 am on Jun 24, 2004 (gmt 0)

10+ Year Member



Woah! You guys went the extra distance on this one! I feel honored; I love this forum lol.

BTW, I needed the last number I posted about due to next previous links, to find what number in an array a number is.

PAGES: «Previous 1 2 3 Next» ¦ 1-10

It works too thx guys!

ergophobe

2:48 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



So your "numbers" are GET parameters and, therefore, strings.

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

coopster

5:01 pm on Jun 24, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe. none taken. I love the discussion. It certainly heightened my awareness of PHP (again). The more you know about any given language, down to the nitty-gritty details, the better off you'll be. Great thread.