PHP Variables

In any programming or scripting language, a variable is a named location in memory that holds the value of a number, character, text string, or complex data type. In PHP, all variables are prefixed by a dollar sign ($). The syntax for declaring a variable in PHP is shown below.


$var_name = value;


PHP is said to be a loosely typed language, because the data type of a variable does not have to be specified before a value is assigned to it. A value can be assigned to a variable when it is created, and PHP automatically selects the correct data type for the variable according to the value assigned. The naming rules for PHP variables are simple. A variable name must start with either a letter or an underscore character ("_"), and may only contain alpha-numeric characters (a-z, A-Z, and 0-9) and the underscore character. Variable names should not contain spaces, and if a variable name consists of two (or more) words, they should either be separated by an underscore, or the first letter in each new word should be an upper case character, as shown below.


$some_variable;
$anotherVariable;


String variables are variables that contain two or more alpha-numeric characters or other printable characters. They are mainly used to store and manipulate text. Two commonly used string-handling functions are described below.

The following simple PHP script uses both numeric and string variables, and demonstrates the use of the strlen() and strpos() functions.


<?php
  // declare and initialise a couple of string variables
  $myText1="Hello World!";
  $myText2="Have a nice day!";

  // output the string values to the screen on different lines
  echo "The first string is: '";
  echo $myText1;
  echo "'<br><br>";
  echo "The second string is: '";
  echo $myText2;
  echo "'<br><br>";

  // declare and initialise some numeric variables
  $posWorld = strpos($myText1, "World");
  $lngMyTxt1 = strlen($myText1);
  $lngMyTxt2 = strlen($myText2);

  // display the values of the string-length numeric variables
  echo "The number of characters in the first string is ";
  echo $lngMyTxt1;
  echo "<br><br>";
  echo "The number of characters in the second string is ";
  echo $lngMyTxt2;
  echo "<br><br>";

  /* display the starting position of the word "World"
  within the first string */
  echo "The word ‘World’ starts at position ";
  echo $posWorld + 1;
  echo " in the first string.<br><br>";

  // demonstrate the use of the concatenation operator
  echo "We can concatenate the strings:<br><br>";
  echo ($myText1) . " " . $myText2 . "\n";
?>


Note that the concatenation operator (.) is the only PHP operator that specifically relates to string variables, and is used to join (concatenate) two strings together. We have used the concatenation operator to join the first and second string variables together, with a blank space (" ") in between them. Enter the code above into a text editor and save it with the filename "variables.php" in the "htdocs" directory of your "xmapp" directory. Run the XMAPP server, then type the following URL into the browser's address bar:


http://localhost/variables.php


You should see output that is something like the screenshot below.


The output from variables.php

The output from variables.php