Dysan Posted December 26, 2007 Share Posted December 26, 2007 Hi, How do I display just the 1st character of a string stored inside a variable? Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/ Share on other sites More sharing options...
PHP_PhREEEk Posted December 26, 2007 Share Posted December 26, 2007 $firstChar = substr($variable, 0, 1); PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423605 Share on other sites More sharing options...
interpim Posted December 26, 2007 Share Posted December 26, 2007 <?php // Get the first character of a string $str = 'This is a test.'; $first = $str[0]; Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423608 Share on other sites More sharing options...
Barand Posted December 26, 2007 Share Posted December 26, 2007 "[]" is deprecated in favour of "{}" when pulling a character from a string. $first = $str{0}; Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423614 Share on other sites More sharing options...
interpim Posted December 26, 2007 Share Posted December 26, 2007 ok... i pulled that snippet of code from the php manual, google is my friend LOL Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423628 Share on other sites More sharing options...
Orio Posted December 26, 2007 Share Posted December 26, 2007 "[]" is deprecated in favour of "{}" when pulling a character from a string. $first = $str{0}; You got it the other way around Barand... From strings: 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 like $str[42] so think of a string as an array of characters. Note: They may also be accessed using braces like $str{42} for the same purpose. However, using square array-brackets is preferred because the {braces} style is deprecated as of PHP 6. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423638 Share on other sites More sharing options...
Barand Posted December 26, 2007 Share Posted December 26, 2007 I know, I just checked the online manual. In my .chm version of the manual which I download Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string in curly braces. ????: For backwards compatibility, you can still use array-brackets for the same purpose. However, this syntax is deprecated as of PHP 4. Wish they'd make up their mind ! Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423642 Share on other sites More sharing options...
Orio Posted December 26, 2007 Share Posted December 26, 2007 I guess it's time for you to update your manual. Orio. Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423643 Share on other sites More sharing options...
Barand Posted December 26, 2007 Share Posted December 26, 2007 Your right, but it'll probably be back to curlies in PHP7 Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423647 Share on other sites More sharing options...
PHP_PhREEEk Posted December 26, 2007 Share Posted December 26, 2007 meanwhile, substr() remains unambiguous... = D PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423655 Share on other sites More sharing options...
Barand Posted December 26, 2007 Share Posted December 26, 2007 ... but probably a lot slower for a single character Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423660 Share on other sites More sharing options...
PHP_PhREEEk Posted December 26, 2007 Share Posted December 26, 2007 For most script projects posted back here, the term 'negligible' comes to mind when speaking in terms of 'slower'! heh Interesting discussion, nonetheless... and PHP Devs do tend to have a certain sense of humor regarding a few things. PhREEEk Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423663 Share on other sites More sharing options...
emehrkay Posted December 26, 2007 Share Posted December 26, 2007 I prefer the curly braces as I, and many others, associate brackets with arrays Quote Link to comment https://forums.phpfreaks.com/topic/83272-display-1st-character/#findComment-423665 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.