Thursday, May 7, 2020

Data-types, Variables and Operators

PHP Data types

A variable can store different types of Data. Let’s have a look at some of the data types supported by PHP:


PHP String

A string is a sequence of characters. In PHP, you can write the string inside single or double quotes.

<?php $a = "Hello World!";
$b = 'Hello World!';
echo $a;
echo "<br>";
echo $b
?>

PHP Integer

An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647. An integer must have at least one digit and can be either positive or negative.
The following example takes $a as an integer. The PHP var_dump() function returns the data type and value.

<?php 
$a = 0711;
var_dump($a); 
?> 

PHP Float

A float or floating point number is a number with a decimal point or a number in exponential form.

The following example takes $a as a float and the PHP var_dump() function returns the data type and value.

<?php
$a = 14.763;
var_dump($a);
?> 

PHP Boolean

A Boolean represents two possible states: TRUE or FALSE. They are often used in conditional testing.

$a = true;
$b = false;

PHP Object

An object is a data type which stores data and information on how to process that data. In PHP, an object must be explicitly declared. We need to declare a class of object using the class keyword.

<?php
class Student
{
    function Student()
    {
    $this->name = “XYZ”;
    }
}
// create an object
$Daniel = new Student(); // show object properties
echo $Daniel->name
?>

PHP Array

An array stores multiple values in one single variable. In the following example, the PHP var_dump() function returns the data type and value.

<?php 
$students = array(“Daniel”,”Josh”,”Sam”);
var_dump($students); 
?>

Now that you have learnt about the various Data Types, let’s move ahead with the PHP Tutorial and have a look at the different PHP Variables. 

PHP Variables

Variables are containers for storing information. All variables in PHP are denoted with a leading dollar sign ($). Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.

Declaring PHP Variables:

<?php
$txt = "Hello World!";
$a = 7;
$b = 11.5
?>

The PHP echo statement is often used to output data to the screen.

PHP Variables Scope

In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be used.

In PHP we have three different variable scopes:

            1. Local – A variable declared within a function has a LOCAL SCOPE and can only be accessed within that function:

<?php 
    function myTest() 
    { 
    $a = 7// local scope 
    echo "<p>Variable a inside function is: $a</p>"
    } 
    myTest(); // using x outside the function will generate an error echo 

"<p>Variable a outside function is: $a</p>"
?>

2. Global– A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a function. The global keyword is used to access a global variable from within a function:

<?php
$a = 9// global scope
function myTest() {
// using a inside this function will generate an error
echo "<p>Variable a inside function is: $a</p>";
}
myTest();
echo "<p>Variable a outside function is: $a</p>"
?>
  1. Static– When a function is executed, all of its variables are deleted. But if you want any variable not to be deleted, the static keyword is used when you first declare the variable:
<?php
function myTest() 
{
static $a = 0;
echo $a$a++; 
}
myTest();
myTest();
myTest();
?>

Now that you know about the declaration of variables, let’s move ahead with the PHP Tutorial and have a look at the operators in PHP.

PHP Operators

Operators are used for performing different operations on variables. Let’s have a look at the different operators in PHP:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Array operators

Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Operator

Name

Example

Result

+

Addition

$a + $b

Sum of $a and $b

Subtraction

$a – $b

Difference of $a and $b

*

Multiplication

$a * $b

Product of $a and $b

/

Division

$a / $b

Quotient of $a and $b

%

Modulus

$a % $b

Remainder of $a divided by $b

**

Exponentiation

$a ** $b

Result of raising $a to the $b’th power

 

Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

Assignment

Similar to

Result

a = b

a = b

The left operand gets set to the value of the expression on the right.

a += b

a = a + b

Addition

a -= b

a = a – b

Subtraction

 

Comparison Operators

The PHP comparison operators are used to compare two numbers or strings

Operator

Name

Example

==

Equal

$a == $b

===

Identical

$a === $b

!=

Not equal

$a != $b

<> 

Not equal

$a <> $b

!==

Not identical

$a !== $b

> 

Greater than

$a > $b

< 

Less than

$a < $b

>=

Greater than or equal to

$a >= $b

<=

Less than or equal to

$a <= $b

 

Logical Operators

The PHP logical operators are used to combine conditional statements.

Operator

Name

Example

and

And

True if both $a & $b are true

or

Or

True if either $a or $b are true

xor

Xor

True if either $a or $b are true, but not both

&&

And

True if both $a & $b are true

||

Or

True if either $a or $b are true

!

Not

True if $a is not true

 

Array Operators

The PHP array operators are used to compare arrays.

Operator 

Name

Example

+

Union

$a + $b

==

Equality

$a == $b

===

Identity

$a === $b

!=

Inequality

$a != $b

<> 

Inequality

$a <> $b

!==

Non-identity

$a !== $b

 Now let’s move ahead with our PHP Tutorial and have a look at the various OOP concepts in PHP.