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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.