🎁GET 20% OFF Using Code 👉SCHOOL👈 Until End 2024🎁

Increment And Decrement Operators

PHP increment operators are used to increment a variable’s value and decrement operators are used to decrement a variable’s value. PHP also supports pre- and post-increment and decrement operators allowing for the increment or decrement of the value by one.

Table : Increment And Decrement Operators

Increment / DecrementNameExample
++$vatPre-increment++$num1
$var++Post-increment$num1++
–$varPre-decrement–$num1
$var–Post-decrement$num1–
Sample Code<?php
$num1 = 5;
$num2 = 10;
$num1++; //Post-increment
echo num1;
$num2--; // Post-decrement
echo $num2;
++$num1; //Pre-increment
echo num1;
--$num2; // Pre-decrement
echo $num2;
?>
<?php // Type Some Code ?>

Comparison Operators

Comparison operators compare two values which can be numbers of strings.

Table : Comparison Operators

ComparisionNameExample
==Equal$num1 == $num2
===Identical$num1 === $num2
!=Not Equal$num1 != $num2
<>Not Equal$num1 <> $num2
!==Not Identical$num1 !== $num2
>Greater Than$num1 > $num2
<Less Than$num1 < $num2
>=Greater Than Or Equal To$num1 >= $num2
<=Less Than Or Equal To$num1 <= $num2
<=>Spaceship$num1 <=> $num2
Sample Code<?php
$name1 = "John";
$name2 = "James";
var_dump($name1 == $name2);
var_dump($name1 == 10); 
$num1 = 12;
$num2 = 22;
var_dump($ num1 > $num2);
var_dump($num1 == 10);
?>
<?php // Type Some Code ?>

Assignment Operators

Assignment operators are used to define and manipulate scalar variables and not arrays. Scalar variables have the same naming convention where the dollar symbol $ is prepend. Therefore arithmetic types are scalar types. Arrays are aggregate types.

Table : Assignment Operators

AssignmentEquivalentDescription
$num1 = $num2$num1 = $num2Set left operand to same value on right
$num1 += $num2$num1 = $num1 + $num2Addition
$num1 -= $num2$num1 = $num1 – $num2Subtraction
$num1 *= $num2$num1 = $num1 * $num2Multiplication
$num1 /= $num2$num1 = $num1 / $num2Division
$num1 %= $num2$num1 = $num1 % $num2Modulus
Sample Code<?php
$num = 1;
$counter = 1; 
$num += $counter; // Arithmetic Assignment Operator
echo $num;

$greeting = 'Hello ';
$name = 'John';
$greeting .= $name; // Concatenation Assignment Operator
echo $greeting;
?>
<?php // Type Some Code ?>

Arithmetic Operators

The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.

Table : Arithmetic Operators

OperatorNameExample
+Addition Or Sum$num1 + $num2
Subtraction Or Difference$num1 – $num2
*Multiplication Or Product$num1 * $num2
/Division Or Quotient$num1 / $num2
%Modulus Or Division Remainder$num1 % $num2
**Exponentiation Or Raising Power$num1 ** $num2
Sample Code<?php
$num1 = 5;
$num2 = 3;
echo ($num1 % $num2)."\n"; // Prints 2
echo ($num1 % $num3)."\n"; // Prints 2
$num1 = -5; // Set To Negative Value
echo ($num1 % $num2)."\n"; // prints -2
$num2 = -3; // Set To Negative Value
echo ($num1 % $num2)."\n"; // prints -2
?>
<?php // Type Some Code ?>

String Formatting

PHP double quoted strings can format strings using defined variables.

Newline character \n causes a line break when using double quotes.

Concatenation character . and concatenation assignment is .= to combine two strings.

Sample Code<?php
$name = "Jake\nRoberts";
echo "Your name is $name";    // prints Your name is Jake and Roberts on next line
$name .= " Roberts"; // Appends to string via concatenation assignment
echo "My name is" . $name; // Concatenation of string
 ?>

