Variables are a very fundamental concept of any programming language and Digital Marketing course in Delhi are no different. Variables allow the developers to temporarily store the data in PHP scripts. This article guides you through different PHP variables and their working applications.
For a neat well-explained code, commenting plays a major role. For single-line comments, one can use two forward slashes (//) preceding the comment or a hash symbol(#). For a comment block, one should write the comment between /* */.
<echo ‘Hello World!’; //This is another comment
By using a comment block, you are
capable of putting comments
on multiple lines.
echo ‘Hello World!’;
echo ‘I\’m using comments!’; # An alternative syntax of commenting
Introduction to Variables:
In PHP, a variable begins with a dollar sign ($). Variables should be assigned descriptive names. Alphanumeric characters (i.e. A-Z, a-z, and 0-9) and underscores (_) are allowed in naming these variables, however, the name shouldn’t start with a digit and must not contain any other character.
$player_name;// Valid$playerName;// Valid
$_player_name;// Not valid – has a space
$player name;// Not valid – starts with a number
Assigning Data to a Variable:
Assigning value is done as shown:
$player_name = ‘Freddy’;
Here the variable player_name has the value Freddy assigned to it
Command echo lets you print out the value of a variable.
$player_name = ‘Freddy’;// Prints out “Hello Freddy!”
echo ‘Hello’. $player_name . ‘!’;
The (.)lets you concatenate the static string with the variable. Double quotes can also be used or the very same purposes.
Different Data Types:
In PHP, unlike other languages, the data type is automatically set according to the value assigned to the variable.
1. Null Data Type
A variable with no value assigned to it is a variable of type “NULL”. Assigning null to a variable deletes its content from the memory and assigns 0 bytes to it
Data Type Switching
In PHP you’re allowed to switch any variable to any of the available data types. Whenever you assign a value to a variable, its data type is set accordingly. This is called type juggling. However, juggling is resource expensive and thus is recommended only when needed.
2. Boolean Data Type
The smallest data type, that takes either of the two values- true or false.
The var_dump() is a function that inputs any expression and displays additional information e.g the data type, the size and the value of the input variable etc. On the screen.
'; $has_permission = 'false'; var_dump( $has_permission); echo '
Here, $has_permission = ‘false’; results in- (string(5) “false”), meaning it is a string data type with 5 characters, and the value ‘false’.
3. Int Data Type
Int short for Integer variable is used to store numeric values. The range of this data type is platform-dependent, in general, a max value of 2 billion is typical. Being numeric in nature, allows them to be subjected to mathematical operations.
4. Float Data Type
Unlike integers, floats can have decimal values such as 12.3 or 2435.45895. Afloat has more precision as compared to integer and thus requires more storage space, max size being platform-dependent. Although typically it is 1.8e308
5. String Data Type
The string data type is the biggest data type PHP knows. The maximum size equals the size of the server’s memory. \n and \t are called linefeeds and they are categorized as escaped characters.
Type casting refers to the process of transforming a specific data type into another (e.g. switching from a string data type to float data type).
You can cast almost any data type to another one by using the following syntax- (data type) $variable name, e.g. (bool)$myString
This guide provides you with a brief description of how to use comments, variables, their types, and typecasting giving you a good idea about how you can put these features to your advantage with the right knowledge of their applications.