PHP Pandas: Strings

← Back to Index

We've encountered strings already, haven't we? Well, I think they are a really interesting data type. That's why I've chosen to dedicate a short chapter to them.

Interpolation

Strings are the name that programmers have given to sequences of text held within the variables of our applications. Let's assign a string to a variable for some catch-up.

<?php

// String with single quotes.
$panda = 'Pandas rule!';

// String with double quotes.
$panda = "Pandas rule!";

// Output.
echo $panda;

Okay, so I know the second line is pointless. I left it in there to remind you of the two different types of quotes that can be used to enclose string values.

Right then, if we have two options, then what's the difference? It can't just be for clarity, right? I mean, they both look equally clean to me.

Well, there is actually a difference. String values that are enclosed in double quotes are a little smarter. They have a trick that their single-quoted brethren just aren't able to do. It's called String interpolation.

That's a horrible term, isn't it? It's very confusing. It simply means that we can embed values within a string. Let's take a look at this in action.

<?php

// Set a string.
$value = 'pandas';

// Single quotes.
$first = 'We love $value!';

// Double quotes.
$second = "We love $value!";

// Output.
var_dump($first);
var_dump($second);

We'll try inserting a variable by name into both types of strings. Let's take a look at the result of dumping out both types of strings.

string(15) "We love $value!"
string(15) "We love pandas!"

As you can see, with single quotes the name of the variable is presented as if it were simply part of the string. However, within the double quoted string, our variable name has been replaced with the value of the variable.

Woah! That's super useful. Isn't it?

I want to offer you a little advice. It's best to wrap interpolated values within { curly braces }. It makes the interpolation much cleaner, and you'll find it will work better when you start using arrays.

Here's an example.

<?php

// Set a string.
$value = 'pandas';

// Insert value.
$result = "We love {$value}!";

// Output.
var_dump($result);

Much cleaner, isn't it? Stick with the curly braces. Trust me, you will grow to love them.

Concatenation

Concatenation is the process of joining two strings end to end. Like creating a bead necklace, or the human centipede... oh my. Let's take a look at an example of this process in action.

<?php

// First value.
$first = 'Pandas are';

// Second value.
$second = ' awesome!';

// Concatenate.
var_dump($first . $second);

First we create two string values, and then we output the result of concatenation. In PHP we use the period . character to perform concatenation. Let's take a look at the result, shall we?

Pandas are awesome!

Great! Our strings have been stuck together. We can concatenate as many values as we like. Here's an example.

<?php

// First value.
$first = 'Pandas';

// Second value.
$second = ' are';

// Third value.
$third = ' completely';

// Fourth value.
$fourth = ' awesome!';

// Concatenate.
var_dump($first . $second . $third . $fourth);

We can also concatenate different types of variables together; PHP will simply treat them as strings. To prove this, let's concatenate a string and a float value together.

<?php

// First value.
$first = 'Value of: ';

// Second value.
$second = 27.325;

// Concatenate.
var_dump($first . $second);

It's a similar example to what we've seen previously. So, what's the result?

string(16) "Value of: 27.325"

Just as we expected! The float value has been converted to a string, and stuck to the end of the other value. The same would happen for all basic data types.

I've got a question. Actually, I know the answer... it's just that sometimes I find asking questions is a great way to get that brain of yours ticking!

Why can't we use the addition operator to concatenate strings? I mean, we're just adding two strings together, aren't we?

In other languages, Javascript for example, you can use + to concatenate strings. In fact, it's the most accepted way to do so. In PHP, the addition operator is purely mathematical. Let's see what happens if we attempt to perform addition on two strings.

<?php

// First value.
$first = 'Pandas are ';

// Second value.
$second = 'awesome!';

// Concatenate.
var_dump($first + $second);

What's the result? It's nothing impressive!

int(0)

PHP understands that our strings are complicated, and so, it treats them as the integer value of zero. Two zeroes equal zero.

If our strings represented real numbers in string format, then PHP would be able to do something a little more sensible with them. Let's try it!

<?php

// First value.
$first = '3';

// Second value.
$second = '5';

// Concatenate.
var_dump($first + $second);

This time, the result makes more sense to us.

int(8)

We call the process of transforming from one type to another 'casting'. The casting of strings to numeric values within mathematical operations is either convenient or dangerous, depending on your circumstance. It's convenient that we don't have to cast these values ourselves, but sometimes casting doesn't give us the value we expect. For example, three won't cast to 3, it will be 0. Be sure to use caution when using strings within mathematical operations.

PHP comes with a number of functions, little machines that do work for us, that can be used to perform a number of operations on strings. We can reverse them, replace sections of them, extract sub-strings, calculate length and much, much more. There are more string functions than I can count. Don't worry. We'll be looking at functions in a later chapter. For now, let's take a look at the 'Array' data type in the next chapter.

My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.

It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!