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.
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;
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.
Let’s now look at how PHP determines the data type depending on the attributes of the supplied data.
Integer
<?php
$k = 10;
echo $k;
?>
Output:
10
Floating point numbers
<?php
$pi = 3.14;
echo $pi;
?>
Output:
3.14
Character strings
<?php
$my_text ="PHP is Server Side Script";
echo $my_text;
?>
Output:
PHP is Server Side Script
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.
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.
<?php
$a = 4;
$b = 1.5;
$c = $a + $b;
$c = $a + (int) $b;
echo $c;
?>
Output:
2
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.
<?php
$a = 4;
var_dump($a);
$b = 1.5;
var_dump($b);
$c = "I am PHP";
var_dump($c);
$d = true;
var_dump($d);
?>
Output:
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.