# INTRODUCTION

# DATA TYPES

PHP Data Types :

Variables can store data of different types, and different data types can do different things.

PHP supports the following data types:

- String
- Integer
- Float (floating point numbers – also called double)
- Boolean
- Array
- Object
- NULL
- Resource

PHP String :

- A string is a sequence of characters, like “Hello world!”.
- A string can be any text inside quotes. You can use single or double quotes:

<?php
$x = “Hello world!”;
$y = ‘Hello world!’;

echo $x;
echo “<br>”;
echo $y;
?>

Output:

Hello world!
Hello world!

PHP Integer

An integer is a whole number (without decimals). It is a number between -2,147,483,648 and +2,147,483,647.

Rules for integers:

- An integer must have at least one digit (0-9)
- An integer cannot contain comma or blanks
- An integer must not have a decimal point
- An integer can be either positive or negative
- Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based – prefixed with 0x) or octal (8-based – prefixed with 0)

In the following example $x is an integer. The PHP var_dump() function returns the data type and value:

<?php
$x = 5985;
var_dump($x);
?>

Output:

int(5985)

PHP Float :

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

In the following example $x is a float. The PHP var_dump() function returns the data type and value:

<?php
$x = 10.365;
var_dump($x);
?>

Output:

float(10.365)

PHP Array :

An array stores multiple values in one single variable.

In the following example $cars is an array. The PHP var_dump() function returns the data type and value:

<?php
$cars = array(“Volvo”,”BMW”,”Toyota”);
var_dump($cars);
?>

Output:

array(3) { [0]=> string(5) “Volvo” [1]=> string(3) “BMW” [2]=> string(6) “Toyota” }

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.

First we must declare a class of object. For this, we use the class keyword. A class is a structure that can contain properties and methods:

<?php
class Car {
function Car()

{
$this->model = “VW”;
}
}

// create an object
$herbie = new Car();

// show object properties
echo $herbie->model;
?>

Output :

VW

## PHP NULL Value :

Null is a special data type which can have only one value: NULL.

A variable of data type NULL is a variable that has no value assigned to it.

Tip: If a variable is created without a value, it is automatically assigned a value of NULL.

Variables can also be emptied by setting the value to NULL:

Example :

<?php
$x = “Hello world!”;
$x = null;
var_dump($x);
?>

Output:

NULL

## PHP String Functions :

Get The Length of a String:

The PHP strlen() function returns the length of a string (number of characters).

The example below returns the length of the string “Hello world!”:

<?php
echo strlen(“Hello world!”); // outputs 12
?>

Output:

12

Count The Number of Words in a String

The PHP str_word_count() function counts the number of words in a string:

<?php
echo str_word_count(“Hello world!”); // outputs 2
?>

Output :

2

Reverse a String

The PHP strrev() function reverses a string:

<?php
echo strrev(“Hello world!”); // outputs !dlrow olleH
?>

Output:

!dlrow olleH

Search For a Specific Text Within a String

The PHP strpos() function searches for a specific text within a string.

If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.

The example below searches for the text “world” in the string “Hello world!”:

<?php
echo strpos(“Hello world!”, “world”); // outputs 6
?>

Output:

6

Replace Text Within a String

The PHP str_replace() function replaces some characters with some other characters in a string.

The example below replaces the text “world” with “Dolly”:

<?php
echo str_replace(“world”, “Dolly”, “Hello world!”); // outputs Hello Dolly!
?>

Hello Dolly!

## Create A PHP Constant

To create a constant, use the define() function.

### Syntax

define(name, value, case-insensitive)

Parameters:

- name: Specifies the name of the constant
- value: Specifies the value of the constant
- case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false

The example below creates a constant with a case-sensitive name:

<?php
define(“GREETING”, “Welcome to vissicomp.com!”);
echo GREETING;
?>

Output:

Welcome to vissicomp.com!

PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators

## PHP Arithmetic Operators:

+ | Addition

##

<?php
$x = 10;
$y = 6;

echo $x + $y;
?>

Output:

16

– | Subtraction

<?php
$x = 10;
$y = 6;

echo $x – $y;
?>

Output:

4

* | Multiplication

<?php
$x = 10;
$y = 6;

echo $x * $y;
?>

Output:

60

/ | Division

<?php
$x = 10;
$y = 6;

echo $x / $y;
?>

Output:

% | Modulus

<?php
$x = 10;
$y = 6;

echo $x % $y;
?>

Output:

4

## PHP Assignment Operators :

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

x = y

<?php
$x = 10;
echo $x;
?>

Output:

10

x += y | x = x + y | Addition

<?php
$x = 20;
$x += 100;

echo $x;
?>

Output:

120

x -= y | x = x – y | Subtraction

<?php
$x = 50;
$x -= 30;

echo $x;
?>

Output:

20

x *= y | x = x * y | Multiplication

<?php
$x = 10;
$y = 6;

echo $x * $y;
?>

Output:

60

x /= y | x = x / y | Division

<?php
$x = 10;
$x /= 5;

echo $x;
?>

Output:

2

x %= y | x = x % y | Modulus

<?php
$x = 15;
$x %= 4;

echo $x;
?>

Output:

3

## PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

== | Equal | $x == $y | Returns true if $x is equal to $y

<?php
$x = 100;
$y = “100”;

var_dump($x == $y); // returns true because values are equal
?>

Output:

Bool(true)

!= | Not equal | $x != $y | Returns true if $x is not equal to $y

<?php
$x = 100;
$y = “100”;

var_dump($x != $y); // returns false because values are equal
?>

Output:

Bool(false)

> | Greater than | $x > $y | Returns true if $x is greater than $y

<?php
$x = 100;
$y = 50;

var_dump($x > $y); // returns true because $x is greater than $y
?>

Output:

bool(true)

< | Less than | $x < $y | Returns true if $x is less than $y

<?php
$x = 10;
$y = 50;

var_dump($x < $y); // returns true because $x is less than $y
?>

Output:

Bool(true)

>= | Greater than or equal to | $x >= $y | Returns true if $x is greater than or equal to $y

<?php
$x = 50;
$y = 50;

var_dump($x >= $y); // returns true because $x is greater than or equal to $y
?>

Output:

bool(true)

<= | Less than or equal to | $x <= $y | Returns true if $x is less than or equal to $y

<?php
$x = 50;
$y = 50;

var_dump($x <= $y); // returns true because $x is less than or equal to $y
?>

Output:

bool(true)

## PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable’s value.

The PHP decrement operators are used to decrement a variable’s value.

++$x | Pre-increment | Increments $x by one, then returns $x

<?php
$x = 10;
echo ++$x;
?>

Output:

11

$x++ | Post-increment | Returns $x, then increments $x by one

<?php
$x = 10;
echo $x++;
?>

Output:

10

–$x | Pre-decrement | Decrements $x by one, then returns $x

<?php
$x = 10;
echo –$x;
?>

Output:

9

$x– | Post-decrement | Returns $x, then decrements $x by one

<?php
$x = 10;
echo $x–;
?>

Output:

10

## PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

&& | And | $x && $y | True if both $x and $y are true

<?php
$x = 100;
$y = 50;

if ($x == 100 && $y == 50) {
echo “Hello world!”;
}
?>

Output:

Hello world!

|| | Or | $x || $y | True if either $x or $y is true

<?php
$x = 100;
$y = 50;

if ($x == 100 || $y == 80) {
echo “Hello world!”;
}
?>

Output:

Hello world!

! | Not | !$x | True if $x is not true

<?php
$x = 100;

if ($x !== 90)

{
echo “Hello world!”;
}
?>

Output:

Hello world!