Core PHP

Lesson 2: Using Variables

Overview

The purpose of this lesson is to explain how variables are used in PHP. PHP variable names are always preceded by a dollar sign. Variables are not strongly typed, as they are in many other languages. In fact, the type of a variable can be changed at any time to any other type. To help understand how to use variables, PHP provides several functions that are employed in this lesson: gettype(), print_r(), var_dump(), and get_defined_vars().

Variable Names

Here we have an HTML page with two lines of PHP inserted into it. The first line create a varaible named $Stuff. Notice that the first character of the line is a dollar sign. In PHP, a dollar sign always precedes the name of a variable. The dollar sign is used whereever the variable name is invoked, as the code shows.

The second line contians an echo statement that is used to print out the variable's name and its value. Notice that the first instance of the name is inside the quotation marks, and the second is betweeen two quotations. Since the second instance is outside the quotation marks, it is evaluated and its value is printed as a string. The periods on either side signal that the strings are to be concatenated together as one string. This is what is being echoed.

Prog1.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
  $Stuff = 14641;
  echo '<p>$Stuff = '.$Stuff.'</p>';
?>
  </body>
</html>
 
 

Output for Prog1.php

 

Note that each line ends with a semicolon (;) character. This is the case for all PHP lines. In fact, a line of code may stretch over several lines of text before it reaches a semicolon and ends the line of code. Whitespace characters are ignored. So, the program below, where the first line is split into three, does exactly the same thing as the first program.

Prog1a.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
  $Stuff
  =
  14641;
  echo '<p>$Stuff = '.$Stuff.'</p>';
?>
  </body>
</html>
 
 

Output for Prog1a.php

 

Finally, note that PHP variable names are case-sensitive, even though PHP commands are not. So, in the program below, the variables $Stuff and $stuff are different variables, but the commands echo and EcHo are not. So, the effect of the code below is that the first variable $Stuff and its value are printed twice.

Prog2.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
  $Stuff = 14641;
  echo '<p>$Stuff = '.$Stuff.'</p>';
  $stuff = 121;
  EcHo '<p>$Stuff = '.$Stuff.'</p>';
?>
  </body>
</html>
 
 

Output for Prog2.php

 

Variable Types

If you are used to other programming languages, then you might be used to strongly-type variables. Strongly-typed variables retained their type throughout the entire program. In PHP, variables may change their type repeatedly, as well as their value. In the program below, the variable is assigned the value "XoaX.net" and then 14641. The first value is a string and the second is an integer, as the program indicates. The gettype() function allows us to check the type of a variable.

Prog3.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
  $Stuff = "XoaX.net";
  echo '<p>$Stuff = '.$Stuff.'</p>';
  echo '<p>Type of $Stuff = '.gettype($Stuff).'</p>';
  $Stuff = 14641;
  echo '<p>$Stuff = '.$Stuff.'</p>';
  echo '<p>Type of $Stuff = '.gettype($Stuff).'</p>';
?>
  </body>
</html>
 
 

Output for Prog3.php

 

Printing Variables

PHP provides us with two functions to print the value of a variable: print_r() and var_dump(). As the output indicates, the function print_r() only tells us the value of the variable, but the function var_dump() tells us the type as well as the value.

Prog4.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
  $sName = "XoaX.net";
  $iPrime = 19;

  echo "<pre>";
  print_r($sName);
  echo "\n";
  print_r($iPrime);
  echo "</pre>";

  echo "<pre>";
  var_dump($sName);
  var_dump($iPrime);
  echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog4.php

 

Checking Variables

PHP provides us with a function to list all of the currently defined variables: get_defined_vars(). The variables are returned as an array. Here, the print_r() and var_dump() functions are used to print the variables. Notice that there are four additional variables: _GET, _POST, _COOKIE, and _FILES. These are four variables that are always defined; they will be explained later.

Prog5.php

<!DOCTYPE html>
<html>
  <head>
    <title>XoaX.net PHP</title>
  </head>
  <body>
<?php
  $sName = "XoaX.net";
  $iPrime = 19;

  echo "<pre>";
  print_r(get_defined_vars());
  echo "</pre>";

  echo "<pre>";
  var_dump(get_defined_vars());
  echo "</pre>";
?>
  </body>
</html>
 
 

Output for Prog5.php

Array
(
    [_GET] => Array
        (
        )

    [_POST] => Array
        (
        )

    [_COOKIE] => Array
        (
        )

    [_FILES] => Array
        (
        )

    [sName] => XoaX.net
    [iPrime] => 19
)
array(6) {
  ["_GET"]=>
  array(0) {
  }
  ["_POST"]=>
  array(0) {
  }
  ["_COOKIE"]=>
  array(0) {
  }
  ["_FILES"]=>
  array(0) {
  }
  ["sName"]=>
  string(8) "XoaX.net"
  ["iPrime"]=>
  int(19)
}
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.