Who's Online
We have 7 guests onlineBreadcrumbs
| Strings |
PHP String VariablesUntil now,we used strings a bit, but didn't talk about them in depth. Throughout your PHP career you will be using strings a great deal, so it is important to have a basic understanding of PHP strings. Example - PHP String <?php PHP Strings Operators
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 Code: <?php 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. Code: <?php 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
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 Code: <?php Notice the apostrophe. It's a special character which we didn't need to escape in double quotes.
Example - String in Single Quotes Code: <?php
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 Code: <?php 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 Code: <?php
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 Code: <?php 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. Code: <?php In the above example we escaped the apostrophe using a backslash like this \'.
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. Code: <?php 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. Code: <?php 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.Code: <?php The above example will output "HELLO!"
Example - ucfirst() Function This function changes the first letter in the string to upper case. Code: <?php The above example will output "Hello!"
Example - trim() Function This function removes whitespace from the beginning and from the end of the string. Code: <?php PHP String FunctionsPHP: indicates the earliest version of PHP that supports the function.
|