Working with Strings

PHP & MySQL Tutorial

This tutorial covers crucial aspects of PHP and MySQL, specifically focusing on string comparisons, converting to upper and lower case, and searching through strings. The tutorial is beneficial for anyone interested in boosting their web development skills or planning to start a career in the field.

Key Insights

  • The tutorial covers three key topics in PHP and MySQL: comparing strings, converting to upper and lower case, and searching through strings.
  • The strcmp() function is used for case-sensitive comparison of strings. The function outputs 0 if the strings are equal.
  • The strtolower() and strtoupper() functions are used to convert strings to lower case and upper case respectively.
  • The strpos() function is used to search through a string for a specific substring. The function returns a numerical value indicating the position of the substring in the string.
  • The tutorial provides multiple examples and exercises to test the usage of these functions, enhancing practical understanding.
  • The PHP manual provides more in-depth explanations and functions related to strings.

This exercise is excerpted from Noble Desktop’s past app development training materials and is compatible with iOS updates through 2021. To learn current skills in web development, check out our coding bootcamps in NYC and live online.

What This Tutorial Covers

  • String Concatenation

    Use the . operator to join strings.

  • String Functions

    strlen, strpos, substr, str_replace.

  • Variable Interpolation

    Embed variables directly in double-quoted strings.

Topics Covered in This PHP & MySQL Tutorial:

Comparing Strings, Converting to Upper & Lower Case, Searching Through Strings, Case-sensitive & Case-insensitive

Exercise Overview

Let’s explore some basic string functions.

Comparing Two Strings

One way to compare strings is to use the strcmp() function. This will perform a case-sensitive comparison. If the strings are equal it will output 0. If the first string is greater than the second string it will return a positive value, and if the second string is greater it will return a negative value.

SQL Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.
  1. In your code editor, open strings.php from the phpclass folder.

  2. To test it out we’ll write a simple if/else statement to see if two strings match. Notice that the second string is capitalized. In between the <body> tags add the following bold code:

    <?php 
    
       $var1 = 'noble';
       $var2 = 'Noble';
    
       if (strcmp($var1, $var2) == 0) {
          echo 'The strings match.';
       }
       else {
          echo 'The strings do not match.';
       }
    
    ?>

    If the strings exactly match strcmp() will output 0. Otherwise, the strings do not match.

  3. Save the page and then in a browser go to:
    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    As you can see, the strings do not match.

  4. Switch back to your code editor.

  5. Next let’s try a case-insensitive search. To do that we’ll use strcasecmp(). Change the code as shown in bold:

    if (strcasecmp($var1, $var2) == 0) {
          echo 'The strings match.';
       }
    else {
       echo 'The strings do not match.';
    }
  6. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    Now the strings match.

Converting to Upper & Lower Case

  1. Switch back to your code editor.

  2. To convert a string to lower case we use the strtolower() function. Delete the code between the <?php ?> tags and replace it with the following bold code:

    $email = 'myemail@myurl.com';
    
    echo strtolower($email);
  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    The string is converted to lower case.

  4. Switch back to your code editor.

  5. Converting to upper case is exactly the same with the strtoupper() function. Let’s test it. Delete the code between the <?php ?> tags and replace it with the following bold code:

    $var = 'why are you yelling?';
    
    echo strtoupper($var);
  6. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    The page will display: WHY ARE YOU YELLING?

  7. Switch back to your code editor.

  8. A more useful function only capitalizes the first word in a sentence. It also leaves any capitalization that is already there alone.

  9. Delete the code between the <?php ?> tags and replace it with the following bold code:

    $var = "don't you think that PHP is great?";
    
    echo ucfirst($var);
  10. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    Don’t you think that PHP is great?

Searching Through Strings

To search through a string we could use the strpos() function. The syntax would look like:

strpos($haystack, $needle)

This says to search through the “haystack” (typically the larger string that we are searching in) for the “needle” (the smaller substring we are searching for). It returns a numerical value with the location of the “needle.” If the needle is found at the very start of the haystack it will return 0. Keep this in mind when evaluating whether the statement is true or not. Also note that this is a case-sensitive search.

  1. Switch back to your code editor.

  2. Delete the code between the <?php ?> tags and replace it with the following bold code:

    $haystack = 'abcdefg';
    $needle = 'b';
    
    echo strpos($haystack, $needle);

    This searches the haystack “abcdef” for the needle “b.” It then outputs the numeric position.

  3. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    It will say 1. This is the position of the sub-string (needle) in the string (haystack).

  4. Switch back to your code editor.

  5. Change the $needle to 'a' as shown in bold:

    $haystack = 'abcdefg';
    $needle = 'a';
    
    echo strpos($haystack, $needle);
  6. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    It will say 0. It is very important to keep this in mind when trying to find out if a string exists.

  7. Switch back to your code editor.

  8. Delete the echo then add the following bold code:

    if ( strpos($haystack, $needle)!= false ) {
          echo 'I found my needle!';
    }

    This says if strpos() is not equal to false (in other words if it is found), echo that the needle was found.

  9. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    The page will be blank. Why is this? Because 'a' is the very first element of the string, its location is 0. In the if statement we test to see if it is false, but because we didn’t use strict typing it evaluated 0 as false. Typically in true/false statements 1=true and 0=false.

  10. Switch back to your code editor.

  11. Make it more strict by adding an extra = sign as shown in bold:

    if ( strpos($haystack, $needle)!== false ) {
            echo 'I found my needle!';
    }
  12. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    It should now say, “I found my needle!”

  13. Switch back to your code editor.

  14. Capitalize the $needle variable as shown in bold:

    $haystack = 'abcdefg';
    $needle = 'A';
  15. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    The page will be blank because it is a case-sensitive search.

  16. Switch back to your code editor.

  17. To make it case-insensitive, change the function to stripos() as shown in bold:

    if ( stripos($haystack, $needle)!== false ) {
          echo 'I found my needle!';
    }
  18. Save the page and then in a browser go to:

    • Mac: localhost:8888/phpclass/strings.php
    • Windows: localhost/phpclass/strings.php

    It will now say, “I found my needle!” again.

  19. Switch back to your code editor.

  20. Close the file. We’re done with it.

    There are many more string functions that are explained in detail in the PHP manual: php.net/manual/en/ref.strings.php

Learn more in these courses

How to Learn Full-Stack Web Development

Master Full-stack Web Development with Hands-on Training. Build Fully Functional Websites and Applications Using HTML, CSS, JavaScript, Python, and Web Developer Tools.

Yelp Facebook LinkedIn YouTube Twitter Instagram