PHP string to uppercase, PHP string to lowercase, PHP string first letter to uppercase, strtoupper, strtolower, ucwords, ucfirst, lcfirst

PHP String to Uppercase, Lowercase and First Letter Uppercase

To regulate how to input data is entered into the system by multiple users cannot be an easy task since a system that is a multiuser system can have the freedom of entering data as one wishes to.

For example, an eCommerce website that supports multi vendors who add and controls their products can come up with different kind of data keyed into the system. But now, as the owner of the website, you can control how data is displayed to other users who are potential customers by ensuring you have uniformity of data.

For example, you can have the product names to be uppercase letters while the product description to be lowercase but the first letter to start as uppercase. This can be achieved by using PHP inbuilt functions.

In this article, we shall discuss PHP string to uppercase, PHP string to lowercase, and PHP first letter to uppercase by showing the functions that are used to achieve each result and have different examples to support each function.

PHP string to uppercase

This means displaying a set text of data as uppercase text, that is, every letter of the word or words in the defined set of data will appear as capital letters as it is known in layman language. This is mostly used when one wants to emphasize something such that it will get attention.

To achieve this in PHP,

  • First, you will need to have a set of text that will be converted to uppercase defined

For this example, we will use "this is sample data to convert to uppercase".

  • The PHP function to convert to uppercase which is strtoupper() function

Please note that strtoupper() function accepts only one parameter.

The syntax for strtoupper() function is as follows

strtoupper( string );

To convert our data text to uppercase, we will need to do the following in PHP

<?php

echo strtoupper ("this is sample data to convert to uppercase");

?>

The result or output for the above code will be

THIS IS SAMPLE DATA TO CONVERT TO UPPERCASE

Another example is when data is defined as a variable like shown below

<?php

$stringdata="this is sample data to convert to uppercase";

echo strtoupper($stringdata);

?>

The output will just be the same.

When we have string data that contains numbers, there will be no change in numbers display as numbers are not defined as either uppercase or lowercase.

 PHP string to lowercase

This means displaying a set text of data as lowercase text, that is, every letter of the word or words in the defined set of data will appear as small letters as it is known. This is mostly used when one wants to display data without much emphasis.

We use the strtolower() function to convert data to lowercase and its syntax for strtolower() function is as follows

strtolower( string );

To convert our data text to lowercase, we will need to do the following in PHP

<?php

echo strtolower("THIS IS SAMPLE DATA TO CONVERT TO LOWERCASE");

?>

The result or output for the above code will be

this is sample data to convert to lowercase

 

PHP string first letter to uppercase

This means displaying a set text of data as uppercase to the first letter of each word and lowercase to the remaining set of data lowercase text, that is, every letter of the word or words in the defined set of data will appear as small letters as it is known. This is mostly used when one wants to display data without much emphasis.

We use the ucwords() function to convert data to uppercase of first letter of every word and its syntax for ucwords() function is as follows

ucwords( string );

The following code is used to convert first letter of every word to uppercase

<?php

echo ucwords("convert first letter of every word to uppercase");

?>

The result or output for the above code will be

Convert First Letter Of Every Word To Uppercase

 

Ucwords() functions has other functions related to it

  • ucfirst() which converts the first character only of a string to uppercase

<?php

echo ucfirst("convert first letter of word to uppercase");

?>

The result or output for the above code will be

Convert first letter of word to uppercase 
  • lcfirst() which converts the first character of a string to lowercase

<?php

echo lcfirst("CONVERT FIRST LETTER TO LOWERCASE");

?>

The result or output for the above code will be

cONVERT FIRST LETTER TO UPPERCASE