ArchivesPHP and MySQLi Tutorials of

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: , , ,

PHP Tutorial: Introduction

In order to use PHP you will need three things:

  • Webserver (or webhost) with PHP installed
  • A text editor
  • A web browser
  • Optional: a database, such as MySQL

We’ll begin with getting in to PHP, so open up your text editor and enter the following (don’t copy and paste, you’ll learn slower!)


Your PHP code should always start and end with the above code. Although PHP has other tags that can be used such as <% %> (ASP tags) and (these are short tags), the ASP tags will not be working as of PHP6, and some people feel that the short tags are bad practice.

Ok back to the tutorial! Sometimes while you’re coding, you might want to leave yourself a note or two. You can accomplish this with comments. PHP has three types of comments, as displayed below.

#  The first comment type is a hash comment
#  anything after the hash, on the same line
#  will be ignored by PHP.

// the second type is the double forward slash comment
// anything after the double slashes, on the same line
//  will be ignored by PHP
/*  The third type is the multi-line comment
anything between the opener: / * (no space)
and the closer: * / (again, no space)
will be ignored by PHP */

And there you have it, some basic syntax to help you with your PHP development!

Hope to see you in the next tutorial.

What is PHP? What can PHP do?

What is PHP? Well according to the PHP Manual:

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

Riiiight, but what does that mean?  To put it simply, PHP can be placed inside of HTML web pages, allowing a seamless transition between the two languages.  To help clarify, here is an example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<title>PHP/MySQLi Tutorials</title>
What is PHP, you ask?  
<?php echo "PHP is a general purpose scripting language!"; ?>

This would output: “What is PHP, you ask? PHP is a general purpose scripting language!” to the browser.

“Ok, ok, I get it! But what can PHP actually do?”

I’m glad you asked. PHP can do just about anything any other language can do. Mind you, you won’t find PHP on the space station (yet…) but you will find it on the majority of web servers today.

PHP is not limited to simply outputting HTML to a browser, it can output images, PDFs, Flash videos. You can output numerous types of text, such as XHTML and XML too!

One of PHP’s biggest attractions is its long list of database extensions. These databases range from the common MySQL, to PostgreSQL, MSSQL and the list goes on.

Combining PHP’s database support, with its ability to output multiple types of text, creates endless possibilities!

By Mike on | PHP
Tags: , ,