0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Variables and Data Types

Posted at

Introduction to PHP for Beginners.jpg


3. Variables and Data Types

Declaring Variables

In PHP, a variable is a container used to store data that can change during the execution of a script. All variables in PHP start with a dollar sign ($) followed by the variable name. A variable name can include letters, numbers, and underscores, but it must start with a letter or an underscore.

Here’s how you declare a variable:

<?php
  $name = "John"; // String variable
  $age = 25;      // Integer variable
  $isStudent = true; // Boolean variable
?>

In the example above:

  • $name is a variable that stores a string value.
  • $age is an integer variable.
  • $isStudent is a boolean variable (either true or false).

PHP Data Types

PHP supports several types of data. The most common ones are:

  1. String: A sequence of characters enclosed in quotes (either single ' or double ").

    • Example: $name = "John Doe";
  2. Integer: A whole number (positive or negative).

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

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

    • Example: $isValid = true;
  5. Array: A collection of values. You can store multiple items in a single variable.

    • Example:
      $colors = array("Red", "Green", "Blue");
      
  6. Object: A data structure that can contain both data and functions (methods). We will explore objects in later sections when we dive into object-oriented programming.

  7. NULL: A special data type that represents a variable with no value.

    • Example: $emptyVar = NULL;

Variable Naming Conventions

While naming variables, there are a few rules and conventions to keep in mind:

  • A variable name must start with a letter or underscore (_), followed by letters, numbers, or underscores.
  • Variable names are case-sensitive. For example, $Name and $name are different variables.
  • It's good practice to use descriptive names for your variables to make the code easier to understand.

Example of good variable names:

<?php
  $firstName = "John";
  $lastName = "Doe";
  $isStudent = true;
?>

Avoid using vague names like:

<?php
  $x = "John";
  $y = 25;
?>

Type Casting and Type Juggling

PHP automatically converts (or "casts") data from one type to another when necessary. This is called type juggling. For example, if you try to add a string and an integer, PHP will convert the string to a number if possible.

Example of type juggling:

<?php
  $num = "5"; // string
  $sum = $num + 10; // $num is automatically converted to an integer
  echo $sum; // Outputs: 15
?>

However, if you need to explicitly convert a variable from one type to another, you can use type casting:

<?php
  $str = "123.45";
  $int = (int) $str; // Cast string to integer
  echo $int; // Outputs: 123
?>

Constants

In PHP, constants are variables that cannot be changed once they are defined. You define a constant using the define() function.

Example:

<?php
  define("PI", 3.14159);
  echo PI; // Outputs: 3.14159
?>

Unlike variables, constants do not start with a dollar sign ($) and are case-sensitive by default.


Summary of Variables and Data Types:

  • PHP variables start with $ and can hold different types of data like strings, integers, floats, booleans, arrays, and objects.
  • PHP supports automatic type conversion (type juggling), but you can also explicitly cast data types.
  • Constants are similar to variables but cannot be changed once defined.

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?