In the last tutorial we learned what a string is and coded some examples. In this tutorial we’ll go into some further depth with string functions. PHP’s built-in string functions enable you to easily do special things with your strings. Let’s start with a quick example.
1. If WampServer is not running, go ahead and start it. Then open your code editor, create a new file called function_strings.php and save it to your “introduction” directory under c:\wamp\www
2. Type in the code below (with or without the comments — your choice):
<html> <head> <title>PHP String Functions</title> </head> <body> <?php $dog = "spot"; // The dog is named Spot, written in lowercase letters. echo $dog; // Output "spot" to the browser. echo '<br />'; // Output a line break. // Using the strtoupper function, we will make all of the letters UPPERCASE. echo strtoupper($dog); ?> </body> </html>
3. Save the file. In your Web browser, go to: http://localhost/introduction/function_strings.php
Note the difference between the first line and the second. The first line shows “spot” which is the output of the variable. The second line shows “SPOT” which is the ouput of the variable after being handled by the strtoupper function, which caused it to show in all UPPERCASE.
This was just one quick example. PHP has a ton of string functions to use. Feel free to try them out for yourself!
Note: This article is incomplete. More info will be added… eventually. ;)
Go back to Building PHP Scripts.