Php first letter capitalise

PHP ucfirst() Function

The ucfirst() function converts the first character of a string to uppercase.

  • lcfirst() — converts the first character of a string to lowercase
  • ucwords() — converts the first character of each word in a string to uppercase
  • strtoupper() — converts a string to uppercase
  • strtolower() — converts a string to lowercase

Syntax

Parameter Values

Technical Details

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

PHP Capitalize First Letter with Best Example

PHP Capitalize First Letter with Best Example

This tutorial will teach you step-by-step how to capitalize the first letter in PHP, with examples.

There are times during the development process when we need to make a string of uppercase, lowercase, or camel-case letters. PHP has many built-in functions that make the first letter capital.

And we are going to show you three methods on how to capitalize the first letter of a string in PHP. Before we begin, if you missed any of our prior lessons, you can review the topics in our PHP Tutorial for Beginners.

The following are the methods to capitalize the first letter in PHP:

Using ucfirst() function

The ucfirst() function converts the first character of a string to uppercase.

Syntax

Parameters

Below is the example of how to use the ucfirst() function.

"); echo ("Capitalized: ".ucfirst($str)); ?>
Original: itsourcecode Capitalized: Itsourcecode

If we wish to convert only the first letter of a string to uppercase and the rest to lowercase, we can use the strtolower() function in combination with the ucfirst() function.

"; echo ("Capitalize: ".ucfirst(strtolower($str))); ?>
Original: ItSourceCode Capitalize: Itsourcecode

Using chr() function

The chr() function returns a character from the specified ASCII value.

Syntax

Parameters

  • Step 1: The initial index can be used to retrieve the first character of a string; str[0] returns the first character of the input string.
  • Step 2: Using the ord() function and a negative of 32 from the character’s ASCII value, the retrieved character can be transformed into upper case. The uppercase character’s resulting ASCII value is subsequently transformed into a character using the chr() method.
  • Step 3: The substr() method returns a part of the original string between the start and end indexes.

The substr() method can be used to get the string from the second character to the end of the string. The final string is made by adding the first character that was changed to upper case to the substring that was found.

"); // This is to get the first character $gfc = $str[0]; // This is converting the first character to uppercase $conv_upper = chr(ord($gfc)-32); // This is to append the first remaining string to upper case character $str2 = $conv_upper.substr($str,1); print("Capitalized: ".$str2); ?>
Original: where source code is not a problem Capitalized: Where source code is not a problem

Using strtoupper() function

The strtoupper() function converts a string to uppercase.

Based on PHP official documentation, strtoupper() makes a string uppercase.

Syntax

Parameters

  • Step 1: The initial index can be used to retrieve the first character of a string; str[0] returns the first character of the input string.
  • Step 2: Using the strtoupper() method, which accepts a string as input and changes it to upper case, the character taken from the string can be transformed to upper case. Since a character is also a string, it can be passed to this method as input.
  • Step 3: In this example, the substr() method can be used to extract from the second character of the string to the string’s length. Concatenating the initial character changed to upper case with the resulting substring produces the final string.
"); // This is to get the first character $gfc = $str[0]; // This is converting the first character to uppercase $conv_upper = strtoupper($gfc); // This is to append the first remaining string to upper case character $str2 = $conv_upper.substr($str,1); print("Capitalized: ".$str2); ?>
Original: where source code is not a problem Capitalized: Where source code is not a problem

Frequently Asked Questions

The ucwords() function changes to uppercase the first letter of each word in a string.

Note: This function is binary-safe.

The strtoupper() method is one of the most extensively used functions in PHP for converting strings to uppercase. It accepts a string as an argument and converts all lowercase characters to uppercase. Other characters in the string, such as numerals and special characters, stay unchanged.

To capitalize the first character of a string, we can separate it using the charAt() method and then capitalize it using the toUpperCase() function.

Summary

In summary, the best way to capitalize the first letter in PHP is by using the ucfirst() function.

Lastly, if you want to learn more about Types of Loops in PHP, please leave a comment below. We’ll be happy to hear it!

Leave a Comment Cancel reply

You must be logged in to post a comment.

Источник

Читайте также:  Что делает with в питон
Оцените статью