PHP Strings

PHP Strings are groups of characters, like \”PHP supports string operations.\”

NOTE: Built-in string functions are listed in PHP String Functions Function Reference.

Here are some good examples of string:

$var= \”This is a string with double quotes\”;
$var_2 = \”This is a string that is a bit longer and has only one quote\”;
$var_39 = \”This string has thirty-nine characters\”;
$var_0 = \”\”; / a string with no characters

Single-quoted strings are almost taken at face value, while double-quoted strings replace variables with their values and treat certain character sequences in a special way.

PHP Strings Example

<?php
$string= \”Hello\”;
$val= \’$string will not print!\\\\n\’;

print($val);
print \”<br />\”;

$literally = \”$string will print!\\\\n\”;

print($val);
?>

Output

$string  will not print!\\n
Hello will print!\\n

There are no artificial limits on how long a string can be. As long as you have enough memory, you should be able to make strings as long as you want.

PHP does the following two things to strings that are separated by double quotes, like \”this\”:

Some sequences of characters that start with a backslash () are replaced by special characters.

Variable names that start with $ are changed to strings that show what their values are.

The escape-sequence replacements are −

 

  • \\n is replaced by the newline character;
  • \\r is replaced by the carriage-return character;
  • \\t is replaced by the tab character;
  • \\$ is replaced by the dollar sign itself ($);
  • \\\” is replaced by a single double-quote (\”);
  • \\n is replaced by the newline character;

Concatenation Operator for Strings

Use the dot (.) operator to join two string variables together.

PHP Strings Example

<?php
$string1=\”Hello User\”;
$string2=\”43210\”;

echo $string1 . \” \” . $string2;
?>

Output

Hello User 43210

If you look at the code above, you can see that the concatenation operator was used twice. This is because a third string had to be added.

We put a string with a single character, an empty space, between the two string variables to separate them.

The strlen() function

With the strlen() function, you can find out how long a string is.

Let\’s find out how long our \”Hello String!\” string is. −

PHP strlen Example

<?php
echo strlen(\”Hello String!\”);
?>

Output

13

The length of a string is often used in loops or other functions where it is important to know when the string ends. (That is, we would want the loop to end when the last character in the string is reached.)

The strpos() function

The strpos() function is used to look for a string or character within a string.

This function will return the position of the first match if the string contains a match. It will return FALSE if no match is found.

Let\’s see if our string contains the word \”String.\”

Example

<?php
echo strpos(\”Hello String!\”,\”String\”);
?>

Output

6

As you can see, the string \”String\” is in position 6 of our string. The reason that it is 6, and not 7, is that the first position in the string is 0, and not 1.

Related Posts
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

Hypertext Preprocessor (PHP) is a programming language that lets web developers make dynamic content that works with databases. PHP is Read more

Introduction of PHP

PHP started out as a small open source project that grew as more and more people found out how useful Read more

Syntax Overview of PHP

This chapter will show you some of PHP\'s very basic syntax, which is very important for building a strong PHP Read more

Environment Setup in PHP

To develop and run PHP on your computer, you need to instal three important parts. Web server PHP can almost Read more

Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program. Here are the Read more

Scroll to Top