php strings

In the last lesson, PHP Echo, we used strings a bit, but didn’t talk about them in depth. Throughout your PHP career you will be using strings a great deal, so it is important to have a basic understanding of PHP strings.

A string is a sequence of characters

PHP string creation

Before you can use a string you have to create it! A string can be used directly in a function or it can be stored in a variable. Below we create the exact same string twice: first storing it into a variable and in the second case we send the string directly to echo.

Example:

Common string function:

 

  • PHP strlen() Function

    This function is used to find the length of a given string. This function is very common in use as many of the times we need to know the length of the string.

    Syntax:

    Strlen(string whose length you want to find);

    Example:

     

  •  PHP stripos() Function

    This function is used to “Find the position of the first occurrence of some substring inside the string:”

    Syntax

    stripos(string,find,start)

    Example:

     

  • PHP strcmp() Function

    This function is used to compare two strings.

    Note: The comparison is case sensitive i.e the string “tutsocean” and “TUTSOCEAN” is different.

    Syntax:

    Strcmp(first string,second string);

    Example:

Note: In the above example, we have used the ! operator which we will learn in detail in the upcoming tuts. If the output of strcmp() function is 0 then that means strings are equal. That’s why we have used ! (NOT) operator.

This is how you can use PHP Strings. Good Luck!