Expressions and Control Flow in PHP. Expressions Expressions evaluate to a value. The value can be a string, number, boolean, etc... Expressions often.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Introduction to C Programming
Session 1 & 2BBK P1 Module5-May-2007 : [‹#›] PHP: Moving On..
Air Force Institute of Technology Electrical and Computer Engineering
Introduction to PHP Dr. Charles Severance
ISBN Chapter 7 Expressions and Assignment Statements.
LOOP / REPETITION while loop. for loop do/while loop We assume that loops are not meant to be infinite. That is, there should always be a way out of the.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Announcements The first graded lab will be posted Sunday 2/15 and due Friday 2/27 at midnight It is brand new, so please don’t hand in last semester’s.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Expressions and Control Flow in PHP Dr. Charles Severance
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Fundamentals of Python: From First Programs Through Data Structures
CIS3931 – Intro to JAVA Lecture Note Set 3 19-May-05.
Fundamentals of Python: First Programs
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
Intro to PHP – Page 1 of 43CSCI 2910 – Client/Server-Side Programming CSCI 2910 Client/Server-Side Programming Topic: Intro to PHP Reading: Chapters 1.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
PHP Logic. Review: Variables Variables: a symbol or name that stands for a value – Data types ( Similar to C++ or Java): Int, Float, Boolean, String,
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Expressions and Assignment Statements
Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Python Mini-Course University of Oklahoma Department of Psychology Day 3 – Lesson 10 Iteration: Loops 05/02/09 Python Mini-Course: Day 3 - Lesson 10 1.
Expressions and Control Flow. Expressions An expression is a combination of values, variables, operators, and functions that results in a value y = 3(abs(2x)
2: Basics Basics Programming C# © 2003 DevelopMentor, Inc. 12/1/2003.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Expressions and Order of Operations Operators – There are the standard operators: add, subtract, divide, multiply – Note that * means multiply? (No times.
COMP Loop Statements Yi Hong May 21, 2015.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
LOOPS CHAPTER Topics  Four Types of Loops –while –do…while –for –foreach  Jump Statements in Loops –break –continue 2.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Unit – 3 Control structures. Condition Statements 1.If.…..else :- Has someone ever told you, "if you work hard, then you will succeed"? And what happens.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Flow Control. Comments u Comments: /* This is a comment */ –Use them! –Comments should explain: v special cases v the use of functions (parameters, return.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
UNIVERISTY OF SOMALIA College of Engineering and Computer Science Course code: N/A COURSE: WEB DEVELOPMENT WITH PHP
Expressions and Assignment Statements
PHP Arrays Dr. Charles Severance
Expressions and Control Flow in PHP
Introduction to PHP Dr. Charles Severance
Introduction to PHP Dr. Charles Severance
Expressions and Control Flow in JavaScript
Expressions and Control Flow in PHP
Expressions and Assignment Statements
Arithmetic operations, decisions and looping
FMI-PLOVDIV Web Dynamic Applications
Chapter (3) - Looping Questions.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Repetition Control Structure
Data Types and Expressions
CHAPTER 21 LOOPS 1.
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Expressions and Control Flow in PHP

Expressions Expressions evaluate to a value. The value can be a string, number, boolean, etc... Expressions often use operations and function calls, and there is an order of evaluation when there is more than one operator in an expression Expressions can also produce objects like arrays

Hig h Lo w

Operators of Note Increment / Decrement ( ) String concatenation (. ) Equality ( == != ) Identity ( === !== ) Ternary ( ? : ) Side-effect Assignment ( += -=.= etc.) Ignore the rarely-used bitwise operators ( >> << ^ | & )

Increment / Decrement These operators allow you to both retrieve and increment / decrement a variable They are generally avoided in civilized code. $x = 12; $y = 15 + $x++; echo "x is $x and y is $y \n"; x is 13 and y is 27

Increment / Decrement These operators allow you to both retrieve and increment / decrement a variable They are generally avoided in civilized code. $x = 12; $y = 15 + $x; $x = $x + 1; echo "x is $x and y is $y \n"; x is 13 and y is 27

String Concatenation PHP uses the period character for concatenation because the plus character would instructor PHP to to the best it could do to add the two things together, converting if necessary. $a = 'Hello '. 'World!'; echo $a. "\n"; Hello World!

Equality versus Identity The equality operator (==) in PHP is far more agressive than in most other languages when it comes to data conversion during expression evaluation. if ( 123 == "123" ) print ("Equality 1\n"); if ( 123 == "100"+23 ) print ("Equality 2\n"); if ( FALSE == "0" ) print ("Equality 3\n"); if ( (5 < 6) == "2"-"1" ) print ("Equality 4\n"); if ( (5 < 6) === TRUE ) print ("Equality 5\n");

php

$vv = "Hello World!"; echo "First:". strpos($vv, "Wo"). "\n"; echo "Second: ". strpos($vv, "He"). "\n"; echo "Third: ". strpos($vv, "ZZ"). "\n"; if (strpos($vv, "He") == FALSE ) echo "Wrong A\n"; if (strpos($vv, "ZZ") == FALSE ) echo "Right B\n"; if (strpos($vv, "He") !== FALSE ) echo "Right C\n"; if (strpos($vv, "ZZ") === FALSE ) echo "Right D\n"; print_r(FALSE); print FALSE; echo "Where were they?\n"; First:6 Second: 0 Third: Wrong A Right B Right D Where were they? Beware FALSE variables. They are detectable but not visible...

Ternary The ternary operator comes from C. It allows conditional expressions. It is like a one-line if-then-else. Like all "contraction" syntaxes, we use it carefully. $www = 123; $msg = $www > 100 ? "Large" : "Small" ; echo "First: $msg \n"; $msg = ( $www % 2 == 0 ) ? "Even" : "Odd"; echo "Second: $msg \n"; $msg = ( $www % 2 ) ? "Odd" : "Even"; echo "Third: $msg \n"; First: Large Second: Odd Third: Odd

Side-Effect Assignment These are pure contractions. Civilized programmers use them sparingly. echo "\n"; $out = "Hello"; $out = $out. " "; $out.= "World!"; $out.= "\n"; echo $out; $count = 0; $count += 1; echo "Count: $count\n"; Hello World! Count: 1

Control Structures

Two-way using else : $x = 4; if ($x > 2) { print "Bigger\n"; } else { print "Smaller\n"; } print "All done\n"; x > 2 print 'Bigger' yes no X = 4 print 'Smaller' print 'All Done'

Multi-way $x = 7; if ( $x < 2 ) { print "Small\n"; } elseif ( $x < 10 ) { print "Medium\n"; } else { print "LARGE\n"; } print "All done\n"; x < 2 print 'Small' yes no print 'All Done' x<10x<10 print 'Medium' yes print 'LARGE' no

Curly Braces are not Required if ($page == "Home") echo "You selected Home"; elseif ($page == "About") echo "You selected About"; elseif ($page == "News") echo "You selected News"; elseif ($page == "Login") echo "You selected Login"; elseif ($page == "Links") echo "You selected Links"; if ($page == "Home") { echo "You selected Home"; } elseif ($page == "About") { echo "You selected About"; } elseif ($page == "News") { echo "You selected News"; } elseif ($page == "Login") { echo "You selected Login"; } elseif ($page == "Links") { echo "You selected Links"; }

switch ($page) { case "Home": echo "You selected Home"; break; case "About": echo "You selected About"; break; case "News": echo "You selected News"; break; case "Login": echo "You selected Login"; break; case "Links": echo "You selected Links"; break; }

$page = "x"; switch($page) { case "w": echo "A\n"; break; case "x": echo "X\n"; case "y": echo "Y\n"; break; case "z": echo "Z\n"; break; case "x": echo "XX\n"; break; } XYXY

Looping Structures

$fuel = 10; while ($fuel > 1) { print "Vroom vroom\n"; } $fuel = 10; while ($fuel > 1) { print "Vroom vroom\n"; $fuel = $fuel -1; } A while loop is a "zero-trip" loop with the test at the top. before the first iteration starts. We hand construct the iteration variable to implement a counted loop.

$count = 1; do { echo "$count times 5 is ". $count * 5; echo "\n"; } while (++$count <= 5); 1 times 5 is 5 2 times 5 is 10 3 times 5 is 15 4 times 5 is 20 5 times 5 is 25 A do-while loop is a "one- trip" loop with the test at the bottom after the first iteration completes.

for($count=1; $count<=6; $count++ ) { echo "$count times 6 is ". $count * 6; echo "\n"; } 1 times 6 is 6 2 times 6 is 12 3 times 6 is 18 4 times 6 is 24 5 times 6 is 30 6 times 6 is 36 A for loop is the simplest way to construct a counted loop.

for($count=1; $count<=6; $count++ ) { echo "$count times 6 is ". $count * 6; echo "\n"; } 1 times 6 is 6 2 times 6 is 12 3 times 6 is 18 4 times 6 is 24 5 times 6 is 30 6 times 6 is 36 A for loop is the simplest way to construct a counted loop. Before loop starts Loop runs while TRUE (top- test) Run after each iteration.

Looping Through an Array <?php $stuff = array("name" => "Chuck", "course" => "SI664"); foreach($stuff as $k => $v ) { echo "Key=",$k," Val=",$v,"\n"; } ?> Key=name Val=Chuck Key=course Val=SI664

Looping Through an Array <?php $stuff = array("Chuck","SI664"); foreach($stuff as $k => $v ) { echo "Key=",$k," Val=",$v,"\n"; } ?> Key=0 Val=Chuck Key=1 Val=SI664

Counted Loop Through an Array <?php $stuff = array("Chuck","SI664"); for($i=0; $i < count($stuff); $i++) { echo "I=",$i," Val=",$stuff[$i],"\n"; } ?> I=0 Val=Chuck I=1 Val=SI664

Loop Controls Like many C-inspired languages, PHP has two control structures that work within a loop break - exit the loop immediately continue - finish the current iteration and jump to the next iteration, starting at the top of the loop

for($count=1; $count<=600; $count++ ) { if ( $count == 5 ) break; echo "Count: $count\n"; } echo "Done\n"; Breaking Out of a Loop The break statement ends the current loop and jumps to the statement immediately following the loop It is like a loop test that can happen anywhere in the body of the loop Count: 1 Count: 2 Count: 3 Count: 4 Done

Finishing an Iteration with continue The continue statement ends the current iteration and jumps to the top of the loop and starts the next iteration for($count=1; $count<=10; $count++ ) { if ( ($count % 2) == 0 ) continue; echo "Count: $count\n"; } echo "Done\n"; Count: 1 Count: 3 Count: 5 Count: 7 Count: 9 Done

Conversion / Casting As PHP evaluates expressions, at times values in the expression need to be converted from one type to another as the computations are done. PHP does aggressive implicit type conversion (casting) You can also make type conversion (casting) explicit with casting operators.

Casting $a = 56; $b = 12; $c = $a / $b; echo "C: $c\n"; $d = "100" TRUE; echo "D: ". $d. "\n"; echo "D2: ". (string) $d. "\n"; $e = (int) ; echo "E: $e\n"; $f = "sam" + 25; echo "F: $f\n"; $g = "sam". 25; echo "G: $g\n"; C: D: D2: E: 8 F: 25 G: sam25 In PHP, division forces operands to be floating point. PHP converts expression values silently and agressively.

PHP.vs. Python $x = "100" + 25; echo "X: $x\n"; $y = "100". 25; echo "Y: $y\n"; $z = "sam" + 25; echo "Z: $z\n"; X: 125 Y: Z: 25 x = int("100") + 25 print "X:", x y = "100" + str(25) print "Y:", y z = int("sam") + 25 print "Z:", z X: 125 Y: Traceback:"cast.py", line 5 z = int("sam") + 25; ValueError: invalid literal

Casting echo "A".FALSE."B\n"; echo "X".TRUE."Y\n"; AB X1Y The concatenation operator tries to convert its operands to strings, TRUE becomes an integer 1 and then becomes a string. FALSE is "not there" it is even "smaller" than zero. At least when it comes to width.

Summary Expressions Operators Conditional Structures Looping Structures Type Conversion and Casting