PHP Tutorial: Strings and Variables

PHP Tutorial: Strings and Variables

In PHP there are two things you will often use, variables and strings. A variable is an item in your program that can or will change often, hence it is variable. A string is text, typically used when outputting to the browser. When out putting to the browser, you would typically use the echo command. Let’s try that:

/* let's try echoing a string
and see what it does */
echo "Hello, this is a string, and I am using PHP.<br />";
/* let's try another one, just for good measure
what do you say? */
echo 'Hello, this is my second attempt at a string, using PHP.';

Great job! “But Mike, why did you use double quotes in your first string, and single quotes in your second string?” Good question. I did this simply to show you that PHP allows both types of quotes to contain strings. However, they do have some differences.

  • Double quotes: allow you to put variables directly inside, without having to concatenate your string.
  • Single quotes: do not allow variables to be placed directly inside, they will echo the text as literally as possible.

Note: If you string contains quotes that are the same as the ones you use to encase your string, you must escape these characters, using the escape character \ (backslash). For example: echo ‘You can\’t do this.’;

Let’s try escaping a few things.

echo "The name of this website is \"PHP and MySQLi tutorials\", isn't it?";
echo 'You can\'t use \' inside \', without escaping them';

Let’s move on to variables. All variables in PHP start with a dollar sign, $, and are CaSE-SeNSitiVE. They can contain letters (capital or small), numbers (0-9) and underscores ( _ ). They must, however, start with a letter or an underscore, they can’t start with a number. The next part of variables in PHP is the assignment operator, which is a single equals sign ( = ). Now, don’t start thinking of this as equals, think of it more as the left operand (typically a variable) “gets set to” the expression(s) on the right. Let’s try that:

/* so remember, all variables start with a $
then a letter or an underscore, then any other combination */
$variable = 'This is my first variable, it contains a string.';
$Math = 1+1;
$9badname = 'This will fail';

The last variable in that example will not work, because it’s name begins with a number.

Variables in PHP can contain 8 data types, they are as follows:

  • Strings (“This is a string”)
  • Integers (whole numbers)
  • Floating Point Numbers (numbers with a decimal place, 124.356)
  • Booleans (TRUE/FALSE)
  • Arrays (an ordered map)
  • Resources (holds a reference to an external resource)
  • NULL (a value that represents nothing)
  • Objects (a particular instance of a class)

We’ll start with the simple ones, Strings, Integers, and Floating Point numbers.

/* Let's set a variable to have a value of a string */
$string = 'This is a string value.';
/* Let's set a variable to have a value of an integer */
$integer = 2011;
/* Let's set a variable to have a value of a floating point number */
$float = 2.011;

Note: Notice that when setting the integer and the floating point variables, there are no quotes required.

Now let’s try putting all that we’ve learned together in one big finale, shall we? OK!

/* we'll begin by setting a string variable */
$message = 'We have learned a lot in this tutorial.<br />';
/* now an integer */
$types = 3;
/* and a float */
$understood = 0.1;
/* now we can echo all those variables, in any order we want
let's try and make a nifty sentence. */
echo $message;
echo "We've learned about $types data types, and we've retained $understood percent of it.";
/* this should output:
We have learned a lot in this tutorial.
We've learned about 3 data types, and we've retained 0.1 percent of it.

Thanks for reading! Hope to see you in the next tutorial.

By Mike on March 22, 2011 | PHP, Tutorials
Tags: , , ,