## PHP Operators

PHP Operators are symbols which are used to perform certain operations on variables. There are following types of operators in PHP.

## PHP Arithmetic Operators

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

Operator Name Example Result
`+` Addition `\$x + \$y` Sum of \$x and \$y
`-` Subtraction `\$x - \$y` Difference of \$x and \$y.
`*` Multiplication `\$x * \$y` Product of \$x and \$y.
`/` Division `\$x / \$y` Quotient of \$x and \$y
`%` Modulus `\$x % \$y` Remainder of \$x divided by \$y

## PHP Assignment Operators

The assignment operators are used to assign values to variables.

Operator Name Example Is The Same As
`=` Assign `\$x = \$y` `\$x = \$y`
`+=` Add and assign `\$x += \$y` `\$x = \$x + \$y`
`-=` Subtract and assign `\$x -= \$y` `\$x = \$x - \$y`
`*=` Multiply and assign `\$x *= \$y` `\$x = \$x * \$y`
`/=` Divide and assign quotient `\$x /= \$y` `\$x = \$x / \$y`
`%=` Divide and assign modulus `\$x %= \$y` `\$x = \$x % \$y`

## PHP Comparison Operators

The comparison operators are used to compare two values and return Boolean.

Operator Name Example Result
`==` Equal `\$x == \$y` True if \$x is equal to \$y
`===` Identical `\$x === \$y` True if \$x is equal to \$y, and they are of the same type
`!=` Not equal `\$x != \$y` True if \$x is not equal to \$y
`<>` Not equal `\$x <> \$y` True if \$x is not equal to \$y
`!==` Not identical `\$x !== \$y` True if \$x is not equal to \$y, or they are not of the same type
`<` Less than `\$x < \$y` True if \$x is less than \$y
`>` Greater than `\$x > \$y` True if \$x is greater than \$y
`>=` Greater than or equal to `\$x >= \$y` True if \$x is greater than or equal to \$y
`<=` Less than or equal to `\$x <= \$y` True if \$x is less than or equal to \$y

## PHP Increment / Decrement Operators

The increment and decrement operators are used to increment or decrement a variable’s value.

Operator Name Effect
`++\$x` Pre-increment Increments \$x by one, then returns \$x
`\$x++` Post-increment Returns \$x, then increments \$x by one
`--\$x` Pre-decrement Decrements \$x by one, then returns \$x
`\$x--` Post-decrement Returns \$x, then decrements \$x by one

## PHP Logical Operators

The logical operators are typically used to combine conditional statements.

Operator Name Example Result
`and` And `\$x and \$y` True if both \$x and \$y are true
`or` Or `\$x or \$y` True if either \$x or \$y is true
`xor` Xor `\$x xor \$y` True if either \$x or \$y is true, but not both
`&&` And `\$x && \$y` True if both \$x and \$y are true
`||` Or `\$x || \$y` True if either \$\$x or \$y is true
`!` Not `!\$x` True if \$x is not true

## PHP String Operators

The following operators are used for string concatenation.

Operator Name Example Result
`.` Concatenation `\$str1 . \$str2` Concatenation of \$str1 and \$str2
`.=` Concatenation assignment `\$str1 .= \$str2` Appends the \$str2 to the \$str1

## PHP Array Operators

The following operators are used to compare Arrays.

Operator Name Example Result
`+` Union `\$x + \$y` Union of \$x and \$y
`==` Equality `\$x == \$y` True if \$x and \$y have the same key/value pairs
`===` Identity `\$x === \$y` True if \$x and \$y have the same key/value pairs in the same order and of the same types
`!=` Inequality `\$x != \$y` True if \$x is not equal to \$y
`<>` Inequality `\$x <> \$y` True if \$x is not equal to \$y
`!==` Non-identity `\$x !== \$y` True if \$x is not identical to \$y

## PHP Ternary Operator

The following operator is used to set a value depending on conditions.

Operator Name Example Result
?: Ternary \$x = expr1 ? expr2 : expr3 Returns the value of \$x.
The value of \$x is expr2 if expr1
= TRUE.
The value of \$x is expr3 if expr1 = FALSE

## PHP Data Types

PHP variables can store data of different types including simple string and numeric types to more complex data types like arrays and objects. PHP supports following primitive data types.

• Integer
• String
• Float
• Boolean
• Array
• Object
• NULL
• Resource

## PHP Integer Data Type

