PHP fundamentals – PHP constants, PHP data types

Continuing with the PHP fundamentals, first we are going to talk about constants, what they are and what do they do. Basically, they represent memory zones referred by a name which initialized, it can’t change. In PHP, constants have the following characteristics.

  • They are created using “define” which gets 2 parameters. First is the name (must be string) and second is the constant value (can take any data type [we are going to talk later about them]).
  • Their name is case sensitive so be careful
  • They are available in all PHP files (they are global values)
  • The value of a constant can be only scalar (integer, float, boolean or string)

Some examples of constants can be found below.


//to print them you can use "echo" but without the $.. examples below.
echo "My name is " .me; // My name is Bogdan
echo "My age is " .age; // My age is 27

PHP data types

The data type a PHP variable has at a specific moment can be determined using two operations:

  • The last value assigned to the variable. For example, along the way a variable can change its type from string to boolean (if needed).
  • Last conversion applied. For example, a variable can change its type using the predefined function “settype()

PHP data types can be divided in the following categories:

  • Scale data types – variables that can memorize only a single value
    • numeric values – int and float
    • true or false values – boolean
    • char strings – string
  • Composed data types
    • array – key -> values pairs where the key can be integer or string and the value can have any data type
    • object – an entity that has collections of data but also functions necessary to process them
  • Special data types
    • resource – data types for variables that store information that is not within PHP gained by using special functions
    • NULL – used to tell that a variable does not have a value

Ok, we know what types of data are in PHP, lets talk about them a little bit. First we start with bool or boolean data types which are used to store true or false values. The two values are case-sensitive and you need to be added without ” or ‘. Examples below.

$test = true;
$testFalse = FALSE;
$sum = (7>3) // $sum will become a boolean variable with the value true (or 1)

The boolean true or false values are also used in more complex functions that return a true/false condition depending on the situation. For example

if($age > 100) {
     echo "invalid age"; 
     //returning false because is hard to find someone over 100 years. 
} else {
     // return true

int or integer data types are used to memorize numbers. There is a limit that int or integer can have and it is memorized in the PHP_INT_SIZE constant. If int/integer have values that are over the maximum limit of this data type, the variable will automatically transform to float. Lets see some examples.

$a = 4;
$b = -123232;
$c = 0x5a; // or $c = 90 - hexazecimal 
$d = 067; // or $d = 55 - octal

A hexazecimal number can be recognized by the “0x” in front and a octal number can be recognized by the “0” in front.

float data type is used to memorize numbers with decimals. The number of bytes used to storage in memory is as int or integer, dependent of the platform. float data types examples are below.

$me1 = 5.14;
$me2 = 514E-2; // 514 x 10(-2)
$me3 = 0.232e3; // 0.232 x 10(2)

string data type is usually used to stock in memory char strings. Many programs out there are construct from many strings and in PHP, a string data type variable can contain a series of binary data (can contain letters, numbers, etc).

We can use the string data type in many ways and we are going to talk about them a bit. The value that is entered between “” will always be considerate as string. Keep in mind that using “” in a string in a different way can cause problems (check the examples bellow for more details).

$s = "string me"; //correct form.
$s = "string me "a string" on me";
// the first set of " will be considered as final delimiter thus 
// resulting in an error

To be able to use quoted text into a string you need to escape them. The easiest way to do that is by using the \ char. See example bellow.

$s = "This is a string with \"escape\" function";

The \ character has also other roles like when you want to add a new line in a series of strings you can use the “\n” (newline) snippet or use the “\t” for tab. “\” is also used to escape variables inside a string. Check example below.

$a = 10;
$s = "The value of \$a is $a";
echo $s; // The value of $a is 10

We learned that a charset will automatically transform to string when we use the “” chars. But if we use the ” what happens? Well, almost the same thing. The charset inside the ” will transform into a string but the special variables inside the string will not be transformed anymore. Meaning, if we have $a inside a string that is ”, it will output $a and not the value of the $a variable.

When you have a string, you can access every bit of it. Lets say for example we have a variable $s = “test me”. Using $s[2] will return the letter “s” ($s[] is transforming our string into an array of elements. Every array starts with 0, thats why $[2] returns “s” and not “e”).

In some cases you might need to concat some string variables so it would output a phrase or something else. For example, if we have 2 variables: $a = “I am “; $b = “sleepy”; and we create another variable $c = $a.$b, then $c will become “I am sleepy”. Notice we have a space after “am” so the output of the concat string will be readable.

PHP data conversion

You can easily convert any variable from a type to another (as we mentioned before) but there are specific cases where you can get the integer from a string. For example…

$s = "3 flowers are in the left side";
var_dump((int)$a); // will return 3.

This is it for today. If you have any questions or any objections/suggestions/etc, please leave them in the comment box bellow.

Related Posts

PHP fundamentals – introduction, php instructions and variables

What is PHP? The Beginnings.

Cool forum software written in PHP and MySQL

Single-file PHP debugger script

Leave a Reply