PHP is easy to learn compared to the other language like Java and ASP.NET.PHP has a quite easy to parse and human-friendly syntax.In this tutorial, we will learn PHP programming concepts such as Datatypes,variables, and Operators.
What is Data Types in PHP
Data Types data types are a core component of programming language.A Data type refers to the type of data a variable can store.
PHP supports eight primitive types:
Four scalar types:
- Boolean
Booleans were introduced for the first time in PHP 4. A Boolean value can be either true or false.
- integer
Integers can be written in decimal, hexadecimal, and octal notation.
- float
Floating-point numbers is also known as real numbers.
- string
Strings are a sequence of characters in PHP.
Two compound types:
- array
An array in PHP is a collection of key/value pairs.
- object
And finally three special types:
- resource
Resources is a special data type which represents a PHP resource such as a database query, an open file, a database connection.
- NULL
Null is a data type with only one possible value is the NULL value.
PHP is a loosely coupled language so you don’t need to define data types explicitly. PHP determines the data types by data assigned to the variable. PHP implicitly supports the following data types.PHP also allows you to cast the data type. This is known as explicit casting.In below section, we have explained type casting with an example.
Now let’s first discuss variables in PHP.
What is Variable in PHP
Variables are the core of part for your code which makes your value flexible.Variables are used for storing values, like text strings, numbers or arrays.
Variables are denoted by $ symbol followed by the variable’s name in PHP. .A variable is a name of the value that stores data at run-time. The variable scope determines its visibility. A 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.
Variables are used to store data and provide stored data when needed and to assign a value to a variable in PHP is quite easy.To assign a value to a variable, use an equals sign (=).
Variable Naming Rules
- All variable names must start with the dollar sign.
1 2 3 | <?php echo $var_name; ?> |
- Names of variables are case sensitive this means $var_name is different from $VAR_NAME.
1 2 3 | $var_name != $VAR_NAME |
- All variables names must start with a letter follow other characters
e.g. $var_name1. $1var_name is not a legal variable name.
- Variable names must not contain any spaces
$var name is not a legal variable name. You can use an underscore in place of the space
e.g.$var_name.
Let’s now look at how PHP determines the data type depending on the attributes of the supplied data
Example:
1 2 3 4 5 6 7 8 | <?php $str = "Hello World!" $var = 3.14; echo '$var: '.is_double($a)."<br>"; echo '$str."<br>"; ?> |
Variable Type Casting
To Perform arithmetic operations using variables,you require the variables to be of the same data type.Type casting is converting a variable or value into the desired data type. This is very useful when performing arithmetic operations that require variables to be of the same datatype.
Here is the example of Variable
1 2 3 4 5 6 7 8 | <?php $var1 = 3.14; $var2 = 5; $var1_cast = (int)$var1; echo $var1 + $var2; ?> |
Type casting in PHP is performed by the interpreter. PHP also allows you to cast the data type. This is known as explicit casting.The var_dump function is used to determine the data type.
Here we use the same code used above in variable section to demonstrates the var_dump function.
1 2 3 4 5 6 | <?php $var = 3.14; var_dump($var); ?> |
What is Constant?
A constant is an identifier (variable) for a simple value. The value cannot be changed during the script.Suppose you are developing a program that uses the value of PI 3.14, you can use a constant to store its value.
Let’s see the example of constant to assign value 3.14 to PI
1 2 3 | define('PI',3.14); //creates a constant with a value of 3.14 |
Once you define PI as 3.14 and if you try to reassign value to constant,it will generate an error.A valid constant name starts with a letter or underscore (no $ sign before the constant name).
Note: Unlike variables, constants are automatically global across the entire script.
There are two built-in constants, TRUE and FALSE (case-insensitive), which represent the two possible boolean values.
What is Operators?
PHP offers several types of Operators and we will learn here
Arithmetic operators
Arithmetic operators are used to performing arithmetic operations on numeric data. The concatenate operator works on strings values too. PHP supports the following operators.
OPERATOR | DESCRIPTION | EXAMPLE | RESULT |
+ | Addition | x=2,x+2 | 4 |
– | Subtraction | x=2,5-x | 3 |
* | Multiplication | x=4,x*5 | 20 |
/ | Division | 15/5,5/2 | 32.5 |
% | Modulus(division remainder) | 5%2,10%8 | 12 |
++ | Increment | x=5,x++ | 6 |
— | Decrement | x=5,x– | 4 |
Assignment Operators
Assignment operators are used to assigning values to variables. They can also be used together with arithmetic operators.
Operator Name Description Example Output
OPERATOR | EXAMPLE | IS THE SAME AS |
= | x=y | x=y |
+= | x+=y | x=x+y |
-= | x-=y | x=x-y |
*= | x*=y | x=x*y |
/= | x/=y | x=x/y |
.= | x.=y | x=x.y |
%= | X%=y | x=x%y |
Comparison operators
Comparison operators are used to comparing values and data types.
OPERATOR | DESCRIPTION | EXAMPLE |
== | is equal to | 5==8 return false |
!= | is not equal | 5!=8 return true |
<> | is not equal | 5<>8 return true |
> | is greater than | 5>8 return false |
< | is less than | 5<8 return true |
>= | is greater than orequal to | 5>=8 return false |
<= | is less than or equal to | 5<=8 return true |
Logical operators
When working with logical operators, any number greater than or less than zero (0)evaluates to true. Zero (0) evaluates to false.
OPERATOR | DESCRIPTION | EXAMPLE |
&& | and | x=6,y=3(x < 10 && y > 1) return true |
|| | or | x=6,y=3(x==5 || y==5) return false |
! | not | x=6,y=3!(x==y) return true |
There you go! Start enjoying your new checklist!
If you have any questions, don’t hesitate to ask in the comments section, and I will be here to answer you.