Arithmetic Operators: Explain Different Types of Operators in PHP?
Arithmetic Operators: Explain Different Types of Operators in PHP?
Arithmetic Operators
The PHP arithmetic operators are used to perform common arithmetic operations such as addition, subtraction,
etc. with numeric values.
Assignment Operators
The assignment operators are used to assign value to different variables. The basic assignment operator is "=".
& And $a & $b Bits that are 1 in both $a and $b are set to 1, otherwise 0.
~ Not ~$a Bits that are 1 set to 0 and bits that are 0 are set to 1
<< Shift left $a << $b Left shift the bits of operand $a $b steps
>> Shift right $a >> $b Right shift the bits of $a operand by $b number of places
Comparison Operators
Comparison operators allow comparing two values, such as number or string. Below the list of comparison
operators are given:
=== Identical $a === $b Return TRUE if $a is equal to $b, and they are of same data type
!== Not identical $a !== $b Return TRUE if $a is not equal to $b, and they are not of same
data type
<= Less than or equal to $a <= $b Return TRUE if $a is less than or equal $b
>= Greater than or equal to $a >= $b Return TRUE if $a is greater than or equal $b
Logical Operators
The logical operators are used to perform bit-level operations on operands. These operators allow the
evaluation and manipulation of specific bits within the integer.
xor Xor $a xor $b Return TRUE if either $ or $b is true but not both
String Operators
The string operators are used to perform the operation on strings. There are two string operators in PHP,
which are given below:
.= Concatenation and $a .= $b First concatenate $a and $b, then assign the concatenated string
Assignment to $a, e.g. $a = $a . $b
Array Operators
The array operators are used in case of array. Basically, these operators are used to compare the values of
arrays.
=== Identity $a === $b Return TRUE if $a and $b have same key/value pair of same type in same
order
Its main function is to provide the Its primary function is to manipulate and provide access
requested output to the end user. to the respective database as per the request.
It usually depends on the browser and its In this any server-side technology can be used and it
version. does not
depend on the client.
It does not provide security for data. It provides more security for data.
It is a technique used in web development It is a technique that uses scripts on the webserver to
in which scripts run on the client’s browser. produce a response that is customized for each client’s
request.
HTML, CSS, and javascript are used. PHP, Python, Java, Ruby are used.
No need of interaction with the server. It is all about interacting with the servers.
It reduces load on processing unit of the It surges the processing load on the server.
server.
• Warning Error
A warning error in PHP does not stop the script from running. It only warns you that there is a problem, one that
is likely to cause bigger issues in the future.
The most common causes of warning errors are:
• Calling on an external file that does not exist in the directory
• Wrong parameters in a func�on
For instance:
<?php
echo "Warning error"';
include ("external_file.php");
?>
As there is no “external_file”, the output displays a message, no�fying it failed to include it. S�ll, it doesn’t stop
execu�ng the script.
• No�ce Error
No�ce errors are minor errors. They are similar to warning errors, as they also don’t stop code execu�on. O�en, the
system is uncertain whether it’s an actual error or regular code. No�ce errors usually occur if the script needs access
to an undefined variable.
Example:
<?php
$a="Defined error";
echo "Notice error";
echo $b;
?>
In the script above, we defined a variable ($a), but called on an undefined variable ($b). PHP executes the script but
with a no�ce error message telling you the variable is not defined.
• Parse Error (Syntax)
Parse errors are caused by misused or missing symbols in a syntax. The compiler catches the error and terminates the
script.
Example:
<?php
echo "Red";
echo "Blue";
echo "Green"
?>
• Fatal Error
Fatal errors are ones that crash your program and are classified as cri�cal errors. An undefined func�on or class in the
script is the main reason for this type of error.
1. Startup fatal error (when the system can’t run the code at installa�on)
2. Compile �me fatal error (when a programmer tries to use nonexistent data)
3. Run�me fatal error (happens while the program is running, causing the code to stop working completely)
<?php
function sub()
{
$sub=6-1;
echo "The sub= ".$sub;
}
div();
?>
How can you create, access and delete a cookie in PHP with example.
• Crea�ng Cookies: Crea�ng a cookie named Auc�on_Item and assigning the value Luxury Car to it. The cookie will
expire a�er 2 days (2 days * 24 hours * 60 mins * 60 seconds).
Example -
<!DOCTYPE html>
<?php
setcookie("Auc�on_Item", "Luxury Car", �me() + 2 * 24 * 60 * 60);
?>
<html>
<body>
<?php
echo "cookie is created."
?>
<p>
<strong>Note:</strong>
You might have to reload the
page to see the value of the cookie.
</p>
</body>
</html>
• Accessing Cookie Values: For accessing a cookie value, the PHP $_COOKIE super global variable is used. It is an
associa�ve array that contains a record of all the cookies values sent by the browser in the current request. The
records are stored as a list where the cookie name is used as the key. To access a cookie named “Auc�on_Item”, the
following code can be executed.
Example –
<!DOCTYPE html>
<?php
setcookie("Auc�on_Item", "Luxury Car", �me() + 2 * 24 * 60 * 60);
?>
<html>
<body>
<?php
echo "Auc�on Item is a " . $_COOKIE["Auc�on_Item"];
?>
<p>
<strong>Note:</strong>
You might have to reload the page
to see the value of the cookie.
</p>
</body>
</html>
• Dele�ng Cookies: The setcookie() func�on can be used to delete a cookie. For dele�ng a cookie, the setcookie()
func�on is called by passing the cookie name and other arguments or empty strings but however this �me, the
expira�on date is required to be set in the past. To delete a cookie named “Auc�on_Item”, the following code can
be executed.
Example –
<?php
setcookie("Auc�on_Item", "Luxury Car", �me() + 2 * 24 * 60 * 60);
?>
<html>
<body>
<?php
setcookie("Auc�on_Item", "", �me() - 60);
?>
<?php
echo "cookie is deleted"
?>
<p>
<strong>Note:</strong>
You might have to reload the page
to see the value of the cookie.
</p>
</body>
</html>
Write a PHP program to find the number of vowels and consonants of the given string.
<html>
<body>
<?php
//Counter variable to store the count of vowels and consonant
$vCount = 0;
$cCount = 0;
//Declare a string
$str = "My name is Richik Das";
//Conver�ng en�re string to lower case to reduce the comparisons
$str = strtolower($str);
for ($i = 0; $i < strlen($str); $i++) {
//Checks whether a character is a vowel
if ($str[$i] == 'a' || $str[$i] == 'e' || $str[$i] == 'i' || $str[$i] == 'o' || $str[$i] == 'u') {
//Increments the vowel counter
$vCount++;
}
//Checks whether a character is a consonant
else if ($str[$i] >= 'a' && $str[$i] <= 'z') {
//Increments the consonant counter
$cCount++;
}
}
echo "Number of vowels : ", $vCount;
echo "<br>";
echo "\nNumber of consonants : ", $cCount;
?>
</body>
</html>