PHP Strings

In PHP, strings can be single or double quoted. A string is a data type, and everything you output to the browser will be a string; thus, you need to know how to use and format strings, because you will be working with strings in some way most of the time.

Echo and Print

PHP provides two commands you can use to print out text to the browser: print or echo. They are similar, so it’s best to choose one and stick with it.

Example

<?php echo 'Welcome to thecodingguys, and your php tutorial!'; ?>

Output

Welcome to thecodingguys, and your php tutorial!

The Escape Character

At times it is necessary to escape certain characters, for example:

<?php echo ' That's Amazing! '; ?>

PHP is going to think that the line terminates at the word ‘That’ but in fact it does not, so we use a backslash:

<?php echo ' That\'s Amazing! '; ?>

The backslash is the escape character.

Concatenation

Concatenation is linking things together. For example, you might want to link “Hello World!” or a value from a variable. For this you use the dot (.); the dot is the concatenation operator.

Example

<?php

$age = 18;

echo ' Asim is ' .$age .' years old ' ;

?>

Here we used the concatenation operator, which can be confusing at first.

  • First we declared a variable named ‘age’
  • Then we printed out ‘Asim is ‘
  • We closed off that line and then used the concatenation operator, and linked the variable age. We did this by using two dots: one at the beginning and one at the end.
  • Then we continued the line with ‘years old’

The best way to get this syntax correct is first to write your sentence and then concatenate:

echo ‘ Asim is ‘ ‘ years old’;

Then in between just concatenate (.$age.). This method is quite easy, however another method is using double quotes.

Using double quotes looks like this:

echo "Asim is $age years old";

This is an easy method of concatenation; however if you were to use HTML then you would have to use single quotes for your HTML.

Single or Double Quotes?

PHP allows you to use single or double quotes: with double quotes you can easily concatenate variables, while with single quotes you cannot. If you use double quotes and add HTML, you either need to escape the HTML double quotes or use single quotes, so it is best to choose the one you prefer and stick with it.

String Functions

PHP provides many built-in functions which you can use to manipulate strings. In the following examples we will show the common ways to manipulate strings.

In this example we have a variable called $message and set its value to “Welcome to thecodingguys!”:

$message = "Welcome to thecodingguys!";

Strlen

The strlen( ) function returns the length of a string. It takes one parameter: the variable name.

Example

echo strlen($message); // Outputs 25

Substr

The substr( ) function can be used to return certain parts of a string. For example, you might only want to return the first 5 or 6 characters. It takes 3 parameters: the variable name, starting point and end point. The end point is optional, and if you do not specify it, the whole string will be printed out.

Example

echo substr($message, 0, 7); // Outputs Welcome

Strtoupper and Strtolower

The strtoupper and strtolower convert the case of a string. It takes one parameter: the string.

Example

echo strtolower($message); //welcome to thecodingguys!
echo strtoupper ($message);  // Outputs WELCOME TO THECODINGGUYS!

Summary

This tutorial covered how to print text to the browser, how to use the escape character and addressed the different functions.

  • Strings are either enclosed in single or double quotes
  • Strings are a data type
  • The escape character is \
  • To find the length of a string use strlen( )