Register here to save your progress
What are the different types of PHP variables?
Data 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 variables can have the following types of data:
By default, the 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
The PHP interpretor will evaluate the variables and cast their type at the run-time.
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 type of value is the number 456.72?
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 a variable's value 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." ?>
Which type of quotes allow inserting the variables value into a string?
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 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 will be the output of the following lines:
<?php $name = 'Ethan'; $text = "My $name is \$name, web development is great."; echo $text; ?>