PHP Strings

What do you use your Strings for?

I know that for the past few articles we’ve looked at strings, but we never actually talked about them. A string is just a sequence of characters surrounded by quotes; strings in PHP are placed inside either single or double quotes. In other programming languages, like Java, strings are surrounded by double quotes and individual characters are surrounded by single quotes.

 

<?php$string_1 = 'This is an acceptable string';
$string_2 = "String with double quotes";echo $string_1 . "<br>";
echo $string_2 . "<br>";?>

 

You don’t have to store PHP Strings in variables in order to output them to the screen. You can just echo them out.

 

<?phpecho "I'm a string that's directly echoed out.";?>

 

We can see from the example above that PHP only evaluates the outer double quotes. We have single quotes inside the echo statement. Those single quotes are surrounded by double quotes so no error is thrown. If we surrounded the overall string with single quotes, we would have an issue.

 

<?php// Causes an error
echo 'I'm a string that's directly echoed out';?>

 

The problem with that code is that PHP interpreter starts looking at the code and says:

  1. I see the echo statement.
  2. Look for an open quote, variable, constant… (or whatever else is acceptable).
  3. Found the open quote and it’s a single quote. I need to look for a closing single quote to end this statement.
  4. Found the single quote right after the first character. I need to look for the semi-colon.
  5. Found something other than the semi-colon. Throw error.

If you insist on having the single quotes surrounding the overall string, and use them inside the string itself, than you will have to “escape” the single quote character. A problematic character can be escaped with a backslash \.

 

<?php// Won't cause errors
echo 'I\'m a string that\'s directly echoed out';?>

 

Most of the time it’s much easier if you just mix and match.

  1. If you’re going to be using single quotes only inside the string, surround it with double quotes.
  2. If you’re going to be using double quotes only inside the string, surround it with single quotes.
  3. If you’re using both single and double quotes inside the string, surround it with whatever you find most appropriate. You will need to escape either the single or the double quotes inside the string.

 

<?phpecho "I'm a single quote inside the double quotes.";
echo 'Jeff said "how is it going?"';
echo "Jeff's wife said, \"I'm good.\"";?>

 

The big difference between single and double quotes starts when you look at evaluating variables.

 

<?php$name = "Dino Cajic";echo "Hi, my name is $name";?>

 

If you run the code, you will see the following output on your screen:

Hi, my name is Dino Cajic

With single quotes, it comes out a little different.

 

<?php$name = "Dino Cajic";echo 'Hi, my name is $name';?>

 

The output will be:

Hi, my name is $name

If you really wanted to use single quotes, you can concatenate (join) the variable with the concatenation (dot) operator. Or you can use a separate echo statement. We’re going to look at the concatenation operator in more detail a little later.

 

<?php$name = "Dino Cajic";echo 'Hi, my name is ' . $name;// or even use a separate echo statement
echo 'Hi, my name is ';
echo $name;?>

 

We don’t have to store strings inside of our variables. We can store other data types, like integers. Once added to the string, PHP will treat the echo statement as outputting one big string.

 

<?php$price = 100000;echo "The price of that car is $price";?>

 

The output will show: The price of that car is 100000

What if you wanted to add a dollar sign in front of it? That’s actually pretty simple. Just go ahead and add it.

 

<?php$price = 100000;echo "The price of that car is $$price";?>

 

The output will be: The price of that car is $100000

There is one weird thing and we’re going to look at this in greater detail later, but I did want to introduce it. You can add a dollar sign to a string and it will evaluate it as a variable. Also, if you add a dollar sign to a variable that’s holding a string, and surround it with a couple of curly braces, it will evaluate the variable inside the curly braces and then treat the string appended to the dollar sign as a variable as well. A lot of words, I know, but let’s just jump into an example and walk through it.

 

<?php$price = 100000;
$x = "price";echo "That car is selling for ${$x}";?>

 

If you run the code, it will display:

That car is selling for 100000

What just happened?

  1. PHP looked at the echo statement.
  2. It found a dollar sign inside the double quotes.
  3. It saw that right after the dollar sign symbol was an opening curly brace.
  4. It saw that inside the curly braces was the variable $x.
  5. It evaluated the variable. It came out to a string “price.”
  6. It appended the “price” string to the outer dollar sign and created a new variable $price.
  7. It evaluated $price, which turned out to be the integer 100000.
  8. It outputted the entire string to the screen.

You wouldn’t think that this article would be this long on just strings did you? Well, there’s more.

We looked at escaping problematic characters. But, there are characters that when they’re prepended with the backslash have some new functionality. Think about the character n. By itself it has no special powers. But, if you prepend it with backslash, \n, all of a sudden it acts like a new line character.

 

<?phpecho "First Line\nSecondLine";?>

 

The output will be:

First Line
Second Line

PHP will also interpret HTML characters.

 

<?phpecho "<div>First Div</div><div>Second Div</div>";?>

 

When you look at the output, you will notice that the div tags are hidden behind the scenes and you just see the two strings, one below the other.

First Div

Second Div

I think that I’m finally satisfied talking about strings in PHP. See you next time.

 

PHP Tutorial Series

Continue your learning with these articles

PHP Floats - Doubles

Time to take a dip in the Pool. Where’s my Float?

PHP – P7: FLOATS/DOUBLES

Floats and doubles are the same thing in PHP. Floating points in PHP have precision for up to 14 digits. That includes the cumulative digits, before and after the period.

PHP Strings

What do you use your Strings for?

PHP – P8: Strings

A string is just a sequence of characters surrounded by quotes; strings in PHP are placed inside either single or double quotes. In other programming languages, like Java, strings are surrounded by double quotes and individual characters are surrounded by single quotes.

PHP Simple Arrays

ONCE YOU UNDERSTAND THEM, THEY REALLY DO BECOME SIMPLE.

PHP — P9: SIMPLE ARRAYS

What is an array? In layman’s terms, it’s a way to store multiple items together under one variable. You could create different variables for everything, but sometimes that’s just not practical.

Leave a Reply