PHP String Variables

Posted by Monir Hossain On 6:42:00 pm | No comments
«Previous                                                                                                            NEXT CHAPTER»




A string variable is used to store and manipulate text.

String Variables in PHP

String variables are used for values that contain characters.
After we have created a string variable we can manipulate it. A string can be used directly in a function or it can be stored in a variable.
In the example below, we create a string variable called txt, then we assign the text "Hello world!" to it. Then we write the value of the txt variable to the output:

Example

<?php
$txt="Hello world!";
echo $txt;
?>

Run example »

lamp Note: When you assign a text value to a variable, remember to put single or double quotes around the value.
Now, lets look at some commonly used functions and operators to manipulate strings.


The PHP Concatenation Operator

There is only one string operator in PHP.
The concatenation operator (.)  is used to join two string values together.
The example below shows how to concatenate two string variables together:

Example

<?php
$txt1="Hello world!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;
?>
 
The output of the code above will be: Hello world! What a nice day!
Tip: In the code above we have used the concatenation operator two times. This is because we wanted to insert a white space between the two strings.


The PHP strlen() function

Sometimes it is useful to know the length of a string value.
The strlen() function returns the length of a string, in characters.
The example below returns the length of the string "Hello world!":

Example

<?php
echo strlen("Hello world!");
?>

Run example »
 
The output of the code above will be: 12
Tip: strlen() is often used in loops or other functions, when it is important to know when a string ends. (i.e. in a loop, we might want to stop the loop after the last character in a string).


The PHP strpos() function

The strpos() function is used to search for a character or a specific text within a string.
If a match is found, it will return the character position of the first match. If no match is found, it will return FALSE.
The example below searches for the text "world" in the string "Hello world!":

Example

<?php
echo strpos("Hello world!","world");
?>

Run example »
 
The output of the code above will be: 6.
Tip: The position of the string "world" in the example above is 6. The reason that it is 6 (and not 7), is that the first character position in the string is 0, and not 1.


Complete PHP String Reference

For a complete reference of all string functions, go to our complete PHP String Reference.
The PHP string reference contains description and example of use, for each function!

















«Previous                                                                                                            NEXT CHAPTER»



0 comments:

Post a Comment

Blogroll

E COMMERCE

BANGLA PAPER

ENGLISH PAPER

Blogger news

About

E COMMERCE

BANGLA PAPER

ENGLISH PAPER