0

Here is my block of php I want to capitalize the first letter in the value the string returns from $user->data['username_clean']

The method I tried didn't work with this error.

Fatal error: Call to undefined method

It is likely however that I formatted it incorrectly. php code is here.

<?php 
if ($user->data['user_id'] == ANONYMOUS) {
   echo '
        <li class="dropdown">
           <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Login<span class="caret"></span></a>
           <ul class="dropdown-menu">
              <form id="loginform" method="post" action="/forums/ucp.php?mode=login">
                 <label>Username</label>
                 <input name="username" type="text" id="username" class="width120 textbox" tabindex="1" />
                 <label>Password</label>
                 <input name="password" type="password" id="password" class="width120 textbox" tabindex="2" />
                  <label>Remember Me?:</label>
                  <input type="checkbox" name="autologin">
                  <input type="submit" value="Login" name="login">
                  <input type="hidden" name="redirect" value="indextest.php"
              </form>
        </li>';
} else { 
    echo '
        <li class=dropdown>
        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button"  aria-haspopup="true" aria-expanded="false">',  $user->data['username_clean'], '<span class="caret"></span></a>
        <ul class="dropdown-menu">
                    <li><a href="#">User Control Panel</a></li>
                    <li><a href="#">Profile</a></li>
                    <li><a href="#">Logout</a></li>
                  </ul>
        </li>';
}
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Ben Scott
  • 11
  • 3

3 Answers3

2

To uppercase the first character of a string use

ucfirst( $user->data['username_clean'] );
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
2

Uppercase the first character using ucfirst like so:

ucfirst( $user->data['username_clean'] );

ucfirst — Make a string's first character uppercase

Description

string ucfirst ( string $str )

Source

http://php.net/ucfirst

And, an alternative and kind of creative solution would be to do this (fun to think about although you will probably not want to do this):

$user->data['username_clean']{0} = strtoupper($user->data['username_clean']{0});

You are accessing the first character {0} and changing it to uppercase.

Clay
  • 4,700
  • 3
  • 33
  • 49
1

ucfirst() is the PHP function to capitalize the first letter in a string.

So, you need to wrap the value you want to capitalize in that call.

Try changing:

 $user->data['username_clean'], 

to:

ucfirst($user->data['username_clean']), 
Umopepisdn
  • 807
  • 6
  • 16