This tutorial is actual start of the PHP code, in last lecture we wrote simple PHP Hello World program , in this tutorial we start to learn PHP Data Types, Variables, Constant &Operators. As like any other programming language PHP also have variable, constants & operators. Let’s dive in to deep of those concepts in consideration of PHP.
PHP Data Types
Data types are types of variables , i.e. what type of data variable can save or assigned. Some times we need to save name of user or any other text , then we need different data type, where as to save date, age , file etc. each time we need to have different data types. Here are some data types below;
- Alphanumeric characters are classified as strings
- Whole numbers are classified as integers
- Numbers with decimal points are classified as floating points.
- True or false values are classified as Boolean.
PHP is a loosely typed language; what does mean by loosely typed language? it means PHP does not have explicit defined data types. PHP determines the data types by analyzing the attributes of data supplied. You don’t need to define data type of variable while defining it. Just like other some programming language needs to define variable type with variable like its string, its integer etc. That’s not case with PHP. We will see more about data types supported by PHP , first we discuss about PHP variables..
A variable is a name given to a memory location that stores data at runtime.
The scope of a variable determines its visibility.
A Php global variable is accessible to all the scripts in an application.
A local variable is only accessible to the script that it was defined in.
Think of a variable as a glass containing water. You can add water into the glass, drink all of it, refill it again etc.
The same applies for variables. Variables are used to store data and provide stored data when needed. Just like in other programming languages, PHP supports variables too. Let’s now look at the rules followed when creating variables in PHP.
- All variable names must start with the dollar sign e.g. $My_var
- Variable names are case sensitive in PHP; this means $a is different from $A
- All PHP variables names must start with a letter follow other characters e.g. $a1.While $1a is not a legal variable name.
- Variable names in PHP must not contain any spaces, “$user name” is not a legal variable name. You can instead use an underscore in place of the space. Also can use minus sign to generate variable names instead of space. E.g. $last-name, $last_name are legal variable names.
Let’s now look at how PHP determines the data type depending on the attributes of the supplied data.
$k = 10;
Floating point numbers
$pi = 3.14;
$my_text ="PHP is Server Side Script";
PHP is Server Side Script
Use of Variables
Variables help separate data from the program logic.
The same logic/function can be used for different input data values.
For example, suppose that you are creating newsletter form, where you simply accepts the email from users as input, then send some verification mail on that mail. In that case you not write different mail sending logic for different mail. Rather we assign some variable to store that email from user. and send it to logic/function which will send email to client.
Variable Type Casting
while doing algorithmic / coding computations using variables in a language such as C# requires the variables to be of the same data type.
Type casting is converting a variable or value into a data type to perform respected arithmetic calculations.
This is very useful when performing arithmetic computations that require variables to be of the same data type.
Type casting in PHP is done by the interpreter.
In other languages such as C#, you have to cast the variables. The code below shows type casting in C#.
The diagram below shows how same code logic implemented in PHP.
PHP also allows you to cast the data type. This is known as explicit casting. The code below demonstrates explicit type casting.
$a = 4;
$b = 1.5;
$c = $a + $b;
$c = $a + (int) $b;
Above Code prints 5, The var_dump function is used to determine the data type. The code below demonstrates how to use the var_dump function.
$a = 4;
$b = 1.5;
$c = "I am PHP";
$d = true;
int(4) float(1.5) string(8) "I am PHP" bool(true)
Define constant– A constant is a variable whose value cannot be changed at runtime.
Suppose we are developing a program that uses the value of PI 3.14, we can use a constant to store its value.
Let’s now look at an example that defines a constant. define(‘PI’,3.14); //creates a constant with a value of 3.14 Once you define PI as 3.14 , writing a code like below will generate an error PI = 4;
//PI has been defined as a constant therefore assigning a value is not permissible.
Arithmetic operators are used to perform arithmetic operations on numeric data. The concatenate operator works on strings values too. PHP supports the following operators.
Assignment operators are used to assign values to variables. They can also be used together with arithmetic operators.
Comparison operators are used to compare values and data types.
When working with logical operators, any number greater than or less than zero (0) evaluates to true. Zero (0) evaluates to false.
- PHP is a loosely typed language.
- Variables are memory locations used to store data
- The value of constants cannot be changed at runtime
- Type casting is used to convert a value or variable into a desired data type
- Arithmetic operators are used to manipulate numeric data
- Assignment operators are used to assign data to variables
- Comparison operators are used to compare variables or values
- Logical operators are used to compare conditions or values