“Don’t be pushed by your problems. Be led by your dreams”

Can we change the value of constant during the execution of the script.

  • A. can
  • B. can not
  • C. depends on data type
  • D. depends on variable

Can we change the value of constant during the execution of the script.

  • A. can
  • B. can not
  • C. depends on data type
  • D. depends on variable

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

What will be the output of the following PHP code?

$y = 2;

if (–$y == 2 || $y xor –$y)

{

echo $y;

}

  1. 1
  2. 0
  3. 2
  4. no output

What will be the output of the following PHP code?

$y = 2;

if (–$y == 2 || $y xor –$y)

{

echo $y;

}

  1. 1
  2. 0
  3. 2
  4. no output

Explanation

–$y == 2 is false but y is decremented, the xor gives true if only one of the operands are true, thus 1 xor 0 is true.

Operator Precedence Table

Operators P

recedence

Associativity Operators Additional Information
(n/a) clone new clone and new
right ** arithmetic
(n/a) + – ++ —

~ (int) (float) (string) (array) (object) (bool) @

arithmetic (unary + and -), increment/decrement, bitwise, type casting and error control
left instanceof type
(n/a) ! logical
left * / % arithmetic
left + – . arithmetic (binary + and -), array and string (. prior to PHP 8.0.0)
left << >> bitwise
left . string (as of PHP 8.0.0)
non-associative < <= > >= comparison
non-associative == != === !== <> <=> comparison
left & bitwise and references
left ^ bitwise
left | bitwise
left && logical
left || logical
right ?? null coalescing
non-associative ? : ternary (left-associative prior to PHP 8.0.0)
right = += –

= *= **= /= .= %= &= |= ^= <<= >>= ??=

assignment
(n/a) yield from yield from
(n/a) yield yield
(n/a) print print
left and logical
left xor logical
left or logical

CONTROL STRUCTURES

Conditional Statements

  • PHP also allows you to write code that perform different actions based on the results of a logical or comparative test conditions at run time.
  • This means, you can create test conditions in the form of expressions that evaluates to either true or false and based on these results you can perform certain actions.

Conditional Statements(contd.)

  • There are several statements in PHP that you can use to make decisions:
    • The if statement
    • The if…else statement
    • The if…elseif….else statement
    • The switch…case statement

Do you know?

  • 244M sites were developed using PHP language in the world. So it is the most popular programming language in the field of web development.

The if Statement

  • The if statement is used to execute a block of code only if the specified condition evaluates to true.
  • This is the simplest PHP’s conditional statements and can be written like:

<?php

$d = date(“D”);

if($d == “Fri”){

echo “It’s Fridayyyy!”;

}

?>

The if Statement(contd.)

<?php

$t = date(“H”);

if ($t < “20”) {

echo “Have a good day!”;

}

?>

The if Statement(contd.)

<?php

$num=12; if($num<100){

echo “$num is less than 100”;

}

?>

OUTPUT:

12 is less than 100

The if…else Statement

  • The if…else statement allows you to execute one block of code if the specified condition is evaluates to true and another block of code if it is evaluates to false.

<?php

$d = date(“D”);

if($d == “Fri”){

echo “Have a nice weekend!”;

} else{

echo “Have a nice day!”;

}

?>

The if…else Statement(contd.)

<?php

$t = date(“H”);

if ($t < “20”) {

echo “Have a good day!”;

} else {

echo “Have a good night!”;

}

?>

The if…else Statement(contd.)

<?php

$num=13; if($num%2==0){

echo “$num is even number”;

}else{

echo “$num is odd number”;

}

?>

OUTPUT:

13 is odd number

The if…elseif…else Statement

  • The if…elseif…else a special statement that is used to combine multiple if…else statements.

<?php

$d = date(“D”);

if($d == “Fri”){

echo “Have a nice weekend!”;

} elseif($d == “Sun”){

echo “Have a nice Sunday!”;

} else{

echo “Have a nice day!”;

}

?>

The if…elseif…else Statement(contd.)

<?php

$t = date(“H”);

if ($t < “10”) {

echo “Have a good morning!”;

} elseif ($t < “20”) {

echo “Have a good day!”;

} else {

echo “Have a good night!”;

}

