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:
-
<?php
-
$variable_name = value;
-
?>
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’:
-
<?php
-
/* This is an example for the ‘echo’ command */
-
/* Display the text in the quotation marks */
-
echo "This is a String.";
-
?>
An Example of the use of a variable and a string is shown below:
-
<?php
-
/* set the variables */
-
$name = "John Doe";
-
$age = "32";
-
echo "Hello $name, you are $age years old.";
-
?>
The output of this script would display as the following:
John Doe you are 32 years old.
























