Posted by : senan Wednesday, February 12, 2014

Welcome! In programming, a string is a sequence of letters, symbols, characters and arithmetic values or combination of all tied together in single or double quotes.

Strings in PHP

Example - PHP String

<?php
$str = "I Love PHP"; 
echo $str
?>
In the above example, we store the string value "I Love PHP" in a variable name $str and use echo to output its value.

PHP Strings Operators

Concatenating Strings in PHP
Sometimes while working with strings in our code, we need to join two strings. In PHP, you can use '.' to concatenate two or more strings together to form a single string.

Example 1 - Concatenating PHP Strings

<?php
$str1 = "I Love PHP.";
$str2 = "PHP is fun to learn.";
echo $str1." ".$str2;
?>
In the above example we join $str1, empty string and $str2 to form a single string. The above code will output "I Love PHP. PHP is fun to learn."

Example 2 - Concatenating PHP Strings

Another way you to concatenate strings in PHP.
<?php
$str1 = "I Love PHP.";
$str2 = $str1." PHP is fun to learn.";
echo $str2;
?>
In the above example we join $str1 with " PHP is fun to learn." and set it $str2 to form a single string and echo str2. The above code will output the same value, "I Love PHP. PHP is fun to learn." as above.

PHP Strings in Single and Double Quotes

Strings in Double Quotes
As we saw in our examples, strings are wrapped in double quotes. Using double quotes is the primary method. Double quotes allow us to escape specials characters in our string. For example, you can use a single quotes with in double quotes.
Special characters are escaped using backslash. Sometimes we need escape special characters in our strings. We will learn about in the following section.

Example - String in Double Quotes

<?php
$str = "It's a nice day today."
echo $str;
?>
Notice the apostrophe. It's a special character which we didn't need to escape in double quotes.
Strings in Single Quotes

Example - String in Single Quotes

<?php
$str = 'This is a PHP string examples in single quotes';
echo $str;
?>
Single Quotes vs. Double Quotes
Thou looking at single quote strings and double strings you may think there is any difference. However, there are reasons for using single quotes and doubles in a string.
Single quotes should be used when outputting HTML code. Since HTML tag attributes use double quotes with in themselves and since using double quotes in HTML tags is the convention, therefore it is advisable to use single quotes when wrapping a HTML code in PHP. Here's an example.

Example - Single Quotes used for wrapping HTML Code

<?php
 echo '<input type="text" name="first_name" id="first_name">';
?>
Double quotes are used when we want to use special characters in our strings such as new line characters \n and \r. Single quotes will treat them as regular characters. Also when printing a variable in a string, it is advisable to use double quotes. For example…

Example - Variable Wrapped in Double Quote String

<?php
 $name = "Matt";
 echo "Hello $name!";
?>

Escaping Special Characters

As mentioned above we can escape characters in a string using backslash. An example would be using quotes inside quotes in a string. Let's have a look.

Example - Escaping quotes with in quotes

<?php
$str = "\"This is a PHP string examples in double quotes\"";
echo $str;
?>
Notice the \". We escaped the double quote using a backslash. The above code will output "This is a PHP string examples in double quotes" in double quotes.
And same goes with single quotes. When you need to use single quotes within single quote, we need to escape it.
<?php
$str = 'It\'s a nice day today.';
echo $str;
?>
In the above example we escaped the apostrophe using a backslash like this \'.

Useful PHP String Functions

Here we will list some of the commonly used PHP string functions.

Example - strlen() Function

This function returns the number of characters in a string. It takes a string as an argument.
<?php
$str = "Hello!";
echo strlen($str);
?>
The above will output 6.

Example - str_replace() Function

This function replaces all occurrences of the search string in the main string with the replace string. Let's look at the example below.
<?php
$str = "Hello! How are you today?";
echo str_replace("Hello", "Hi", $str);
?>
In the above example, I'm saying replace the "Hello" with "Hi" in string $str. The above code will output "Hi! How are you today?"

Example - strtoupper() Function

This function converts all lower case letters to upper.
<?php
$str = "hello!";
echo strtoupper($str);
?>
The above example will output "HELLO!"

Example - ucfirst() Function

This function changes the first letter in the string to upper case.
<?php
$str = "hello!";
echo ucfirst($str);
?>
The above example will output "Hello!"

Example - trim() Function

This function removes whitespace from the beginning and from the end of the string.
<?php
$str = " hello! ";
echo trim($str);
?>
The above example will output "hello!"

Leave a Reply

Subscribe to Posts | Subscribe to Comments

- Copyright © Learning Programming Language - Skyblue - Powered by Blogger - Designed by Johanes Djogan -