6

I'm trying to figure how a function I've been given works -- or rather doesn't work. The problematic areas include array notation like this:

$small[$end[$i]{0}]

I think the idea is to append "0" to the value of $end[$i] and use that for the index of $small. But it doesn't work in PHP5.3. Is this a deprecated syntax and is it trying to do what I think it is?

6
  • doesn't work as in "php breaks with a syntax error" or doesn't work as in "what should happen isn't happening"? Commented Apr 23, 2010 at 14:00
  • 6
    "Note: String s may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42]." php.net/manual/en/language.types.string.php Commented Apr 23, 2010 at 14:06
  • @Lo'oris: $end[$i]{0} seems to be evaluating to null or ''. Commented Apr 23, 2010 at 14:20
  • @Tom Haigh: now that you've pointed it out, I do remember strings being accessible this way. Turns out though, that if the string is a number, it doesn't work. e.g. $foo=200; $foo[0]!=2; I think the solution is to replace the syntax with substr($foo,1); Commented Apr 23, 2010 at 14:32
  • 1
    @dnagirl: "$foo=200;" that isn't a string. using substr() is maybe slower and only works because the int is implicitly converted to a string. Commented Apr 23, 2010 at 14:46

1 Answer 1

9

It's getting the first character from the $end[$i] string and then accessing the $small array using that character as the array key.

Edit:

Since $foo=200; $foo[0] != 2;//=='', what do you recommend for getting the leftmost digit when the magnitude of the number is unknown?

The easiest way is substr($foo, 0, 1) in PHP.

If you're using a strongly typed language, I have some metrics you may be interested in reading from another answer of mine: How can you get the first digit in an int (C#)?

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

1 Comment

thanks for correcting my understanding of what it's trying to do. B

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.