Jump to content

Recommended Posts

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

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

 

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

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.