php

How Do I convert a String to Lowercase in PHP

A string refers to a sequence of characters typically used to store and work with text information in a program.
PHP is a popular and influential programming language that supports the creation and use of string types.

This guide will teach us how to work with strings and various methods to make working with strings in PHP easier and more efficient.

The Basics – How to Create a String

Before we get to discussing how to use built-in PHP methods to manipulate strings, let us go back to the basics and learn how to create a string.

There are two main ways to create a string in PHP:

  1. Using a pair of single quotes
  2. Using Double Quotes

NOTE: You can use other methods such as Heredoc and Newdoc as introduced in various versions of PHP. Check the documentation to learn more.

Although we will not dive into how each of the methods works, let us use a few examples for each technique of string creation in PHP.

Using Single Quotes

This is the easiest way to create a string in PHP. To create a string using single quotes, enclose the text value with single quotes as shown in the syntax below:

<?php

    $mystring = 'Hello world';

    echo $mystring;

?>

The above syntax will create a string variable and store the value specified in single quotes.

If you have a string with a single quotation mark, you can escape it using a single backslash, as shown in the example below:

<?php

    $mystring = 'This is a single quote (\')';

    echo $mystring;

?>

The backslash character will escape the single quote, and PHP will treat it as part of a string.

Using Double Quotes

The second and most popular way to create a string in PHP is using a double quote. Enclose your string value in a pair of double quote marks as shown in the example below:

<?php

    $mystring = "This is a double quoted string";

    echo $mystring;

?>

If you want to include double quotes, you can use the backslash escape character as:

<?php

    $mystring = "This is a double "zero" quoted string";

    echo $mystring;

?>

PHP Convert String to Lowercase

When working with string types in most programming languages, there is one standard operation: Converting a string to lowercase.
PHP provides you with a method to convert the passed string to lowercase characters.

Remember that the function works on alphabetical characters, and characters such as numerical values are ignored.

The syntax of the method can be expressed as:

strtolower(string $string)

The function accepts the string to be converted to lowercase as the parameter.

The following example illustrates how to use the strtolower function.

<?php

    $mystring = "HELLO WORLD";

    $lower = strtolower($mystring);

    echo $lower

?>

Run the code as shown in the command:

php strings.php

The above command should print the string stored in $mystring converted to lowercase:

hello world

PHP Convert to Uppercase

Suppose you want to perform the reverse of the strtolower method? In that case, you can use the strtoupper() method.

It is similar to the strtolower() function but returns all the alphabetical characters converted to uppercase.

The syntax of the method is as shown.

strtoupper(string $string)

It takes the string to convert to uppercase as the argument.

The example below shows how to use the strtoupper()

<?php

    $mystring = "";

    $upper = strtoupper($mystring);

    echo $upper

?>

The code above returns the value of $mystring variable converted to uppercase.

Closing

This tutorial walks you through the basics of creating and manipulating strings using the built-in PHP methods.

Thank you for reading, and stay tuned for more tutorials.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list