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()
andstrtoupper()
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.
Explore the essentials of string functions in PHP & MySQL, including comparing strings, converting to upper & lower case, and searching through strings, with step-by-step coding examples and exercises.
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.
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.
In your code editor, open strings.php from the phpclass folder.
-
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. - 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.
Switch back to your code editor.
-
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.'; }
-
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
Switch back to your code editor.
-
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);
-
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.
Switch back to your code editor.
-
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);
-
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?
Switch back to your code editor.
A more useful function only capitalizes the first word in a sentence. It also leaves any capitalization that is already there alone.
-
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);
-
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.
Switch back to your code editor.
-
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.
-
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).
Switch back to your code editor.
-
Change the
$needle
to'
a'
as shown in bold:$haystack = 'abcdefg'; $needle = 'a'; echo strpos($haystack, $needle);
-
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.
Switch back to your code editor.
-
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. -
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. Switch back to your code editor.
-
Make it more strict by adding an extra = sign as shown in bold:
if ( strpos($haystack, $needle)!== false ) { echo 'I found my needle!'; }
-
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!”
Switch back to your code editor.
-
Capitalize the
$needle
variable as shown in bold:$haystack = 'abcdefg'; $needle = 'A';
-
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.
Switch back to your code editor.
-
To make it case-insensitive, change the function to
stripos()
as shown in bold:if ( stripos($haystack, $needle)!== false ) { echo 'I found my needle!'; }
-
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.
Switch back to your code editor.
-
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