Web Development Tutorials




  
  

Scalar Variables

Types of Variables

Variables are temporary place holders used to store values used in a PHP script. PHP includes two main types of variables: scalar and array. Scalar variables contain only one value at a time, and array variables contain a list of values. Array variables are discussed in the next section. PHP scalar variables contain values of the following types:

  • Integers: whole numbers or numbers without decimals. (e.g., 1, 999, 325812841)
  • Floating-point numbers (also known as floats or doubles): numbers that contain decimals. (e.g., 1.11, 2.5, .44)
  • Strings : text or numeric information. String data is always specified with quotes (e.g., "Hello World", "478-477-5555")
  • Boolean: used for true or false values

Creating Variables

PHP variables of all types begin with the "$" sign. Variable names can contain letters, numbers, and the (_) underscore character. Variables cannot, however, begin with numbers. In PHP, variable names are case sensitive. The following variables are interpreted as two different variables:

  • $myVar
  • $MYVAR

Legal Variable Names

  • $myVar
  • $F_Name
  • $address1
  • $my_string_variable

Illegal Variable Names

  • Myvar – the $ sign is not specified
  • $1stvar – cannot start with a number
  • $&62## - special characters except (_) underscore cannot be used

PHP scalar variables are assigned a single value in the following format:

  • $username = "jdoe";
  • $first_name = "John";
  • $Last_Name = "Doe";

The variable username contains the value "jdoe".

PHP is considered a loosely typed language. This means that variables do not have to be explicitly declared as a specific type such as string, integer, boolean, or array. The variable's data type is automatically set based on the value assigned to the variable. For example, when a variable is assigned a value surrounded by quotes (e.g., "Hello World"), it is set to a string data type. A variable is given an integer data type when a numeric value (e.g., 5 or 100) is assigned to it. Finally, assigning the value 'true' or 'false' to the variable gives it a boolean type.

Displaying Variables

The following code segment demonstrates how to declare a scalar variable, assign the scalar variable a value, and display the results in the browser window:


<!DOCTYPE html>
< html > 
    <head>
 <title>A Web Page</title>
</head>
<body>
    <p>
 <?php
    $string_var = "My PHP program";
    $integer_var = 500;
    $float_var = 2.25;

    echo $string_var;
    echo $integer_var;
    echo $float_var;
 ?>
</p>
</body>
</html>

            

variable can be concatenated or joined together with other variables or HTML tags using the PHP dot (.) operator. In the above example PHP code, the output will be displayed in the following format:

   My PHP program5002.25 
            

To create a carriage return or line break, you can concatenate the HTML tag to the end of each variable:

<!DOCTYPE html>
<html>
    <head>
<title>A Web Page</title>
</head>
<body>
    <p>
  <?php
       $string_var = "My PHP program" . "<br>";
       $integer_var = 500 . "<br>";
       $float_var = 2.25 . "<br>";

       echo $string_var;
      echo $integer_var;
       echo $float_var;
  ?>
 </p>
</body>
</html>
            

Now, a line break will be inserted after each variable, causing each value to be rendered on a separate line:

   My PHP program
   500
   2.25 
    

Variable Concatenation

The dot operator (.) can also be used to join strings and variables. In the example below, the message, "The user's name is Joe Doe", is displayed in the browser window. The string, "The user's name is ", is concatenated to the value of $fname, which is "John", followed by a blank space, " ", and the value of $lname, which is "Doe".

<!DOCTYPE html>
<html>
<head>
  <title>A Web Page</title>
</head>
<body>
 <p>
  <?php
      $fname = "John";
      $lname = "Doe";

       echo "The user's name is " . $fname . " " . $lname;
  ?>
 </p>
</body>
</html>
   The user's name is John Doe  
            

Interpolation

PHP also supports a process known as interpolation - replacing a variable with its contents within a string. Instead of concatenating variables and literals, they can be combined inside of double quotes (" "). Interpolation is a feature of double quotes only. Variables and literals cannot be combined inside of single quotes (' '). With double quotes, the variable's value is displayed along with the literal. With single quotes, the variable name is "literally" displayed along with the rest of the string. The following example illustrates PHP's interpolation feature:

<!DOCTYPE html>
<html>
<head>
  <title>A Web Page</title>
</head>
<body>
 <p>
  <?php 
       $fname = "John";
       $lname = "Doe";
    
       //$fname and $lname will be replaced with "John" and "Doe" respectively
       echo "The user's name is $fname  $lname";
  ?>
 </p>
</body>
</html>

This code produces the same output as the previous example. Here the variables are combined with the literal strings surrounded by double quotes. Concatenation is not required.

The isset() Function

When working with variables, it is often necessary to check that the variable is set and not NULL. A NULL variable is one in which no value has been assigned. PHP includes the built-in isset() function that can be used to check the status of a variable. The format of the function is shown below:

isset(variable) - returns TRUE if variable exists. Otherwise, FALSE is returned

The following code demonstrates the use of the isset() function:

<?php

$variable_one = "Hello World";
$variable_two;

var_dump(isset($variable_one)); //RETURNS TRUE
var_dump(isset($variable_two)); // RETURNS FALSE


?>

In the previous example, two variables are declared, but only one variable,$variable_one, has been set. The special PHP function var_dump() is used to provide information about the variable. The first statement returns the boolean (bool) value of TRUE while the second statement returns the boolean (bool) value of FALSE. The isset() function is very useful and will used extensively in upcoming sections.

The var_dump() Function

The var_dump() function used in the previous section can be used in any case where information about a variable is needed. The function "dumps" or returns the variables type and value. This function can be used in error handling and troubleshooting situations.

<?php

$variable_one = "Hello World";
$variable_two;

var_dump($variable_one); //RETURNS string(11)
var_dump($variable_two); // RETURNS NULL


?>

In the previous example, the var_dump() function is used with two variables. The first statement returns string(11), indicating that $variable_one contains a string value with a length of 10. The second statement returns a NULL, indicating that the variable $variable_two contains no value or has not been set to any value yet.

Variable Scope

Variable scope refers to the context in which the variable was declared or defined. Variables are considered valid or useable only within the scope in which they are declared. PHP supports four types of variable scopes:

  • global
  • static
  • parameter
  • local

In most cases, variables declared outside of a PHP function are global. This means that the variables can be accessible or used anywhere within the PHP page except inside of a function. Static, parameter, and local variables are associated with a function and they will be discussed later in conjunction with the function later.


TOP | NEXT: Array Variables