🎧 New: AI-Generated Podcasts Turn your study notes into engaging audio conversations. Learn more

LESSON 4 - Supplementary Lectures -4 (Operators and Control Structures-If and Switch).pdf

Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...

Full Transcript

COMPUTER PROGRAMMING 3 WEB APPLICATION USING PHP Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP Operators § Arithmetic, Assignment, Relational/Comparison, Logical, String § PHP Control Structures/Statements (IF and SWITCH) WEB APPLICATION USING PHP...

COMPUTER PROGRAMMING 3 WEB APPLICATION USING PHP Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP Operators § Arithmetic, Assignment, Relational/Comparison, Logical, String § PHP Control Structures/Statements (IF and SWITCH) WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: OPERATORS § PHP Operators PHP Operator is a symbol i.e., used to perform operations on operands. In simple words, operators are used to perform operations on variables or values. Example: PHP Operators can be categorized in following forms: ü Arithmetic Operators ü String Operators ü Assignment Operators ü Array Operators ü Bitwise Operators ü Type Operators ü Comparison Operators ü Execution Operators ü Incrementing/Decrementing Operators ü Error Control Operators ü Logical Operators WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: OPERATORS § PHP Operators We can also categorize operators on behalf of operands. They can be categorized in 3 forms: ü Unary Operators: § works on single operands such as ++, -- etc. ü Binary Operators: § works on two operands such as binary +, -, *, / etc. ü Ternary Operators: § works on three operands such as "?:". WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: OPERATORS § PHP 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 The exponentiation (**) Division $a / $b Quotient of operands operator has been / introduced in PHP 5.6. % Modulus $a % $b Remainder of operands ** Exponentiation $a ** $b $a raised to the power $b WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: OPERATORS § PHP Assignment Operators: The assignment operators are used to assign value to different variables. The basic assignment operator is "=". Operator Name Example Explanation The value of right operand is assigned to the left = Assign $a = $b operand. += Add then Assign $a += $b Addition same as $a = $a + $b -= Subtract then Assign $a -= $b Subtraction same as $a = $a - $b *= Multiply then Assign $a *= $b Multiplication same as $a = $a * $b Divide then Assign /= $a /= $b Find quotient same as $a = $a / $b (quotient) Divide then Assign %= $a %= $b Find remainder same as $a = $a % $b (remainder) WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: OPERATORS § PHP 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 = $b Return TRUE if $a is greater than or equal $b Return -1 if $a is less than $b Spaceship $a $b Return 0 if $a is equal $b Return 1 if $a is greater than $b WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: OPERATORS § PHP Incrementing/Decrementing Operators: The increment and decrement operators are used to increase and decrease the value of a variable. Operator Name Example Explanation ++$a Increment the value of $a by one, then return $a ++ Increment $a++ Return $a, then increment the value of $a by one --$a Decrement the value of $a by one, then return $a -- decrement $a-- Return $a, then decrement the value of $a by one WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: OPERATORS § PHP 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 $a 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 WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: OPERATORS § PHP 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 First concatenate $a and $b, then assign the.= Assignment $a.= $b concatenated string to $a, e.g. $a = $a. $b WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP Control Structures/Statements PHP if else statement is used to test condition. There are various ways to use if statement in PHP. ü if ü if-else ü if-else-if ü nested if WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP IF Statements: PHP if statement allows conditional execution of code. It is executed if condition is true. If statement is used to executes the block of code exist inside the if statement only if the specified condition is true. Syntax: Example: Output: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP IF-ELSE Statements: PHP if-else statement is executed whether condition is true or false. If-else statement is slightly different from if statement. It executes one block of code if the specified condition is true and another block of code if the condition is false. Example: Output: Syntax: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP IF-ELSE-IF Statements: The PHP if-else-if is a special statement used to combine multiple if-else statements. So, we can check multiple conditions using this statement. Syntax: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP IF-ELSE-IF Statements: Output: Example: The PHP if-else-if is a special statement used to combine multiple if-else statements. So, we can check multiple conditions using this statement. Syntax: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS Example: § PHP NESTED IF Statements: 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. Syntax: Output: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP NESTED IF Statements: 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. Syntax: Example: Output: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP SWITCH Statement: PHP switch statement is used to execute one statement from multiple conditions. It works like PHP if-else-if statement. Syntax: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP SWITCH Statement: Important points to be noticed about switch case: ü The default is an optional statement. Even it is not important, that default must always be the last statement. ü There can be only one default in a switch statement. More than one default may lead to a Fatal error. ü Each case can have a break statement, which is used to terminate the sequence of statement. ü The break statement is optional to use in switch. If break is not used, all the statements will execute after finding matched case value. ü PHP allows you to use number, character, string, as well as functions in switch expression. ü Nesting of switch statements is allowed, but it makes the program more complex and less readable. ü You can use semicolon (;) instead of colon (:). It will not generate any error. WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP SWITCH Statement: Output: Example: Output: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS Output: § PHP SWITCH Statement: Examples: Output: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS: CONTROL STRUCTURES/STATAMENTS § PHP NESTED SWITCH Statement: Nested switch statement means switch statement inside another switch statement. Sometimes it leads to confusion. Example: Output: WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO INTRODUCTION TO WEB DEVELOPMENT PHP FUNDAMENTALS – SCRIPTING: § How to run PHP code in XAMPP Step 1: Create a simple PHP program like hello world. Step 2: Save the file with p1.php name in the htdocs folder, which resides inside the xampp folder. ü Note: PHP program must be saved in the htdocs folder, which resides inside the xampp folder, where you installed the XAMPP. Otherwise it will generate an error - Object not found. Step 3: Run the XAMPP server and start the Apache and MySQL. Step 4: Now, open the web browser and type localhost http://localhost/hello.php on your browser window. Step 5: The output of p1.php program will be shown in the browser WEB APPLICATION USING PHP | Prof. ARIEL DOMINGO COMPROG2WEB APPLICATION | COMPUTER USING PHP 2| (Elective) PROGRAMMING Prof. ARIEL DOMINGO | Prof. ROEL C. TRABALLO COMPROG2WEB APPLICATION | COMPUTER USING PHP 2| (Elective) PROGRAMMING Prof. ARIEL DOMINGO | Prof. ROEL C. TRABALLO

Use Quizgecko on...
Browser
Browser