Back to Top

PHP Variables, Operators and Data types

PHP Variables Operators and Data types

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:

  1. Boolean

Booleans were introduced for the first time in PHP 4. A Boolean value can be either true or false.

  1. integer

Integers can be written in decimal, hexadecimal, and octal notation.

  1. float

Floating-point numbers is also known as real numbers.

  1. string

Strings are a sequence of characters in PHP.

Two compound types:

  1. array

An array in PHP is a collection of key/value pairs.

  1. object

And finally three special types:

  1. resource

Resources is a special data type which represents a PHP resource such as a database query, an open file, a database connection.

  1. 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

  1. All variable names must start with the dollar sign.

  1. Names of variables are case sensitive this means $var_name is different from $VAR_NAME.

  1. All variables names must start with a letter follow other characters

e.g. $var_name1. $1var_name is not a legal variable name.

  1. 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:

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

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.

OPERATORDESCRIPTIONEXAMPLERESULT
+Additionx=2,x+24
Subtractionx=2,5-x3
*Multiplicationx=4,x*520
/Division15/5,5/232.5
%Modulus(division remainder)5%2,10%812
++Incrementx=5,x++6
Decrementx=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

OPERATOREXAMPLEIS THE SAME AS
=x=yx=y
+=x+=yx=x+y
-=x-=yx=x-y
*=x*=yx=x*y
/=x/=yx=x/y
.=x.=yx=x.y
%=X%=yx=x%y

Comparison operators

Comparison operators are used to comparing values and data types.

OPERATORDESCRIPTIONEXAMPLE
==is equal to5==8 return false
!=is not equal5!=8 return true
<>is not equal5<>8 return true
>is greater than5>8 return false
<is less than5<8 return true
>=is greater than orequal to5>=8 return false
<=is less than or equal to5<=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.

OPERATORDESCRIPTIONEXAMPLE
&&andx=6,y=3(x < 10 && y > 1) return true
||orx=6,y=3(x==5 || y==5) return false
!notx=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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to add Sorting columns in Joomla

Posted on 11 years ago

Bhumi

Learn about Variables in LESS CSS

Posted on 10 years ago

Bhumi

The Readers’ Poll – October 2012

Posted on 12 years ago

Bhumi

How to use Session in Joomla

Posted on 12 years ago

Bhumi