Variables and Data Types - potatoscript/php GitHub Wiki

Variables and Data Types in PHP

Overview

In PHP, variables are used to store data that can be used later in your script. Each variable has a type that determines what kind of data it can store. PHP is a loosely typed language, which means you don’t need to explicitly declare the data type of a variable when you create it. PHP automatically converts the data type based on the value assigned to the variable.

In this section, we will cover the following:

  • How to declare and use variables
  • Different data types in PHP
  • Type casting and type checking

Declaring Variables

A variable in PHP is declared using the dollar sign ($) followed by the variable name. PHP variable names must begin with a letter or an underscore, followed by any combination of letters, numbers, or underscores.

Basic Syntax:

$variable_name = value;

Example:

<?php
$name = "Lucy";
$age = 25;
$isStudent = true;
echo $name; // Output: Lucy
?>

Variable Naming Rules:

  • A variable name must start with a letter or an underscore (_), followed by letters, numbers, or underscores.
  • Variable names are case-sensitive, so $name and $Name would be considered different variables.

Data Types in PHP

PHP has several built-in data types, which can be categorized as scalar types (used for single values) and compound types (used for multiple values).

Scalar Data Types:

  1. String: A string is a sequence of characters enclosed in single or double quotes.

    • Example:
      $greeting = "Hello, World!";
      
  2. Integer: An integer is a whole number, positive or negative, without a decimal point.

    • Example:
      $age = 25;
      
  3. Float (or Double): A float is a number with a decimal point.

    • Example:
      $price = 19.99;
      
  4. Boolean: A boolean value can either be true or false.

    • Example:
      $isStudent = true;
      

Compound Data Types:

  1. Array: An array stores multiple values in a single variable. PHP supports both indexed arrays (where the key is a number) and associative arrays (where the key is a string).

    • Example:
      // Indexed Array
      $colors = array("Red", "Green", "Blue");
      // Associative Array
      $person = array("name" => "Lucy", "age" => 25);
      
  2. Object: An object is an instance of a class, which can hold both data and methods (functions).

    • Example:
      class Car {
          public $make;
          public $model;
      
          function __construct($make, $model) {
              $this->make = $make;
              $this->model = $model;
          }
      
          function displayCar() {
              return "Car: $this->make $this->model";
          }
      }
      
      $car1 = new Car("Toyota", "Corolla");
      echo $car1->displayCar();  // Output: Car: Toyota Corolla
      
  3. NULL: A variable that has no value is considered NULL. It is different from an empty string or zero.

    • Example:
      $var = NULL;
      

Type Casting in PHP

Sometimes, you may want to explicitly change the type of a variable. This process is called type casting. PHP allows both implicit and explicit type casting.

Implicit Type Casting (Type Juggling):

PHP automatically converts between data types when necessary. For example, if you add an integer to a string, PHP will convert the integer to a string automatically.

Example:

<?php
$number = 5;
$text = "The number is " . $number;  // $number is automatically converted to a string
echo $text;  // Output: The number is 5
?>

Explicit Type Casting:

You can explicitly cast a variable to a different type using type casting operators. You can cast between data types such as integer, float, string, or boolean.

Example:

<?php
$var = "123.45";  // String
$var_int = (int)$var;  // Cast to integer
echo $var_int;  // Output: 123

$var_float = (float)$var;  // Cast to float
echo $var_float;  // Output: 123.45
?>

Type Checking in PHP

You can check the type of a variable using the gettype() function, which returns the type of a variable as a string. Additionally, PHP provides specific functions for checking certain data types, such as is_int(), is_float(), is_string(), and so on.

Example:

<?php
$var = 25;
echo gettype($var);  // Output: integer

if (is_int($var)) {
    echo "The variable is an integer.";  // Output: The variable is an integer.
}
?>

Common Type Checking Functions:

  • is_int($var) – Returns true if the variable is an integer.
  • is_float($var) – Returns true if the variable is a float.
  • is_string($var) – Returns true if the variable is a string.
  • is_array($var) – Returns true if the variable is an array.
  • is_object($var) – Returns true if the variable is an object.
  • is_null($var) – Returns true if the variable is NULL.

Conclusion

Understanding variables and data types is essential for working with PHP. In this section, you learned how to declare variables, the different data types available in PHP, how to perform type casting, and how to check the types of variables.

In the next section, we will explore more advanced topics like operators, control structures, and working with arrays in PHP.