danludwig Posted August 5, 2009 Share Posted August 5, 2009 Hi, It's been a long time since I've coded PHP. I am trying to rewrite some PHP code I found in a different language, and I have a question about treating strings as character arrays. For example, take the following PHP code: $someString = "Here is my string value."; $someIndex = 6; $someMutation = $someString[$someIndex]; With this code, I would assume that the value of $someMutation would be "s", since it is the character at the 6th position in the string (when starting from zero). However, what happens if the value of $someIndex was changed to 30, or 100, or ONE BILLION, or any index that is >= strlen($someString)? Does PHP return -1 in that case? Thanks for the help, I don't have a PHP execution environment to test in. Dan Quote Link to comment https://forums.phpfreaks.com/topic/169013-square-brackets-applied-to-string-variable/ Share on other sites More sharing options...
Zane Posted August 5, 2009 Share Posted August 5, 2009 why don't your try it and find out. also..it isn't brackets that you put in the declaration....it's curly braces. When you use brackets it assumes you are calling an array. Example: $blahblah = 'some bunch of text that immediately popped into my mind'; echo $blahblah{8} //should return n Quote Link to comment https://forums.phpfreaks.com/topic/169013-square-brackets-applied-to-string-variable/#findComment-891731 Share on other sites More sharing options...
danludwig Posted August 5, 2009 Author Share Posted August 5, 2009 why don't your try it and find out. I don't have a PHP execution environment to test in. Dan Quote Link to comment https://forums.phpfreaks.com/topic/169013-square-brackets-applied-to-string-variable/#findComment-891733 Share on other sites More sharing options...
Zane Posted August 5, 2009 Share Posted August 5, 2009 straight from the manual String access and modification by character Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 6. Use square brackets instead. Warning Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte. Example #8 Some string examples <?php // Get the first character of a string $str = 'This is a test.'; $first = $str[0]; // Get the third character of a string $third = $str[2]; // Get the last character of a string. $str = 'This is still a test.'; $last = $str[strlen($str)-1]; // Modify the last character of a string $str = 'Look at the sea'; $str[strlen($str)-1] = 'e'; ?> Note: Accessing variables of other types using [] or {} silently returns NULL. Guess I was kinda off on the curly brackets thing Quote Link to comment https://forums.phpfreaks.com/topic/169013-square-brackets-applied-to-string-variable/#findComment-891739 Share on other sites More sharing options...
danludwig Posted August 5, 2009 Author Share Posted August 5, 2009 From what you've posted, it looks like PHP will not return an error or -1, but will instead modify $someString by appending extra spaces to the end of it. For example: $someString = "Here is my string value."; $someIndex = 30; $someMutation = $someString[$someIndex]; In this case $someMutation would equal " " (empty space) and $someString would be changed to "Here is my string value. " (added spaces so that the string length is 31 characters and the index of 30 is valid). Is this right? Quote Link to comment https://forums.phpfreaks.com/topic/169013-square-brackets-applied-to-string-variable/#findComment-891746 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.