• 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
  • VBA
  • 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.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 2.  php json_encode() Function – Get JSON Representation of Variables in php
  • 3.  Get Array Length with php count() Function
  • 4.  php unset – How to Destroy Variables in php
  • 5.  Remove First Character from String Using php
  • 6.  Delete Cookie Using php
  • 7.  Check if Variable Exists in php with isset() Function
  • 8.  php Rename File – Renaming Files and Directories Easily
  • 9.  Reversing an Array in php with array_reverse() Function
  • 10.  php Convert Degrees to Radians with php deg2rad() Function

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy