0% found this document useful (0 votes)
35 views

Arithmetic Operators: Explain Different Types of Operators in PHP?

The document discusses different types of operators in PHP including arithmetic, assignment, bitwise, comparison, increment/decrement, logical, string, array, and type operators. It provides examples and explanations of common operators like addition, assignment, AND, equal to, increment, concatenation, and more. The document also briefly discusses how to create hyperlinks and define classes in PHP as well as the differences between client-side and server-side scripting.

Uploaded by

tulika konar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Arithmetic Operators: Explain Different Types of Operators in PHP?

The document discusses different types of operators in PHP including arithmetic, assignment, bitwise, comparison, increment/decrement, logical, string, array, and type operators. It provides examples and explanations of common operators like addition, assignment, AND, equal to, increment, concatenation, and more. The document also briefly discusses how to create hyperlinks and define classes in PHP as well as the differences between client-side and server-side scripting.

Uploaded by

tulika konar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Explain different types of operators in php?

 PHP Operators can be categorized in following forms:


Arithmetic Operator, Assignment Operators, Bitwise Operators, Comparison Operators, Incrementing/Decrementing
Operators, Logical Operators, String Operators, Array Operators, Type Operators, Execution Operators, Error Control
Operators.

Arithmetic Operators
The PHP arithmetic operators are used to perform common arithmetic operations such as addition, subtraction,
etc. with numeric values.

Operator Name Example Explanation

+ Addition $a + $b Sum of operands

- Subtraction $a - $b Difference of operands

* Multiplication $a * $b Product of operands

/ Division $a / $b Quotient of operands

% Modulus $a % $b Remainder of operands

** Exponentiation $a ** $b $a raised to the power $b

Assignment Operators
The assignment operators are used to assign value to different variables. The basic assignment operator is "=".

Operator Name Example Explanation

= Assign $a = $b The value of right operand is assigned to the left operand.

+= Add then Assign $a += $b Addition same as $a = $a + $b

-= Subtract then $a -= $b Subtraction same as $a = $a - $b


Assign

*= Multiply then $a *= $b Multiplication same as $a = $a * $b


Assign

/= Divide then $a /= $b Find quotient same as $a = $a / $b


Assign(quotient)

%= Divide then $a %= $b Find remainder same as $a = $a % $b


Assign(remainder)
Bitwise Operators
The bitwise operators are used to perform bit-level operations on operands. These operators allow the evaluation
and manipulation of specific bits within the integer.

Operator Name Example Explanation

& And $a & $b Bits that are 1 in both $a and $b are set to 1, otherwise 0.

| Or (Inclusive or) $a | $b Bits that are 1 in either $a or $b are set to 1

^ Xor (Exclusive or) $a ^ $b Bits that are 1 in either $a or $b are set to 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:

Operator Name Example Explanation

== Equal $a == $b Return TRUE if $a is equal to $b

=== 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

!= Not equal $a != $b Return TRUE if $a is not equal to $b

<> Not equal $a <> $b Return TRUE if $a is not equal to $b

< Less than $a < $b Return TRUE if $a is less than $b

> Greater than $a > $b Return TRUE if $a is greater than $b

<= 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

<=> Spaceship $a <=>$b Return -1 if $a is less than $b


Return 0 if $a is equal than $b
Return 1 if $a is grater then $b
Incrementing/Decrementing Operators
The increment and decrement operators are used to increase and decrease the value of a variable.

Operator Name Example Explanation

++ Increment ++$a Increment the value of $a by one, then return $a

$a++ Return $a, then increment the value of $a by one

-- decrement --$a Decrement the value of $a by one, then return $a

$a-- Return $a, then decrement the value of $a by one

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.

Operator Name Example Explanation

and And $a and $b Return TRUE if both $a and $b are true

Or Or $a or $b Return TRUE if either $a or $b is true

xor Xor $a xor $b Return TRUE if either $ or $b is true but not both

! Not ! $a Return TRUE if $a is not true

&& And $a && $b Return TRUE if either $a and $b are true

|| Or $a || $b Return TRUE if either $a or $b is true

String Operators
The string operators are used to perform the operation on strings. There are two string operators in PHP,
which are given below:

Operator Name Example Explanation

. Concatenation $a . $b Concatenate both $a and $b

.= 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.

Operator Name Example Explanation

+ Union $a + $y Union of $a and $b

== Equality $a == $b Return TRUE if $a and $b have same key/value pair

!= Inequality $a != $b Return TRUE if $a is not equal to $b

=== Identity $a === $b Return TRUE if $a and $b have same key/value pair of same type in same
order

!== Non-Identity $a !== $b Return TRUE if $a is not identical to $b

<> Inequality $a <> $b Return TRUE if $a is not equal to $b

How can you create hyperlink in PHP pages?


<?php
$target = "text.txt";
$linkname = "mylink";
link($target, $linkname);
?>
The link() function in PHP accepts two parameters.
1. target: It is a mandatory parameter which specifies the target.
2. link: It is a mandatory parameter which specifies the name of the link.
Return Value:
It returns TRUE on success or FALSE on failure.

How you will define a class in php?


A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}). All
its proper�es and methods go inside the braces:
Syntax:
<?php
class Fruit {
// code goes here...
}
?>

What is web applica�on?


A Web applica�on (Web app) is an applica�on program that is stored on a remote server and delivered over
the Internet through a browser interface. Web services are Web apps by defini�on and many, although not all,
websites contain Web apps. According to Web. AppStorm editor Jarel Remick, any website component that performs
some func�on for the user qualifies as a Web app.
Difference between client side scrip�ng and server side scrip�ng?

Client-side scripting Server-side scripting


Source code is visible to the user. Source code is not visible to the user because its output
of server-side is an HTML page.

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 runs on the user’s computer. It runs on the webserver.


There are many advantages linked with The primary advantage is its ability to highly customize,
this like faster. response
response times, a more interactive requirements, access rights based on user.
application.

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.

Write a PHP script to upload a file.


<?php
if(isset($_POST['upload']))
{
echo $_FILES['fi'];
}
?>
<html>
<body>
<form action="f.php" method="POST">
Upload file <input type="file" name="fi">
<input type="submit" name="Upload">
</form>
</body>
</html>

Explain the functions of current() and next().


current() - The current() function returns the value of the current element in an array.
Every array has an internal pointer to its "current" element, which is initialized to the first element inserted into the
array.
next() - The next() function moves the internal pointer to, and outputs, the next element in the array.
Difference between Sta�c and Dynamic Web Pages:
Sta�c Web Page Dynamic Web Page
In sta�c web pages, Pages will remain same un�l In dynamic web pages, Content of pages are different
someone changes it manually. for different visitors.
Sta�c Web Pages are simple in terms of complexity. Dynamic web pages are complicated.
In sta�c web pages, Informa�on are change rarely. In dynamic web page, Informa�on are change
frequently.
Sta�c Web Page takes less �me for loading than Dynamic web page takes more �me for loading.
dynamic web page.
In Sta�c Web Pages, database is not used. In dynamic web pages, database is used.
Sta�c web pages are writen in languages such as: Dynamic web pages are writen in languages such as:
HTML, JavaScript, CSS, etc. CGI, AJAX, ASP, ASP.NET, etc.
Sta�c web pages does not contain any applica�on Dynamic web pages contains applica�on program for
program . different services.
Sta�c web pages require less work and cost in designing Dynamic web pages require compara�vely more work
them. and cost in designing them.

What are the different types of errors in PHP?

• 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.

Parse errors are caused by:

• Unclosed brackets or quotes


• Missing or extra semicolons or parentheses
• Misspellings

Example:

<?php
echo "Red";
echo "Blue";
echo "Green"
?>

It is unable to execute because of the missing semicolon in the third line.

• 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.

There are three (3) types of fatal errors:

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)

For instance, the following script would result in a fatal error:

<?php
function sub()
{
$sub=6-1;
echo "The sub= ".$sub;
}
div();
?>

The output tells you why it is unable to compile.

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>

You might also like