?>

The if…elseif…else Statement(contd.)

<?php

$marks=69;

if ($marks<33){

echo “fail”;

}

else if ($marks>=34 && $marks<50) { echo “D grade”;

}

else if ($marks>=50 && $marks<65) { echo “C grade”;

}

else if ($marks>=65 && $marks<80) { echo “B grade”;

OUTPUT:

B grade

}

else if ($marks>=80 && $marks<90) { echo “A grade”;

}

else if ($marks>=90 && $marks<100) {

echo “A+ grade”;

}

else {

echo “Invalid input”;

}

?>

The Ternary Operator

  • The ternary operator provides a shorthand way of writing the if…else statements.
  • The ternary operator is represented by the question mark (?) symbol and it takes three operands: a condition to check, a result for true, and a result for false.

<?php

$age = 15;

echo ($age < 18) ? ‘Child’ : ‘Adult’;

?>

OUTPUT:

Child

PHP nested if Statement

  • The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.

<?php

$age = 23;

$nationality = “Indian”;

//applying conditions on nationality and age if ($nationality == “Indian”)

{

if ($age >= 18) {

echo “Eligible to give vote”;

}

else {

echo “Not eligible to give vote”;

}

}

?>

OUTPUT:

Eligible to give vote

PHP switch Statement

  • The switch statement is used to perform different actions based on different conditions.
  • Use the switch statement to select one of many blocks of code to be executed.

PHP switch Statement(contd.)

<?php

$num=20; switch($num){ case 10:

echo(“number is equals to 10”); break;

case 20:

echo(“number is equal to 20”); break;

case 30:

echo(“number is equal to 30”); break;

default:

echo(“number is not equal to 10, 20 or 30”);

}

?>

OUTPUT:

number is equal to 20

PHP switch statement with character

<?php

$ch = ‘k’; switch($ch){ case ‘a’:

echo ‘It is a vowel’; break;

case ‘e’:

echo ‘It is a vowel’; break;

case ‘i’:

echo ‘It is a vowel’; break;

case ‘o’:

echo ‘It is a vowel’; break;

case ‘u’:

echo ‘It is a vowel’;

break; default:

echo ‘It is a consonant’;

}

?>

OUTPUT:

It is a consonant

PHP switch statement with String

<?php

$ch = “B.Tech”;

switch ($ch)

{

case “BCA”:

echo “BCA is 3 years course”; break;

case “Bsc”:

echo “Bsc is 3 years course”; break;

case “B.Tech”:

echo “B.Tech is 4 years course”; break;

case “B.Arch”:

echo “B.Arch is 5 years course”;

break;

default:

echo “Wrong Choice”; break;

}

?>

OUTPUT:

B.Tech is 4 years course

PHP switch statement is fall-through

  • PHP switch statement is fall-through. It means it will execute all statements after getting the first match, if break statement is not found.

<?php

$ch = ‘c’; switch ($ch)

{

case ‘a’:

echo “Choice a”; break;

case ‘b’:

echo “Choice b”; break;

case ‘c’:

echo “Choice c”; echo “</br>”;

case ‘d’:

echo “Choice d”;

echo “</br>”;

default:

echo “case a, b, c, and d is not found”;

}

?>

OUTPUT:

Choice c Choice d

case a, b, c, and d is not found

PHP nested switch statement

<?php

$author = “Stephen King”;

$book =”The silence of the lambs”; switch($author)

{

case “JK Rowling”:

{

switch($book)

{

case “Harry Potter1”;

echo “Harry Potter1, The price is 300$”; break;

case “Harry Potter2”;

echo “Harry Potter2, The price is 200$”; break;

default:

echo “Author found but not the book”;

}

}

OUTPUT:

The silence of the lambs, The price is 700$

break;

case “Stephen King”:

{

switch($book)

{

case “Hannibal”;

echo “Hannibal, The price is 500$”; break;

case “The silence of the lambs”;

echo “The silence of the lambs, The price is 700$”;

break; default:

echo “Author found but not the book”;

}

}

break; default:

echo “Author not found”;

}

?>

[pdf_note link=”https://drive.google.com/file/d/1493sidnsBqGwk8M0kLCeIdMWuk7j0Wjl/view”]