PHP stringLOGIN

PHP string

String variables in PHP

String variables are used for values ​​that contain characters.

After creating the string, we can operate on it. You can use the string directly in the function or store it in a variable.

In the following example, we create a string variable named txt and assign the value to "Hello world!". Then we output the value of the txt variable:

Example

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

Try it »

Note: When you assign a text value to a variable, please remember to give the text The value is enclosed in single or double quotes.


The text that needs to be displayed on the page, in the PHP code we use single quotes or double quotes to include

1. If the character If the string contains single quotes, then the string can use double quotes

$val = "'foo '" ;

2. If the string contains double quotes , you can use single quotes to include

$val = '" foo "' ;

3. If you want to display double quotes in the string contained in double quotes, or To display single quotes in a string enclosed by single quotes, we can add an escape character (a slash \)

$val = '\' foo \'' ;

$val = "\" foo \"" ;

Quotation marks can be used freely, as long as you are careful not to mix them, the quotation marks must be paired

Unique The difference is the problem of variables. Variables can be directly converted into values ​​in double quotes, but single quotes cannot.

<?php 
$name = "tom"; 
echo 'my name is $name';   // 输出 "my name is $name" 
echo "my name is $name";   // 输出 "my name is tom" 
?>

Note: One of the most common mistakes for beginners is to forget to add quotes to the text.

"hello" This is a string

$hello This is a variable

hello The system will think this is a constant

Now, let's take a look at some commonly used functions and operators for manipulating strings.

PHP Concatenation Operator

In PHP, there is only one string operator.

Concatenation operator (.) is used to concatenate two string values.

The following example demonstrates how to concatenate two string variables together:

Example

<?php
$txt1 = "Hello world!";
$txt2 = "What a nice day!";
echo $txt1 . " " . $txt2;
?>

The above code will output: Hello world! What a nice day!

Tip: In the above code, we have used the concatenation operator twice. This is due to the fact that we need to insert a space between the two strings.

PHP strlen() function

Sometimes it is useful to know the length of a string value.

strlen() function returns the length of the string (number of characters).

The following example returns the length of the string "Hello world!":

Example

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

Try it »

The above code will output: 12

Tip: strlen() is often used in loops and other functions, when it is important to determine when the string ends. (For example, in a loop, we need to end the loop after the last character in the string. Don't forget that spaces also occupy a length)

PHP strpos() function

strpos() function is used to find a character or a specified piece of text within a string.

If a match is found in the string, this function returns the first matching character position. If no match is found, returns FALSE.

The following example finds the text "world" in the string "Hello world!":

Example

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


Try it out»

The above code will output: 6

Tip: In the above example, the position of the string "world" is 6. The reason it's 6 instead of 7 is that the first character in the string is at position 0, not 1. Please don't forget the spaces.

Complete PHP String Reference Manual

For a complete reference manual for all string functions, please visit our PHP String Reference Manual.

This reference manual provides a brief description and application examples of each function!


Next Section
<?php $txt = "Hello world!"; echo $txt; ?>
submitReset Code
ChapterCourseware