Table : Common Functions For Manipulating Strings

FunctionDescriptionExample
strlen()Gets string lengthstrlen($name);
substr()Return part of stringsubstr($name, -1);
explode()Split a string by a stringexplode(“.”, $height);
implode()Join array elements with a stringimplode(“,”, [“name”,”email”);

<?php // Type Some Code ?>

Variables

Variables store information such as values or other variables.

PHP variables are declared using the dollar symbol “$” before the name. PHP variables can be reassigned values using the assignment operator “=”, making it possible to assign a new value.

PHP string data types can be concatenated using the concatenation operator “.” making it easy to join strings.

Variables cannot start with a number because they must start with an underscore or letter. The second character can contain only alphanumeric characters or underscores.

Variable names are case-sensitive.

Sample Code<?php
    $name = 'John';  // variable of string type being declared and initialized
    $age = 18;       // variable of integer type being declared and initialized
    $height = 5.3;   // variable of double type being declared and initialized
    echo $name . ' is ' . $height . "m tall\n"; // concatenating variables and strings
    echo "$name is $age years old."; // interpolating variables to string
?>
<?php // Type Some Code ?>

Comments

Single Line Comments

Double forward slash sequence // will mark all text until a newline as a comment.

Single Line Comment Example

Sample Code<?php
// This is a single-line comment.
?>

C-Style/Block Comments

The sequence /* is used to declare the start of the comment block and the sequence */ is used to declare the end of comment. All text between the start and end sequences is interpreted as a comment, even if the text is otherwise valid PHP syntax. These are sometimes called “C-style” comments.

Sample Code<?php
/* A block comment with the symbol /*
Note that the compiler is not affected by the second /*
however, once the end-block-comment symbol is reached,
the comment ends.
*/
?>
<?php // Type Some Code ?>

Syntax

PHP looks for opening tag <?php and closing tag ?> in order to determine when to start and stop interpreting code.

PHP can also use the short echo tag <?= as short-hard for <? echo. Short tags must be enabled in the php.ini configuration or during the PHP build.

The closing tag can be eliminated if a file only contains PHP code.

Hello World Example

Sample Code<?php echo "Hello"; ?>

Hello World Short Tags Example

Sample Code<?='Hello, World!'; ?>

IDE

<?php // Type Some Code ?>

About

Edward Ojambo has been programming in various languages since 1985. He works as a freelance programmer creating custom databases, web, desktop and mobile applications. Private one-on-one tutorials are offered for computer programming, website development, Linux, email servers, web servers and search engine optimization.

All content was created for educational purposes and is not affiliated with official PHP. The content was extracted from examples that Edward Ojambo uses during work-related activities.

The content is released under Creative Commons BY-SA 4.0, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.

Use the content present in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to OjamboShop.com.

Basic Data Types

String is a sequence of characters inside double or single quotes.

Integer is a non-decimal whole number and can be either positive or negative.

Float for floating point numbers also called double with a decimal point.

Boolean can only be TRUE or FALSE.

Array used to store multiple values in a single variable.

Object is an instance of a class which is a template for objects.

Null has no assigned value.

Resource stores a reference to functions and resources external to PHP such as a database call.

Sample Code<?php
$my_string = "Hello world!";
$my_string2 = 'Hello world!';
$my_integer = 2024;
$my_float = 2024.12;
$my_boolean = TRUE;
$my_string_array = ["One", "Two", "Three"];
class Greeting {
  public $name;
  public $age;
  public function __construct($name, $age) {
    $this->name = $name;
    $this->age = $age;
  }
  public function message() {
    return ‘My name is a ‘ . $this->name . ‘ and I am ‘ . $this->age . ‘ years old!’;
  }
}
$my_greeting_object = new Car(“John”, 23);
// Display Output Of Variables
echo $my_string;
echo $my_string2;
echo $my_integer;
echo $my_float;
echo $my_boolean;
var_dump($my_string_array);
echo $my_greeting_object;
?>
<?php // Type Some Code ?>