What I’m Learning – PHP Special String Commands

Below I have string commands that are used in the PHP language that will allow you to find out information or change what is already written!

The following string command will return how many characters are within the string:

<?php
echo strlen(“Hello world!”); // outputs 12
?>

The following string command will return how many words are within the string:

<?php
echo str_word_count(“Hello world!”); // outputs 2
?>

The following string command will return the string written backwards:

<?php
echo strrev(“Hello world!”); // outputs !dlrow olleH
?>

The following string command looks for a specific word, and will return the location within the string. Unless it is not there, then will return “false”:

<?php
echo strpos(“Hello world!”, “world”); // outputs 6
?>

The following string command will replace the first word with the second word, in the third section:

<?php
echo str_replace(“world”, “Monkey”, “Hello world!”); // outputs Hello Monkey!
?>
Hope this was helpful,
The Code Damsel

Leave a Reply

Your email address will not be published. Required fields are marked *