Categories
PHP

Testing, setting, and unsetting variables

Before attempting to use variables that come from an external source such as a user-submitted form, you must check whether those variables are defined or not. isset() function is one of the most common tests to check whether a variable has been defined.

Categories
PHP

Strict Typing Mode

In PHP the declare(strict_types = 1); directive enables strict mode. In strict mode, only a variable of the exact type of the “type declaration” will be accepted, or a TypeError will be thrown.

Categories
PHP

Type Declaration

A loosely typed language doesn’t offer the best performance since its compiler has more work to do when trying to determine the data types of its variables. On the other hand, the strict type languages consume less memory and give better performance at runtime than loosely typed languages. Since version 7, PHP offers the possibility of strict type function arguments, return values, and class properties (PHP 7.4), known as “Type declarations”.

Categories
PHP

Constants

A constant is a variable with a value that cannot be changed by the script. Therefore, such a value must be assigned at the same time that the constant is created. Constants may not be redefined or undefined once they have been set.

Categories
PHP

Type Conversion: Convert variable data type

PHP provides several mechanisms to allow variables of one type to be considered as another type. Variables can be explicitly converted to another type with the settype, strval, intval, boolval and floatval functions.

Categories
PHP

Determine Variable Data Type

PHP is known as a loosely-typed language. It converts a variable’s data type automatically, depending on the context in which the variable is used. For example, you can initialize a variable with an integer value, add a float value to it, thereby turning it into a float, then join it onto a string value to produce a string:

Categories
PHP

Data Types

Variables don’t need to be declared, and they have no type until they are assigned a value. PHP is loosely typed, meaning that it automatically determines the data type at the time data is assigned to each variable. It means a variable can hold an integer and then later in the script it can hold a string or some other data type.

Categories
PHP

Variable Substitution

Variable substitution is a way to embed data held in a variable directly into string literals. PHP parse double-quoted (and heredoc) strings and replace variable names with the variable’s value.

Categories
PHP

Delimiting Strings

PHP strings can be delimited in four different ways. A string is any series of characters enclosed in single (‘ ‘) or double (” “) quotes, or that you create using special heredoc or nowdoc syntax.

Categories
PHP

Variable Assignment, Expressions, and Operators

Variables are containers for storing information, such as numbers or text so that they can be used multiple times in the code. Variables in PHP are identified by a dollar sign ($) followed by the variable name. A variable name must begin with a letter or the underscore character and only contain alphanumeric characters and underscores. A variable name cannot contain spaces. Finally, variable names in PHP are case-sensitive.