An integer data type is a whole number, without a decimal point, between -2,147,483,648 and 2,147,483,647. Integers can be specified in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation. In following examples we will use PHP var_dump() function which returns the data type and value.

<?php
\$x = 579;
var_dump(\$x);
?>

## PHP String Data Type

PHP string is sequence of characters, where every character is the same as a byte. A string can hold letters, numbers, and special characters and it can be as large as up to 2GB.

### Example

<?php
\$str1 = “Hello world!”;
\$str2 = ‘Hello world!’;

var_dump(\$str1);
echo “<br>”;
echo \$str2;
?>

## PHP Float Data Type

PHP float (also known as double, or real numbers) is a number with a decimal point or a number in exponential form.

<?php
\$x = 1.234;
var_dump(\$x);
?>

## PHP Boolean Data Type

A Boolean data type represents only two possible values: TRUE (1) or FALSE (0).

<?php
\$x = true;
\$y = false;
var_dump(\$x);
var_dump(\$y);
?>

## PHP Array Data Type

An array data type can store multiple values in single variable. An array is defined as an indexed collection of values where each index is unique and references a corresponding value.

### Example

<?php
\$colors = array(“Red”,”Grees”,”Blue”);
var_dump(\$colors);
?>

## PHP Object Data Type

An object data type stores data and information on how to process that data. An object is a specific instance of a class which serve as templates for objects. A class is a structure that can contain properties and methods. Objects are created based on this template via the new keyword.

Here’s a simple example of a class definition followed by the object creation.

<?php
class Car {
function Car() {
\$this->make = “Honda”;
}
}
// creating class object
\$myCar = new Car();

// displaying object properties
echo \$myCar->make;
?>

## PHP NULL Data Type

The NULL data type is used to represent variables without any data. NULL is the only possible value of type NULL.

### Example

<?php
\$x = NULL;
var_dump(\$x);
echo “<br>”;

\$x = “Hello World!”;
\$x = NULL;
var_dump(\$x);
?>

## PHP Resource Data Type

PHP resource is a special variable which holds a reference to a function or an external resource. The are typically used to opened files and database connections.

## PHP echo and print

The echo and print statements are used for displaying output on web page like string, numbers, variables values etc. They are used in almost every PHP script.

## PHP echo Statement

The echo statement is used to output data to the screen like string, numbers, variables values etc. The echo statement can be used to output HTML markup. We can use echo with or without parentheses: echo or echo().

### Example

<?php
echo “Hello World!”;
?>

## PHP echo Display Variables

The following example shows how to output variables with the echo statement.

<?php
\$num1 = 2;
\$num2 = 4;

echo \$num1;
echo “<br>The value of num2 is ” . \$num2 . “<br>”;
echo \$num1 + \$num2;
?>

## PHP print Statement

The print statement is an alternative to echo statement. It is used to output data on a web page. Like echo you can use print statement with or without parentheses like: print or print().

### Example

<?php
print “Hello World!”;
?>

## PHP print Display Variables

The following example shows how to output variables with the print statement.

<?php
\$num1 = 2;
\$num2 = 4;

print \$num1;
print “<br>The value of num2 is ” . \$num2 . “<br>”;
print \$num1 + \$num2;
?>

## Difference Between echo and print

The echo and print statements are almost same with small differences.

• echo has no return value while print has a return value of 1 so it can be used in expressions.
• echo can take multiple parameters while print can take one argument.
• echo is faster than print

## PHP Variables

Variables are used to store information, like numbers, strings etc. The value of a variable can change over the course of a script. In PHP, there is no need to declare a variable before assigning a value to it. PHP automatically assign correct data types to the variables depending on their values.

## Declaring PHP Variables

In PHP variable can be declared as: \$name = value;

### Example

<?php
\$int = 7;
\$txt = “Some text”;
\$float = 7.5;
?>

PHP has no command for declaring a variable. It is created the moment you first assign a value to it.

## PHP Variable Name Rules

• Variable starts with the \$ sign, followed by the name of the variable
• Variable name must start with a letter or the underscore character
• Variable name can only contain alpha-numeric characters(A-z, 0-9) and underscores , and (_)
• Variable names are case-sensitive

## PHP Variables Scope

The scope of a variable is the part of the script where the variable can be referenced/used. Variables can be declared anywhere in the script. PHP has three type of variable scopes.

• local
• global
• static

## PHP Local Variables

A variable declared within the body of a function is local variable and can only be accessed within that function.

### Example

<?php
function myVariable() {
\$var = 10;
echo “Variable var inside function is: \$var”;
}
myVariable();

echo “Variable var outside function is: \$var”;
?>

## PHP Global Variables

A variable declared outside the body of a function is global variable and can only be accessed outside a function.

### Example

<?php
\$var = 10;
function myVariable() {
echo “Variable var inside function is: \$var”;
}
myVariable();

echo “Variable var outside function is: \$var”;
?>

## PHP Variables global Keyword

The global keyword is used before variables inside a function to access a global variable inside of a function.

### Example

<?php
\$var = 10;
function myVariable() {
global \$var;
echo “Variable var inside function is: \$var”;
}
myVariable();

echo “Variable var outside function is: \$var”;
?>

## PHP Static Variables

All the values of local variables are deleted after the execution of a function. The static keyword is used to make local variables retain their values after the execution of a fuction.

<?php
function myVariable() {
static \$var = 10;
echo \$var;
echo “<br />”;
\$var++;
}

myVariable();
myVariable();
myVariable();
?>

Comments in PHP code are simple lines of text which are ignored by the PHP engine. Its only purpose is to make the code more readable. Comments are used to let other programmers understand your code or remind yourself of what you did with PHP while editing your own code.

PHP support single-line and multi-line comments. To write a single-line comment, you can put two slashes (//) or a hash symbol (#) at the beginning of the line.

### Example

<?php
// Single line comment using slashes
# Single line comment using hash symbol
echo “Hello World!”;
?>

To write multi-line comments in PHP, start the comment with a slash followed by an asterisk (/*) and end the comment with an asterisk followed by a slash (*/).

### Example

<?php
/*
This is a multi-line comment
Another comment line

*/
echo “Hello World!”;
?>

## PHP Syntax

A PHP script starts with the <?php and ends with the ?> tag. These tags tells the PHP engine to treat the enclosed code block as PHP code, rather than simple HTML. PHP files are plain text files with .php extension. A PHP file can contain HTML, JavaScript as well as PHP scripts for server side execution. All PHP statements end with a semicolon (;).

## PHP Syntax

<?php
//PHP code here
?>

Following PHP script uses a built-in PHP function “echo” to print “Hello World!” on a web page.

### Example

<!DOCTYPE html>
<html lang=”en”>
<title>PHP Hello World</title>
<body>
<h1><?php echo “Hello World!”; ?></h1>
</body>
</html>

## PHP Case Sensitivity

The variable names in PHP are case-sensitive which mean \$name, \$Name, and \$NAME are three different variables. However the keywords (e.g. if, else, print, while etc.), function and class names are case-insensitive. As a result calling the print or PRINT produce the same result.

## PHP Install

PHP script execute on a web server running PHP. To start using PHP, you can find a web host with PHP and MySQL installed or you can install a web server on your own PC, and then install PHP and MySQL.

## Install PHP on Your Own PC

To execute PHP script on your own PC you need the following program installed on your computer.

• The Apache Web Server
• The PHP Engine
• The MySQL Database Server

You can either install them individually or choose a pre-configured package for your operating system like XAMPP and WampServer. To install these programs individually you can follow the installation instructions provided on official PHP website (PHP.net): http://php.net/manual/en/install.php

## PHP Introduction

PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP stands for Hypertext Preprocessor. It is a very popular and widely-used open source server-side scripting language to write dynamically generated web pages. PHP scripts are executed on the server and the result is sent to the web browser as plain HTML. PHP 7 is the latest stable release.

PHP has several advantages over other server side scripting languages.

### Easy to learn

For beginner web programmers, PHP is often considered as the easier and preferable language to learn.

### Open source

PHP is an open-source project which is developed and maintained by a worldwide community of developers who make its source code freely available to download and use.

### Portability

PHP runs on various platforms such as Linux, Microsoft Windows, Mac OS etc. and it is compatible with almost all servers used today such Apache, IIS.

### Fast

PHP scripts usually execute faster than those written in other scripting languages like ASP, Ruby, Python, Java, etc.

### Vast Community

Since PHP is supported by the worldwide community, it is extremely easy to find help or documentation.

## PHP File

PHP files are stored with “.php” extension. A PHP file can contain text, HTML, CSS, JavaScript, and PHP code. The PHP code is executed on the server and returned to the browser as plain HTML.