09.25.07
PHP strlen() and strops() Functions
PHP strlen() and strops() Functions
Now that you have a basic grasp and understanding of just how the basics of PHP string variables work, it is important to continue that information by introducing two more functions that you can use in order to perform actions on the strings that you have in your code. These functions might seem pretty useless at the moment, but keep them in mind for later when you have a greater wealth of knowledge that allows you to program more complicated PHP scripts. You will find that this knowledge might come in very handy in more than one circumstance.
The first of the two functions that will be discussed in this article is the strlen() function. Strlen stands for String Length and as the name might simply it is a function that you can use to get the PHP script to calculate the length of a particular string. For example, if we were to take our string of “PHP Rocks” from the previous examples, the strlen() function syntax would look like this:
<?php
Echo strlen(“PHP Rocks”);
?>
When this PHP script was executed on the server, the output that was returned would be the number 9. This is because there are three characters in PHP, one character in the space and five characters in the word rocks for a total sting length of 9 characters. String length is always measured in characters when using the strlen() function. This function can be useful for people that are using string variables to keep track of the number of times something has been done. For example, if a teacher wanted to see how many assignments had been given to a particular student, all they would have to do was do a simple strlen() of the string of grades the student had to get the answer they wanted.
Another useful function is the strpos() function. Strpos stands for String Position and basically signifies the ability of the script to find the exact position of a smaller string within a larger string.
For example, if we wanted to find the position of the word Rocks in the string PHP Rocks, we would execute the following script:
<?php
Echo strpos(“PHP Rocks”,”Rocks”);
?>
The first position in the string is always 0 rather than 1 and therefore the output returned by this script would be the number 4. This is because the first part of the Rocks string is five places into the larger string PHP Rocks.
Scriptycan is a great software repository featuring both free and commercial PHP scripts and applications for developers and programmers.