How to Check If the First Character of a Word Is Uppercase With PHP

The PHP language is used to create interactive Web pages. In some cases, users need to input data according to a specific syntax. For example, proper names should be capitalized. If you want your PHP Web page to test to see if the first character of a word is uppercase, all you need to do is write some short logic. It only takes a few lines of code and does not require much programming experience to accomplish.

Instructions

    • 1

      Decide how you will run your PHP code. If you have a PHP server, you can execute code using PHP files. If you do not have access to a PHP server, you can use an online PHP interpreter. You can enter the code to test the first character of a word into either a PHP file or the online PHP interpreter.

    • 2

      Begin your PHP program with the following statement:

      <?php

    • 3

      Create a new string variable and assign it some text by writing the following statement:

      $string = "Valley";

    • 4

      Test to see if the first character of the string is uppercase using the following "if" statement. If the string begins with an uppercase letter, the line "String is uppercase" is printed to the output window.

      if($string{0} === strtoupper($string{0})) {print("String is uppercase");}

    • 5

      Create an "else" statement that runs when the string is not uppercase. Write the following statement:

      else{print("String is lowercase"); }

    • 6

      Conclude your PHP program with the statement below. Your program is now ready to be tested on your PHP server or online PHP interpreter.

      ?>

Related Searches:

References

Comments

Related Ads

Featured