i

PHP Tutorial

PHP Strings

One of the most common types of data you will be working in any programming language is plain text and strings represents text in php. Any data in text or a sequence of characters that you want to present in the web page can be used inside the string, and you can just write the string in double quotation marks, and you can also store the string in a variable that allows you to manage the string easily.

For e.g. 

$Favquote = "Don't be pushed around by the fears in your mind";

echo $Favquote;

You can use string functions to find out the information in strings or modify the values in the string. These are just snippets or codes when called they give some information or update the string. You can directly write the text inside these string functions or use variables inside the functions.

  • strtolower() – This function will return all the values in the lower case. For e.g. echo strtolower($Favquote); will return don't be pushed around by the fears in your mind.

  • strtoupper() - This function will return all the values in the upper case. For e.g. echo strtoupper($Favquote); will return DON'T BE PUSHED AROUND BY THE FEARS IN YOUR MIND.

  • strlen() – This string function will return the length of the values inside the function. For e.g. echo strlen($Favquote); will return 48.

  • str_word_count() – This string function returns the count of words inside the function or the variable. For e.g. echo str_word_count($Favquote) will return 10.

  • strpos() – If you want to search for a particular text within the string, you can use strpos function to return the position of the word. For e.g. echo strpos("$Favquote","fears"); will return 30.

  • strrev() – This string function will reverse the values of the string inside the function. For e.g. echo strrev($Favquote); will return dnim ruoy ni sraef eht yb dnuora dehsup eb t' noD.

  • str_replace() – It helps to replace certain values inside the string. For e.g., str_replace("fear", "panic", $Favquote) will return Don't be pushed around by the panic in your mind.

  • substr() – It allows us to grab and present some section of the string. For e.g. echo substr($Favquote, 30) will print fears in your mind.