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:
Booleans were introduced for the first time in PHP 4. A Boolean value can be either true or false.
Integers can be written in decimal, hexadecimal, and octal notation.
Floating-point numbers is also known as real numbers.
Strings are a sequence of characters in PHP.
Two compound types:
An array in PHP is a collection of key/value pairs.
And finally three special types:
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.
- Names of variables are case sensitive this means $var_name is different from $VAR_NAME.
$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
Let’s now look at how PHP determines the data type depending on the attributes of the supplied data
"; echo '$str."
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
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.
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
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 are used to performing arithmetic operations on numeric data. The concatenate operator works on strings values too. PHP supports the following 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|
Comparison operators are used to comparing values and data types.
|==||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|
When working with logical operators, any number greater than or less than zero (0)evaluates to true. Zero (0) evaluates to false.
|&&||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.