A Guide About PHP 5 Constants and Operators

by Ahmed Khan  October 15, 2015

Here is another tutorial from our tutorial series beginners guide for PHP. Through this tutorial you will learn about PHP constants and operators concepts, syntax and their significance. You will also learn about assignment operators in detail through relevant examples and I have also covered ternary operators usage. These assignment operators will help you in your logical programming or conditional programming.

PHP 5 Constants And Operators

PHP Constants:

In constants, a simple value is assigned to a constants name. In contrast with the variable concept, constant value can’t be changed. They are similar to variables as they are also holders for keeping values.

A constant name should always begin with an underscore or a letter. Here we don’t use $ sign like variables declaration. Constants are always global throughout the PHP script. Declaring a constant requires the use of keyword define ().

Syntax:

Its general syntax is:

Name, value and case are the parameters of define. Name depicts the name of a constant. Value depicts the value of a constant. Case defines that constant name is case insensitive. Default value of third parameter is false. Consider the following example:

Example:

Output:

Example2:

Output:

Global scope of constants:

Constants have a global scope. This implies that they can be used anywhere within the script inside or outside the function. Consider the following example:

Example:

Output:

PHP Operators:

Operators are useful to perform different operations on variables and values. PHP operators can be categorized into following 7 categories:

  1. PHP Arithmetic Operators:

Arithmetic operators are very useful to perform basic mathematical operations like addition, subtraction, multiplication and division. The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc. Consider the following examples step by step to better understand the arithmetic operators:

Addition:

‘+’ is used to add numbers.

Example:

Output:

Subtraction:

‘-‘ is used to subtract numbers.

Example:

Output:

Multiplication:

‘*’ is used to multiply two numbers.

Example:

Output:

“/” is used to divide two numbers.

Example:

Output:

Modulus:

‘%’ is used to get the remainder value.

Example:

Output:

Exponential:

‘**’ is used to display the value $num1 raise to the power $num2.

Example:

Output:

PHP Assignment Operators:

These operators are used to assign values to variables . ‘=’ is called an assignment operator. It implies that left operand gets the value of assignment operator from the right.

Example1:

Output:

Example2:

‘+=’ adds value and assign to the same variable. It is similar to $x=$x+$y.

Output:

Example3:

‘-=’ subtracts value from the initial value and assign it to the same variable.It is similar to $x=$x-$y.

Output:

Example4:

‘*=’ multiplies 2 values value and then store it within the same variable. It behaves same as $x=$x*$y.

Output:

Example5:

You can use ‘’/=’ operator to do the same operation as $x=$x/$y.

Output:

Example6:

‘%=’ finds remainder value of two numbers division and then stores it in the same varialabe. It acts same as the $x=$x%$y.

Output:

PHP Comparison Operators:

These operators are used to compare two values. Through comparison operators you can compare two numbers or strings. We have the following list of comparison operators:

  1. Equal operator (‘==’)
  2. Identical operator(‘===’ )
  3. Not Equal operator(‘!=’ )
  4. Not Equal operator(‘<>’)
  5. Not Identical operator(‘!==’ )
  6. Greater than operator(‘>’ )
  7. Less than operator (‘<’ )
  8. Greater than or equal to(‘>=’ )
  9. Less than or equal to(‘<=’ )

Example1(Demonstration of Equal Operator):

Output:

Example2(Demonstration of Not Identical Operator):

Output:

Example3(Demonstration of Greater Than Operator):

Output:

Example4(Demonstration of Less than operator):

Output:

Example5(Demonstration of Greater Than Equal To Operator):

Output:

Example6(Demonstration of Less Than Equal To Operator):

Output:

PHP Increment/ Decrement Operators:

They are simply used to increment or decrement values. They are listed as below:

  • Pre increment operator is denoted by ‘++$a’. It increments a value by 1 and returns$a.
  • Post increment operators is denoted by ‘$a++’. It increments $a value by 1.
  • Pre decrement operator is denoted by ‘–$a’. It decrements $a value by 1 and returns $a
  • Post decrement operator is denoted by ‘$a–’. It returns $a and then decrements value by 1

Example1(Demonstration of Post Decrement Operator):

Output:

Example2(Demonstration of Pre Decrement Operator):

Output:

Example3(Demonstration of Post Increment Operator):

Output:

Example4(Demonstration of Pre Increment Operator):

Output:

PHP Logical Operators:

They are used in conditional statements.We have the following 4 main operators in this category:

And (&&) Operator:

It is the logical And operator. It returns true if both the variables are true. It is denoted by ‘and’ or ‘&&’. Consider the following example:

Example:

Output:

OR Operator:

It returns true if any of the variable is true. It is denoted by ‘OR’ or ||.

Example:

Output:

Xor operator:

It returns true only if one of any variable is true otherwise it returns false. It is denoted by keyword Xor.

Example:

Output:

Not operator:

It returns true if the given variable is not true or it is used to negate the condition.

Example:

Output:

PHP String Operators:

In PHP, there are two string operators.

Concatenation Operator:

It is denoted by ‘.’ . It is used to concatenate (link two statements).

Example:

Output:

Concatenation Assignment:

‘.=’ is used for concatenation assignment. These operators are specially designed for strings.

Example:

Output:

PHP Array Operators:

They are used to compare arrays. Array operators are explained with examples as below:

Union:

‘=’ is used for the union of two variables.

Example:

Output:

Equality:

‘==’ is called equality true. It only returns true if both the arrays have same values.

Example:

Output:

Identity:

‘===’ is used for this purpose. It retuns true if both the variables have same value pairs of the same type as well as order.

Example:

Output:

Inequality:

It is denoted by ‘!=’ . It returns true if both the array are unequal. Also it is denoted by ‘<>’

Example:

Output:

Non identity:

It is denoted by ‘!==’. It returns true if first variable is not equivalent to variable 2.

Example:

Output:

PHP Ternary Operator:

Another useful operator category is PHP ternary operator. It is denoted by “?:”. They are similar to if else statements.

Example:

Using if else we can write the code as:

Output:

You are a PHP Programmer.

The above code is exactly same as below:

Conclusion:

I have tried my best to cover the key concepts of almost all types of PHP Operators (php Arithmetic operators, php assignment operators, php logical operators, php string operators, php array operators and PHP ternary operators.) and PHP Constants. If I have missed out on anything big, then let me know in the comments section below!

Create PHP websites without the worry of server management.

Deploy your PHP app on optimized PHP hosting servers.

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...