0

PHP Operators

September 19, 2022
Share

In programming, we need sometimes to carry out some computations or we may be interested in comparing some logic to reach a given decision. For example, we may be writing a program that will automatically return the tuition balance of a student using the total amount and the amount paid. Then basing on the balance, we determine if a student can do exams or not. This is when operators set in, they are used to perform such computations and comparisons on variables and values. PHP has a number of operators and below are some of them:

  • Assignment operators
  • Arithmetic operators
  • Comparison operators
  • Logical operators
  • String operators
  • etc.

Assignment operators

These are used to assign values to variables. The major operator used here is the equal sign we talked about earlier. The equal sign is used to assign values to variables but not for comparison purposes.

<?php


   $hi = "Hello world!";

?>

Arithmetic operators

These are used for carrying out mathematical operations on values and variables like addition (+), subtraction (-), division (/), multiplication (*).

When carrying out this kind of math, there a concept of operator precedence — BODMAS i.e. figures in brackets is worked on first, then division, multiplication, addition and subtraction comes last.

<?php

	$tuition = 1500000;
        $functionalFees = 30000;
	$amountPaid = 700000 + $functionalFees;
	$balance = $tuition - $amountPaid;

	echo "The balance is $balance";


?>

Comparison Operators

They are used when comparing values to reach a decision. They include greater than >, less than >, greater or equal >=, lesser or equal <=, equal ==, not equal !=, etc. For example, we want to determine if a student qualified to sit exams based on the tuition balance. If the balance is 400000 and above, the student cannot do exams and the reverse is true.

<?php

	$tuition = 1500000;
	$amountPaid = 700000;
	$balance = $tuition - $amountPaid;


	if ($balance >= 400000) {
		echo "You are not allowed to do exams.";
	} else {
		echo "You are allowed to do exams.";
	}

?>

In above snippet, you can see how comparison operators were used to achieve decision making using an if statement. You can try out other comparison operators following the above example.

Logical Operators

They work with comparison operators. However, they help us make a decision using multiple conditions. They include logical AND represented as && or and (measures if both conditions are true), logical OR represented as || or or (measures if one of the conditions is true) and logical NOT represented as ! or not (measures if the condition is not true). For example, we could say if the student’s tuition is above 500,000 but is a girl, then they can do exams or else, they are not allowed to do exams. For that case, we introduce in another variable to help us in determining the gender.

<?php

	$tuition = 1500000;
	$amountPaid = 700000;
        $gender = "female"
	$balance = $tuition - $amountPaid;


	if ($balance >= 500000 && $gender == "female") {
		echo "You are allowed to do exams.";
	} else {
		echo "You are not allowed to do exams.";
	}

?>

String operators

Sometimes we want to join or concatenate values in variables. We use the string operator for this. The most common one used under this is the concatenation operator represented using a dot .

<?php

	$tuition = 1500000;
	$student = "Peter Bruce";

        echo $student . "has to pay an amount of " . $tuition; 

?>

Now that you are familiar with the operators, you can start writing your own small programs while following the examples above 🤑