PHP Tutorials – Case Sensitivity in PHP

PHP Tutorials Case Sensitivity in PHP

In PHP, key words are case insensitive whereas variable names are case sensitive. Create an empty text file in your server root directory and save it as  casesensitivity.php  and then include the following code in it.

Key words like  echo  that represents a function that prints a text string to the browser are case insensitive. This means that you can use them in either upper case or lower case or a mix of both and the outcome of the statement would be the same.

In the example below, we use the echo statement in different cases to print out a text string to the web browser and the output is the same.

When this script is executed the string “First Name: Neil” is printed to the browser three times.

Variable names on the other hand are case sensitive. That is to say that a variable named using lowercase characters must only be accessed using the same lowercase characters. Using a different case from that of the name of the variable will result to an error.

In the example below, we define a variable to store the name of the state an event is to be held. We then try to print out the name of the state by accessing the variable using different cases.

Running the above code would result in an error

This is because the variable STATE in caps has not been defined yet. What we have is the variable $state in lower case.

Even though the key words are not case sensitive in PHP, you should choose a particular case to use consistently throughout your code. The lowercase is the most commonly used by many programmers and I personally prefer it over the upper case.

Did you find this article useful? Please share your thoughts below.

About the Author Geoffrey Barnes

Geoffrey is an experienced software developer and open source evangelist. When not coding he writes and talks about current technology trends, small business tips and developer productivity hacks. He is no coffee addict.

follow me on:

Leave a Comment: