Variable Types in PHP

Using a variable is the main way to store information in the middle of a PHP program.

Here are the most important things about PHP variables that you need to know.

  • In PHP, a dollar sign ($) comes before every variable name.
  • A variable’s value is the value of the last thing that was assigned to it.
  • The = operator is used to give a value to a variable. The variable goes on the left and the expression to be evaluated goes on the right.
  • Variables can be declared before they are used, but they don’t have to be.
  • In PHP, variables don’t have intrinsic types. This means that a variable doesn’t know ahead of time if it will be used to store a number or a string of characters.
  • Variables that are used without being given a value have default values.
  • PHP does a good job of automatically switching between types when it’s needed.
  • PHP variables are Perl-like.

PHP has a total of eight different types of data that we can use to make variables.

  • Integers are whole numbers, like 4195, that don’t have a decimal point.
  • Doubles are numbers with a decimal point that can change, like 3.14159 or 49.1.
  • Booleans can only have true or false as their values.
  • NULL is a special type that can only be set to NULL.
  • Strings are groups of characters, such as the phrase “PHP supports string operations.”
  • Arrays are groups of values that have a name and an index.
  • Objects are instances of classes that were made by the programmer. Objects can hold both other types of values and functions that are unique to the class.
  • Resources are special variables that point to things outside of PHP (such as database connections).

The first five are simple types, and the next two (arrays and objects) are compound types. The difference between simple and compound types is that compound types can hold other values of any type, while simple types cannot.

In this blog , we will only talk about simple data types. We will talk about Arrays and Objects separately coming blogs.

Integers

They are whole numbers, like 123 , with no decimal point. They are the easiest to understand. they are both positive and negative simple whole numbers. Numeric values can be given to variables or used in expressions, like this:

$int_var = 54321;
$another_int = -54321+ 54321;

 

Decimal (base 10), octal (base 8), and hexadecimal (base 16) formats are all possible for integers. Hexadecimals have a leading 0x, octal numbers are stated with a leading 0, and decimal format is the default.

Doubles

They enjoy 49.1 or 3.14159. Doubles print with the bare minimum of decimal places by default. For instance, the code

<?php
$many = 2.2888800;
$many_2 = 2.2111200;
$few = $many + $many_2;

print(“$many + $many_2 = $few <br>”);
?>

 

It gives the following result −

2.28888 + 2.21112 = 4.5

 

Boolean

They can only take one of two potential values: true or false. TRUE and FALSE, two constants that PHP offers specifically for usage as Booleans, can be used as follows:

if (TRUE)
print(“This will always print<br>”);

else
print(“This will never print<br>”);

converting different kinds to Booleans

Here are the guidelines for figuring out the “truth” of any value that isn’t already a Boolean value:

  • If the value is a number, it is true unless it is exactly equal to zero in which case it is false.
  • When the value is a string, the value is false if the string is empty (has no characters), otherwise it is true.
  • Type NULL values are always false.
  • If the value is an array, it is false if it is empty and true if it contains additional values. A member variable that has been given a value signifies that an object contains a value.
  • True sources are reliable (although some functions that return resources when they are successful will return FALSE when unsuccessful).
  • Avoid using double as a Boolean.

When used in a Boolean context, each of the following variables gets the truth value prefixed to its name.

$true_num = 5 + 0.51589;
$true_str = “Tried and true”
$true_array[49] = “An array element”;
$false_array = array();
$false_null = NULL;
$false_num = 777- 777;
$false_str = “”;

 

NULL

A unique type called NULL only has the word “NULL” as a value. Simply assign a value of NULL to a variable by writing it out as follows:

$my_var = NULL;

By tradition, the special constant NULL is capitalised, but in reality, it doesn’t care about case; you could have typed instead.

$my_var = null;

The characteristics of a variable that has been set to NULL are as follows:

In a Boolean context, it is evaluated to FALSE.

When tested with the IsSet() function, it returns FALSE.

Strings

They consist of character groups, such as “PHP supports string operations.” The following are appropriate string samples.

$string_1 = “string with double quotes”;
$string_3 = ‘This is a somewhat longer, text  string’;
$string_49 = “This string has fourty-nine characters”;
$string_0 = “”; // a string with zero characters

Single-quoted strings are almost completely literal, whereas double-quoted strings replace variables with their values and give additional meaning to some character combinations.

 

<?php
$var = “coderazaa”;
$str = ‘My $variable will not print!’;

print($str);
print “<br>”;

$str= “My $variable will print!”;
print($str);
?>

It gives following result −

My $var will not print!
My coderazaa will print

 

Single-quoted strings are almost completely literal, whereas double-quoted strings replace variables with their values and give additional meaning to some character combinations.

There are no arbitrary restrictions on string length. You should be able to create strings that are as long as your memory will allow. The two methods below are how PHP preprocesses strings that are separated by double quotes, such as “this”:

  • Backslash () character sequences are replaced with special characters in some character sequences.
  • String representations of the values of variables with names that begin with $ are used in place of those names.

The replacements for the escape sequence are

  1. \n is replaced by the newline character
  2. \r is replaced by the carriage-return character
  3. \t is replaced by the tab character
  4. \$ is replaced by the dollar sign itself ($)
  5. \” is replaced by a single double-quote (“)
  6. \\ is replaced by a single backslash (\)

 

Variable Scope

The range of a variable’s accessibility to the programme in which it is declared is referred to as its scope. There are four different scope types for PHP variables.

Local variables

Functioning variables

Global variables

Static variables

Variable Naming

There are guidelines for naming variables.

The first character in a variable name must be a letter or an underscore.

Numerals, letters, and underscores are permitted in variable names, but you cannot use the characters +, -,%,(,), or (+). & , etc

Variables have no size restrictions.

People also search

Leave a Comment

Scroll to Top