PHP: Variables & Strings

In this tutorial I will explain:
•What a Variable and String are…
•The purpose of a Variable and a String…
•And an Example of a Variable and a String.

A Variable is a place holder that holds a value (I.E. “John” or 42). Once you set a Variable, you can use it over and over in your PHP script. Every single variable in PHP starts with a $ sign. The correct way of setting (Declaring) a variable in PHP is exampled in the following script:

  1. <?php
  2. $variable_name = value;
  3. ?>

There are multiple datatypes for a value. A string is a multiple series of characters: “John Doe”. Strings will always be in quotation marks. It is very easy to output a string in PHP, a command called ‘echo’:

  1. <?php
  2. /* This is an example for the ‘echo’ command */
  3. /* Display the text in the quotation marks */
  4. echo "This is a String.";
  5. ?>

An Example of the use of a variable and a string is shown below:

  1. <?php
  2. /* set the variables */
  3. $name = "John Doe";
  4. $age = "32";
  5. echo "Hello $name, you are $age years old.";
  6. ?>

The output of this script would display as the following:
John Doe you are 32 years old.

Leave a comment

Name: (Required)

eMail: (Required)

Website:

Comment: