Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Strings in PHP

Tags: string


Hello dear readers! welcome back to another section of my tutorial on PHP. In this section of our PHP tutorial, we will be studying about Strings in PHP.

Strings can simply be described as sequences of characters. E.g., "PHP supports string operations".

Following are valid examples of string -

$string_1 = "This is a string in double quotes";
$string_2 = "This is a somewhat longer, single quoted string";
$string_39 = "This string has thirty-nine characters";
$string_0 = ""; // a string with zero characters

RECOMMENDED: A Guide to PHP Arrays

Single quoted strings are treated almost literally, whereas double quoted strings replaces variables with their values and also specially interpreting some character order.

Example

The following below is a simple example -

php
$variable
= "name";
$literally
= 'My $variable will not print!\\n';

print($literally);
print "
"
;

$literally
= "My $variable will print!\\n";

print($literally);
?>

Output

When the above code is executed, it will produce the following result -

My $variable will not print!\n
My name will print!\n

RECOMMENDED: PHP Decision Making

There are no artificial limits on string length - within the bounds of available memory, you ought to be able to produce randomly long strings.

Strings are delimited by double quotes are preprocessed in both the following two ways by PHP -

  • Certain character order that begins with backslash(\) are replaced with some special characters.
  • Variable names (that starts with $) are replaced with the string presentation of their values.

The escape-order replacements are -

  • \n is replaced by the newline character
  • \r is replaced by the carriage return character
  • \t is been replaced by the tab character
  • \$ is replaced by the dollar sign itself ($)
  • \" is replaced by the single double quote (")
  • \\ is replaced by the single backslash (\)

RECOMMENDED: A Guide to PHP Operators


String Concatenation Operator

In order to concatenate two string variables, use the dot (.) operator -

php
$string1
="Hello World";
$string2
="123456";

echo $string1
. " " . $string2;
?>

Output

When the above code is executed, it will produce the following result -

  Hello World 123456

If we look at the above code you will see that we made use of the concatenation operator twice. This is because we had to insert a third string.

In the middle of the two string variables we added a string with a single character, and an empty space used to separate the two variables.

RECOMMENDED: An Introduction to PHP

Using the strlen() function

The strlen() function is used for finding the length of a string.

Example

Let us now find the length of our string "Hello world!" -

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

Output

When the above code is executed, it will produce the following result -

  12

The length of a string is often used in loops or other functions, when it is important to know the string ends.

RECOMMENDED: How to Install PHP on Windows with Apache

Using the strpos() function

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

If a match is found in the string, this function is going to return the position of the first match. If no match is found, it will return False.

Example
Let's now see if we can find the string "world" in our string -

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

Output
When the above code is executed, it will produce the following result -

  6

From the output, the position of the string "world" in our string is 6, and not 7. Also the first string position is 0, and not 1.

RECOMMENDED: How to Setup PHP Development Environment

Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial post, we are going to be discussing about the PHP Web Concept.

Feel feel to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.


This post first appeared on Web Design Tutorialz, please read the originial post: here

Share the post

Strings in PHP

×

Subscribe to Web Design Tutorialz

Get updates delivered right to your inbox!

Thank you for your subscription

×