PHP Data Types & Variables

php data types & variables

PHP Variables

Variables are used to store data, like text, numbers, etc.

How to create a PHP variable?

A variable consists of two parts: the variable’s name and the variable’s value.
Creating a variable in PHP is known as declaring it. When PHP first sees a variable’s name in a script, it automatically creates the variable at that point.

Rules you must follow when naming a variable in PHP

  • In PHP, a variable starts with the $ sign. The first character after the dollar sign must be a letter or an underscore (a variable name cannot start with a number). The remaining characters in the name may be letters, numbers, or underscores without a fixed limit
  • Variable names are case-sensitive in PHP ($Variable and $variable are two distinct variables), while function names are not case sensitive.
    In contrast to JavaScript which is a case-sensitive language. This means variables, function names, or any other identifiers must always be typed with a consistent capitalization of letters.
  • A variable name can not contain spaces or hyphens;

Good variable names make your PHP code easy to understand and maintain. Choose names for variables that tell you what the variable is for.

In a variable name use camel case or the underscore to distinguish between multiple words, like:

#firstName = "Mike";
#first_name = "Mike";

The variable $this is reserved for use in PHP object-oriented scripts. You can’t use it!

PHP is known as a loosely typed language

Meaning you are not required to specify the variable type when declaring a variable.
For example, the variable $number could hold the value 7 and the string “seven” within the same scope.

Many programming languages prevent using a variable without first declaring it, but in PHP the variable does not need to be declared before assigned a value to it, because PHP automatically converts the variable to the correct data type, depending on its value.

Other languages such as C#, Java are strictly typed, meaning, you must declare the type of a variable before assigning a value to it, and the value must be of the specified type.

Anyway, when declaring a variable in PHP, it’s a good practice to assign a value to it at the same time. This is known as initializing a variable.
$my_variable = 5;

PHP Data Types

PHP supports four scalar data types

Scalar data means data that contains only a single value:

Integer

A whole number: 27

Float

A floating – point number: 9.66

String

A series of characters: “welcome to Webpedia!”
Read more here about Working with strings in PHP

  • String literals can use single quotes or double quotes. In double quotes strings, variable values are expanded.

    $year= 1821;
    echo “Michael Faraday invented the electric motor in $year”;
    //Result
    Michael Faraday invented the electric motor in 1821
  • The backslash(\) is used as escape character. Read more here about the Escape sequences
  • Strings can span multiple lines, the newline (\n) is part of the string. But be careful, if you put newlines \n in a single-quoted
    string it doesn’t expand.
  • Concatenation is the “.“, not the “+” like in other languages (Javascript)

Read more here about the Difference between single-quoted and double-quoted

Boolean

A boolean represents either: true or false

Also PHP supports compound data types

Array

An ordered map, that contains names or numbers mapped to values.
Read more here about Working with Arrays in PHP
Read more here about PHP array functions

Object

A type that contain data: properties and methods.
Read more here about Object oriented programming (OOP) in PHP

And finally, PHP supports two special data types

Resource

Contains a reference to an external resource, such as a file or database.

Null

Meaning the variable explicitly does not contain any value

How to test data type?

Because PHP is loosely typed is important to use primitive types checking functions, to check the type of a variable.
is_bool() Boolean One of the two special values true or false
is_integer() Integer A whole number
is_double() Double A floating point number (a number with a decimal point)
is_string() String Character data
is_object() Object An object
is_array() Array An array
is_resource() Resource A handle for identifying and working with external
resources such as databases or files
is_null() Null An unassigned value

Comments in PHP

Are many ways to add comment in PHP. You can use:

  • the comment style that came from C++ which is // to the end of the line
  • the comment style from Perl and shell scripting, which is # to the end of the line.
  • the comment style from the C, /* … */ to comment on several lines

Hello there!

I hope you find this post useful!

I'm Mihai, an online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.

If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!

Leave a Reply

Your email address will not be published. Required fields are marked *