PHP Strings

0
6كيلو بايت

A string is a sequence of characters, like "Hello world!".


Strings

Strings in PHP are surrounded by either double quotation marks, or single quotation marks.

ExampleGet your own PHP Server

echo "Hello";
echo 'Hello';
Try it Yourself »

Note There is a big difference between double quotes and single quotes in PHP.

Double quotes process special characters, single quotes does not.


Double or Single Quotes?

You can use double or single quotes, but you should be aware of the differences between the two.

Double quoted strings perform action on special characters.

E.g. when there is a variable in the string, it returns the value of the variable:

Example

Double quoted string literals perform operations for special characters:

$x = "John";
echo "Hello $x";
Try it Yourself »

Single quoted strings does not perform such actions, it returns the string like it was written, with the variable name:

Example

Single quoted string literals returns the string as it is:

$x = "John";
echo 'Hello $x';
Try it Yourself »


String Length

The PHP strlen() function returns the length of a string.

Example

Return the length of the string "Hello world!":

echo strlen("Hello world!");
Try it Yourself »

Word Count

The PHP str_word_count() function counts the number of words in a string.

Example

Count the number of word in the string "Hello world!":

echo str_word_count("Hello world!");
Try it Yourself »

Search For Text Within a String

The PHP strpos() function searches for a specific text within a string.

If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.

Example

Search for the text "world" in the string "Hello world!":

echo strpos("Hello world!", "world");
Try it Yourself »

Tip: The first character position in a string is 0 (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!


PHP Exercises

Test Yourself With Exercises

Exercise:

Get the length of the string "Hello World!".

echo ("Hello World!");

Start the Exercise

 
 

 

 

البحث
الأقسام
إقرأ المزيد
أخرى
PHP Operators
PHP Operators Operators are used to perform operations on variables and values. PHP divides the...
بواسطة PHP Tutorial 2024-05-17 07:41:39 0 9كيلو بايت
أخرى
PHP Multiline Comments
Multi-line Comments Multi-line comments start with /* and end with */. Any text...
بواسطة PHP Tutorial 2024-05-17 07:15:48 0 6كيلو بايت
أخرى
PHP Nested if Statement
Nested If You can have if statements inside if statements, this is...
بواسطة PHP Tutorial 2024-05-17 07:47:26 0 4كيلو بايت
أخرى
PHP - Escape Characters
Escape Character To insert characters that are illegal in a string, use an escape character. An...
بواسطة PHP Tutorial 2024-05-17 07:32:36 0 11كيلو بايت
أخرى
PHP echo and print Statements
With PHP, there are two basic ways to get output: echo and print. In this...
بواسطة PHP Tutorial 2024-05-17 07:20:06 0 8كيلو بايت
Sociallez https://sociallez.com