I am Considering this PHP string:
$stringTest = “this is a sequence of chars”
.
Let's say I want to have a new string $firstChar
that shall contain the first character in $stringTest
.
How do I write the program?
I am Considering this PHP string:
$stringTest = “this is a sequence of chars”
.
Let's say I want to have a new string $firstChar
that shall contain the first character in $stringTest
.
How do I write the program?
Just do
$firstChar = $stringTest[0];
Strings are an Array of Chars and therefore can be accessed with an Array Index.
$firstChar = $stringTest[0];
This would get the first character of string - treating stringTest as an array of characters - and is fastest method.
$firstChar = substr($stringTest, 0, 1);
This is slower, and takes a substring - retrieving 1 character (the last argument) from the string, and setting off from an offset of 0.