PHP Tutorials – Case Sensitivity in PHP | LoudProgrammer

PHP Tutorials – Case Sensitivity in PHP

free-php-mysql-tutorials-course-online-tutorial

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.

<!DOCTYPE html>
  <head>
    <title>Case Sensitivity </title>
  </head>
  <body>
  <?php
  
  ?>
  </body>
</html>

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 browser and the output is the same.

<!DOCTYPE html>
  <head>
    <title> Case Sensitivity </title>
  </head>
  <body>
  <?php
    echo "First Name: Neil";
    ECHO "First Name: Neil";
    EchO "First Name: Neil";
  ?>
  </body>
</html>

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.

<!DOCTYPE html>
  <head>
    <title>Case Sensitivity</title>
  </head>
  <body>
  <?php
    $state = "Kansas";

    echo $state;
    echo $STATE;
  ?>
  </body>
</html>

Running the above code would result in an error

Undefined variable: STATE

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.

The following two tabs change content below.
Geoffrey is a lead software developer, author and writer. He writes code from scratch but frequents GitHub. He also writes and talks about technology trends, small business tips and developer productivity hacks. He is no coffee addict.

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz