• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Capitalize First Letter of String with php ucfirst() Function

Capitalize First Letter of String with php ucfirst() Function

March 22, 2022 Leave a Comment

In php, we can capitalize the first letter of a string easily with the php ucfirst() function.

$variable = "this is a string variable";

echo ucfirst($variable);

//Output:
This is a string variable

When working with string variables in our php programs, it is useful to be able to easily manipulate and change the value of the variables. One such change is if you want to capitalize the first letter of a string.

To make the first character of a string uppercase in php, we can use the php ucfirst() function.

ucfirst() capitalizes the first character of a string.

Below is an example of how to use ucfirst() to convert the first character of a string to uppercase.

$variable = "this is a string variable";

echo ucfirst($variable);

//Output:
This is a string variable

Capitalizing the First Letter of Each Word with ucwords() Function in php

If you are looking to capitalize the first letter of every word of a string in php, you can use the ucwords() function.

ucwords() capitalizes the first character of each word in a string. To use ucwords() pass a string and an optional delimiter.

Below is an example of how to use ucwords() to convert the first character of each word in a string to uppercase.

$variable = "this is a string variable";

echo ucwords($variable);

//Output:
This Is A String Variable

Converting an Entire String to Uppercase in php with strtoupper() Function

If you instead want to convert all characters in a string to uppercase, we can make a string uppercase easily in php.

The strtoupper() function takes in a string and makes all of the characters uppercase.

Below is an example of how to convert a string uppercase in php with strtoupper().

$variable = "this is a string variable";

echo strtoupper($variable);

//Output:
THIS IS A STRING VARIABLE

Hopefully this article has been useful for you to learn how to capitalize the first letter of a string in php.

Other Articles You'll Also Like:

  • 1.  php array_combine() – Create New Array Given Arrays of Keys and Values
  • 2.  PHP Numerically Indexed Array Begin With Position 0
  • 3.  php Convert Degrees to Radians with php deg2rad() Function
  • 4.  Remove Specific Character from String Variable in php
  • 5.  Get Current Location Latitude Longitude in php
  • 6.  php max – Find Maximum Value of Array in php
  • 7.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 8.  Add Months to Date in php
  • 9.  Get Word Count of String in php with str_word_count() Function
  • 10.  php switch case – How to Use Switch…Case Statements in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy