How many different data types are available in php?
- A. 6
- B. 7
- C. 8
- D. 9
How many different data types are available in php?
- A. 6
- B. 7
- C. 8
- D. 9
Which one is not a data type in PHP?
- A. Resources
- B. Objects
- C. Null
- D. Void
Which one is not a data type in PHP?
- A. Resources
- B. Objects
- C. Null
- D. Void
The range of integers must lie between ?
- A. -2^15 to 2^15.
- B. -2^16 to 2^16.
- C. -2^31 to 2^31.
- D. -2^32 to 2^32.
The range of integers must lie between ?
- A. -2^15 to 2^15.
- B. -2^16 to 2^16.
- C. -2^31 to 2^31.
- D. -2^32 to 2^32.
Objects are defined as instances of user defined classes that can hold
?
- A. values
- B. functions
- C. both values and functions
- D. None of the above
Objects are defined as instances of user defined classes that can hold
?
- A. values
- B. functions
- C. both values and functions
- D. None of the above
What will be the output of the following PHP code?
<?php
$lfc = NULL; echo $lfc;
?>
- 0
- Null
- No Output
- Error
What will be the output of the following PHP code?
<?php
$lfc = NULL; echo $lfc;
?>
- 0
- Null
- No Output
- Error
What will be the output of the following PHP code?
- <?php
- $intArray = array( 10, 20 , 30);
- echo “First Element: $intArray[3]
- “;
- ?>
- A. First Element: 10
- B. First Element: 20
- C. First Element: 30
- D. Warning: Undefined array key
What will be the output of the following PHP code?
- <?php
- $intArray = array( 10, 20 , 30);
- echo “First Element: $intArray[3]
- “;
- ?>
- A. First Element: 10
- B. First Element: 20
- C. First Element: 30
- D. Warning: Undefined array key
Which data type in PHP offers special variables that hold the references of resources that are external to PHP?
- A. Array
- B. Resources
- C. object
- D. string
Do you know?
The first-ever website, created by Tim Berners-Lee, went live on August 6, 1991. It was a simple page describing the World Wide Web project and how to use hypertext to access documents.
PHP OPERATORS
Introduction
- Operators are symbols that tell the PHP processor to perform certain actions.
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- String operators
- Array operators
- Conditional assignment operators
- Logical operators
Arithmetic operators
- The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.
Operator | Description | Example | Result |
+ | Addition | $x + $y | Sum of $x and $y |
– | Subtraction | $x – $y | Difference of $x and
$y. |
* | Multiplication | $x * $y | Product of $x and $y. |
/ | Division | $x / $y | Quotient of $x and
$y |
% | Modulus | $x % $y | Remainder of $x
divided by $y |
** | Exponentiation | $x ** $y | Result of raising $x to the $y’th power |
Arithmetic operators(contd.)
<?php
$x = 10;
$y = 4;
echo($x + $y); // 0utputs: 14 echo “<br>”;
echo($x – $y); // 0utputs: 6
echo “<br>”;
echo($x * $y); // 0utputs: 40 echo “<br>”;
echo($x / $y); // 0utputs: 2.5 echo “<br>”;
echo($x % $y); // 0utputs: 2
echo “<br>”;
echo($x ** $y); // 0utputs: 10000
?>
Assignment operators
- The PHP assignment operators are used with numeric values to write a value to a variable.
Operator | Description | Example | Is The Same As |
= | Assign | $x = $y | $x = $y |
+= | Add and assign | $x += $y | $x = $x + $y |
-= | Subtract and assign | $x -= $y | $x = $x – $y |
*= | Multiply and assign | $x *= $y | $x = $x * $y |
/= | Divide and assign quotient | $x /= $y | $x = $x / $y |
%= | Divide and assign modulus | $x %= $y | $x = $x % $y |
Assignment operators(contd.)
<?php
$x = 10;
echo $x; // Outputs: 10
echo “<br>”;
$x = 20;
$x += 30;
echo $x; // Outputs: 50 echo “<br>”;
$x = 50;
$x -= 20;
echo $x; // Outputs: 30
echo “<br>”;
$x = 5;
$x *= 25;
echo $x; // Outputs: 125 echo “<br>”;
$x = 50;
$x /= 10;
echo $x; // Outputs: 5
echo “<br>”;
$x = 100;
$x %= 15;
echo $x; // Outputs: 10
?>
Assignment operators(contd.)
OUTPUT: 10
50
30
125
5
10
Do you know?
Rasmus Lerdorf created PHP (Personal Home Page) in 1994, originally as a simple tool to track visitors to his online resume.
Comparison operators
- The comparison operators are used to compare two values in a Boolean fashion.
Comparison operators(contd.)
Operator | Name | Example | Result |
== | Equal | $x == $y | True if $x is equal to $y |
=== | Identical | $x === $y | True if $x is equal to $y, and
they are of the same type |
!= | Not equal | $x != $y | True if $x is not equal to $y |
<> | Not equal | $x <> $y | True if $x is not equal to $y |
!== | Not identical | $x !== $y | True if $x is not equal to $y, or they are not of the same type |
< | Less than | $x < $y | True if $x is less than $y |
> | Greater than | $x > $y | True if $x is greater than $y |
>= | Greater than or equal to | $x >= $y | True if $x is greater than or equal to $y |
<= | Less than or equal to | $x <= $y | True if $x is less than or equal to $y |
Comparison operators(contd.)
<?php
$x = 25;
$y = 35;
$z = “25”;
var_dump($x == $z); // Outputs: boolean true var_dump($x === $z); // Outputs: boolean false var_dump($x != $y); // Outputs: boolean true var_dump($x !== $z); // Outputs: boolean true var_dump($x < $y); // Outputs: boolean true var_dump($x > $y); // Outputs: boolean false var_dump($x <= $y); // Outputs: boolean true var_dump($x >= $y); // Outputs: boolean false
?>
Do you know?
“Source code affects search engine ranking”
Incrementing and Decrementing Operators
- The increment/decrement operators are used to increment/decrement a variable’s value.
Operator | Name | Effect |
++$x | Pre-increment | Increments $x by one, then returns $x |
$x++ | Post-increment | Returns $x, then
increments $x by one |
–$x | Pre-decrement | Decrements $x by one, then returns $x |
$x– | Post-decrement | Returns $x, then decrements $x by one |
Incrementing and Decrementing Operators(contd.)
<html>
<?php
$x = 10;
echo ++$x; // Outputs: 11 echo $x; // Outputs: 11 echo “<br>”;
$x = 10;
echo $x++; // Outputs: 10 echo $x; // Outputs: 11 echo “<br>”;
$x = 10;
echo –$x; // Outputs: 9 echo $x; // Outputs: 9 echo “<br>”;
$x = 10;
echo $x–; // Outputs: 10 echo $x; // Outputs: 9
?>
</html>
Incrementing and Decrementing Operators(contd.)
OUTPUT: 1111
1011
99
109
String Operators
- The string operators are used to perform the operation on strings.
Operator | Description | Example | Result |
. | Concatenation | $str1 . $str2 | Concatenation of
$str1 and $str2 |
.= | Concatenation assignment | $str1 .= $str2 | Appends the $str2 to the $str1 |
String Operators(contd.)
<?php
$x = “Hello”;
$y = ” World!”;
echo $x . $y; // Outputs: Hello World! echo “<br>”;
$x .= $y;
echo $x; // Outputs: Hello World!
?>
OUTPUT:
Hello World!
Hello World!
PHP Conditional Assignment Operators
- The PHP conditional assignment operators are used to set a value depending on conditions:
?: | Ternary | $x = expr1 ? expr2 : expr3 | Returns the value of $x.
The value of $x is expr2 if expr1 = TRUE. The value of $x is expr3 if expr1 = FALSE |
?? | Null coalescing | $x = expr1 ?? expr2 | Returns the value of $x.
The value of $x is expr1 if expr1 exists, and is not NULL. If expr1 does not exist, or is NULL, the value of $x is expr2. Introduced in PHP 7 |
PHP Conditional Assignment Operators(contd.) – Ternary operator
<?php
echo $status = (empty($user)) ? “anonymous” : $user; echo “<br>”;
$user = “Michael”;
echo $status = (empty($user)) ? “anonymous” : $user;
?> OUTPUT:
anonymous Michael
PHP Conditional Assignment Operators(contd.) – Null coalescing
<?php
echo $status = $user ?? ‘anonymous’; echo “<br>”;
$user = “Michael”;
echo $status = $user ?? ‘anonymous’;
?>
OUTPUT:
anonymous Michael
Logical Operators
- The logical operators are typically used to combine conditional statements.
Operator | Name | Example | Result |
and | And | $x and $y | True if both $x and $y are
true |
or | Or | $x or $y | True if either $x or $y is
true |
xor | Xor | $x xor $y | True if either $x or $y is
true, but not both |
&& | And | $x && $y | True if both $x and $y are true |
|| | Or | $x || $y | True if either $x or $y is true |
! | Not | !$x | True if $x is not true |
Logical Operators(contd.)
<?php
$year = 2014;
// Leap years are divisible by 400 or by 4 but not 100
if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 ==
0))){
echo “$year is a leap year.”;
} else{
echo “$year is not a leap year.”;
}
?>
OUTPUT:
2014 is not a leap year.
Do you know?
Web 2.0: Coined around 2004, the term “Web 2.0” represented a shift in focus from static web pages to more dynamic and user-centered
experiences, | including | social media, | user- |
generated | content, | and interactive | web |
applications. |
Which of the following is type of operators in PHP?
- A. Arithmetic Operators
- B. Comparison Operators
- C. Assignment Operators
- D. All of the above
Which of the following is type of operators in PHP?
- A. Arithmetic Operators
- B. Comparison Operators
- C. Assignment Operators
D. All of the above
Operator ++ indicate?
- A. Decrement operator
- B. Increment operator
- C. Modulus Operator
- D. None of the above
Operator ++ indicate?
- A. Decrement operator
- B. Increment operator
- C. Modulus Operator
- D. None of the above
Operator ? Means?
- A. Assignment Operators
- B. Modulus AND assignment operator
- C. Conditional Operator
- D. Divide AND assignment operator
Operator ? Means?
- A. Assignment Operators
- B. Modulus AND assignment operator
- C. Conditional Operator
- D. Divide AND assignment operator
[pdf_note link=”https://drive.google.com/file/d/1P2LPxjB16pNj3L_p00COFOACpHd3fn8d/view”]