PHP Tutorial

PHP variables, PHP data types

? ? ? ? ? ?

What are PHP variables?

The variables are used as containers, like boxes, where we can keep values.

Their names should always have the $ symbol at the beginning, followed by a letter or "_" (underscore). After that you can either use letters, numbers or underscore, like "$first_name" or "$_address1". Beside underscore no other special characters (like "+, -, *, % ...") are allowed for the name.

The names are case sensitive (PHP makes the difference between uppercase and lowercase letters of the name), so PHP will see "$movieName" as one variable and "$moviename" as another variable. 

<?php

//examples of variables
$first_name = 'Edward';

$description = 'This is a PHP course';

$month_number = 11;


?>

As their names suggests ("variables"), we can change the content of the PHP variables as many times as we want, the content is variable. We can also assign the value from one variable to another one.

<?

$fruit = 'apple';

$fruit = 'mango';

$tropical_fruit = $fruit;

//it will display the value 'mango'
echo $tropical_fruit;

The semicolor symbol ; is required to signal the end of an line instruction.

We use echo when we want to output a value. 

What are PHP constants?

As the name already says, opposite to the PHP variables, the constant's value is constant, it doesn't change during the application's execution.

To define a constant you need to use the function define().

<?

//we create the constant PI
define("PI", 3.14);

echo PI;
//will output 3.14

In the name of a PHP constant we don't use the symbol $ at the beginning. Also, as a convention among programmers, their names are written with uppercase letters, to make them easier to identify.

In order to check if a constant is defined you can use the function defined. This function will return 1 if the constant has been defined or 0 if it hasn't been defined.

<?php

//check if the constant has been defined
echo defined("PI")

?>

Remember to use double quotations marks, "", when defining the name inside the function define().

What are the PHP variables' type?

PHP variables' type in PHP refers to what kind of content the variables can have inside them, for example a number or text. In PHP  applications we can work with multiple types of data. The PHP variables can have the following types of data:

  • Integer
  • Float
  • String
  • Boolean
  • Array
  • Object
  • Resource
  • NULL

By default, the PHP variables don't have any type of data assign to them, the PHP interpretor will evaluate and cast their type at the run-time (the moment of script's execution).'

You can check the type of a variable using gettype().

<?php

$name = 'Johnny';
$price = 56;

echo gettype($name);
//will output string

echo gettype($price);
//will output integer

What is a float or integer in PHP?

Integer and  Float types are used for numeric values.

The integer type is for non-decimal values, between -2,147,483,648 and 2,147,483,647. They can be either positive or negative.

<?php

$month_number = 11;

$temperature = -16;

?>

The float type (also known as double) is used for numbers with a decimal point. You can convert a float number to an integer using the cast operator (int) (returns the number without decimal).

<?php

//the float variable
$price = 22.89;

//get the integer value of the $price
$integer_price = (int)$price;

//will return 22
echo $integer_price;

?>

What is a string in PHP?

A string is represented by a series of characters. There is no rule for the maximum number of characters a string may have. Compared with integer or float, a string can have any type of character. A string normally starts with ' and ends with ' or " and ends with ".

<?php

$address = '8th Street, nr. 36';

?>

When using double quotes we may also pass PHP variables'  values into the string.

<?php

$name = 'Mike';

$age  = 32;

$person_description = "His name is $name, and he's $age years old.";
echo $person_description;
// this will output "His name is Mike, and he's 32 years old."

?>

What is the difference between single quotes and double quotes in PHP?

 When using double quotes we can also pass into strings special characters like:

  • \n - line feed;
  • \r - carriage return;
  • \t - horizontal tab;
  • \\ - backslash;
  • \$ - dollar sign;

Both \n and \r are used to move the text to a new line. 

To escape PHP variables (just show their names not value) and double quotes or single quotes (depending on how you start and end the assigning of the text), or backslash you need to use backslash.

<?php

$apples = 'Green apples';

$shop_text = "The price of the apples has increased with 60 percent \nso now they are called \$apples or business\\rich apples. \n \t - by \"Shop News\"";

echo $shop_text;
/*
will output

The price of the apples has increased with 60 percent 
so now they are called $apples or business\rich apples. 
 	 - by "Shop News"

- we added a new line with \n
- escaped the variable $apples so we didn't passed it's value into the string
- escaped the backslash with backslash so \r won't make a new line 
- added a tab with \t
- escaped double quotes with backslash
*/

?>

It's recommended to put strings inside double quotes only when needed, as PHP will slightly need more time to check the content of a string inside double quotes than single quotes, so the script's execution with take slightly more time.

What is a boolean type in PHP?

Boolean PHP variables have a logical value. The boolean value can be wither TRUE or FALSE.

You can convert other types of values to boolean the cast operator (bool), but in general you won't be needed to use it, PHP will recognize it's logical values. Values like 0 or empty strings will automatically be converted to False, while strings or number that have other values than 0 will be converted to True.

These boolean values are case-insensitive for PHP, so you can either write TRUE, true or True, same with the FALSE.

<?php

$correct = true;

$still_correct = (bool)'Hello';
//this also has the value true;

?>

What is a NULL type in PHP?

NULL are the PHP variables that don't have any value assigned to them, it represents the absence of any value.

The PHP variables can be NULL if:

  • they have been assign to the NULL value;
  • they didn't have any value assigned to them;
  • their values have been removed using the function unset()
<?php

$name;
//it's null 

$address = null;
//it's null

$phone_number = '0226565333222';

unset($phone_number);
//it's null now

?>

Which variable names are correct?

What function we use to define constants?

The PHP interpretor will evaluate the variables and cast their type at the run-time.

What type of value is the number 456.72?

What will be the output of the following lines:

<?php

$name = 'Ethan';

$text = "My $name is \$name, web development is great.";

echo $text;

?>

How can you set a variable to NULL?