Jump to content

How to use ucfirst() with a Superglobal variable?


nomadsoul

Recommended Posts

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?

 

Link to comment
Share on other sites

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']);
                }
            ?>  

Link to comment
Share on other sites

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  ;D

have fun!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.