nomadsoul Posted May 4, 2011 Share Posted May 4, 2011 Hi, gday, how can I use the ucfirst variable on the posted variable after to form has been submitted to welcome my member? "<p>Welcome: ucfirst{$_SESSION['first_name']};" This is not working. Should I use it in the form instead? Quote Link to comment https://forums.phpfreaks.com/topic/235470-how-to-use-ucfirst-with-a-superglobal-variable/ Share on other sites More sharing options...
cssfreakie Posted May 4, 2011 Share Posted May 4, 2011 not tested but it could probably look like: <?php $_SESSION['first_name'] = 'john'; //bogus session var, remove this line ones tested if(isset($_SESSION['first_name'])){//make sure it's set echo $name = ucfirst($_SESSION['first_name']); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/235470-how-to-use-ucfirst-with-a-superglobal-variable/#findComment-1210173 Share on other sites More sharing options...
blew Posted May 4, 2011 Share Posted May 4, 2011 maybe if you use "( )" instead of "{ }"... maybe gonna work.. but u have to set the $_SESSION first, as said above Quote Link to comment https://forums.phpfreaks.com/topic/235470-how-to-use-ucfirst-with-a-superglobal-variable/#findComment-1210175 Share on other sites More sharing options...
cssfreakie Posted May 4, 2011 Share Posted May 4, 2011 not tested but it could probably look like: tested and on second thought it might be useful to first make all characters lower case and than the first upper case so here is a working example. just edited it a bit and made it into a function <?php function supercaps($var){ $var = ucfirst(strtolower($var)); return $var; } echo supercaps('GORILAaAaaaaAAaaAaaaaAA'); // will output: Gorilaaaaaaaaaaaaaaaaaa ?> now any time you need a word where the first letter is only in caps use superscaps have fun! Quote Link to comment https://forums.phpfreaks.com/topic/235470-how-to-use-ucfirst-with-a-superglobal-variable/#findComment-1210176 Share on other sites More sharing options...
nomadsoul Posted May 4, 2011 Author Share Posted May 4, 2011 Yes, I was using a session that was already set and users name is echoed no problem. It's not imperative but I thought i'd be nice to let the user see hisher name uc. I tried your code and it didn't work. I've wrapped the variable in {}brackets because it is inside html, that's how I learned it but if there is another way I will try. I will keep trying variations on your code. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/235470-how-to-use-ucfirst-with-a-superglobal-variable/#findComment-1210179 Share on other sites More sharing options...
Pikachu2000 Posted May 4, 2011 Share Posted May 4, 2011 You can't directly use a php function from within a quoted string being echoed. You'll need to concatenate it in. The curly braces are fine to use to enclose an array element, but you need to also use the parentheses that are associated with the function. echo "<p>Welcome: " . ucfirst({$_SESSION['first_name']}); // OR // echo "<p>Welcome: " . ucfirst($_SESSION['first_name']); The second is more concise and probably clearer. Quote Link to comment https://forums.phpfreaks.com/topic/235470-how-to-use-ucfirst-with-a-superglobal-variable/#findComment-1210197 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.