String Conversion and Type Juggling

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Chapter 41 Variables and JSP Control Structures JavaServer Pages By Xue Bai.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
1 CS101 Introduction to Computing Lecture 23 Flow Control & Loops (Web Development Lecture 8)
True or false A variable of type char can hold the value 301. ( F )
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
UNIT II Decision Making And Branching Decision Making And Looping
Day 4 Objectives Constructors Wrapper Classes Operators Java Control Statements Practice the language.
Agenda Control Flow Statements Purpose test statement if / elif / else Statements for loops while vs. until statements case statement break vs. continue.
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
2440: 211 Interactive Web Programming Expressions & Operators.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
1 Session 3: Flow Control & Functions iNET Academy Open Source Web Programming.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
PHP Conditional Statements Conditional statements in PHP are used to perform different actions based on different conditions. Conditional Statements Very.
Control Structures By Shyam Gurram. Control Structure In this chapter we have two different types of structures. Conditional Structure Iterative Control.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Lesson - 5. Introduction While programming, we usually need to decide the path of the program flow according to the parameters and conditions. Actually.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
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)
Program Structures Chapter 5. 5 Branching Allows different code to execute based on a conditional test. if, if-else, and switch statements.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Control Statements: Part1  if, if…else, switch 1.
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.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Hello world . Variables A variable consists of a name that you can choose, preceded by a dollar ($) sign. Some legal variables.
Branching statements.
>> Fundamental Concepts in PHP
Expressions and Control Flow in PHP
Creating php Pages Done By: Hind Talafha.
Java Programming Fifth Edition
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 3: Decisions and Loops
Control Statements: Part 1
Web Technologies PHP 5 Basic Language.
CIIT-Human Computer Interaction-CSC456-Fall-2015-Mr
Overview: Programming Concepts
Object Oriented Programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
JavaScript Syntax and Semantics
Programming Fundamentals
JavaScript: Control Statements.
JavaScript: Control Statements I
Agenda Control Flow Statements Purpose test statement
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Java - Data Types, Variables, and Arrays
During the last lecture we had a discussion on Data Types, Variables & Operators
PHP.
Matlab tutorial course
3 Control Statements:.
University of Kurdistan
Summary Two basic concepts: variables and assignments Basic types:
The System.exit() Method
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Chapter 3: Selection Structures: Making Decisions
In this class, we will cover:
Basic Programming Concepts
Control Structures (Structured Programming) for controlling the procedural aspects of programming CS1110 – Kaminski.
PHP an introduction.
Using C++ Arithmetic Operators and Control Structures
Controlling Program Flow
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

String Conversion and Type Juggling If you perform s numerical operation on a string, PHP will evaluate the string as a number. This is known as string conversion ,although the variable containing the String itself my not necessarily change. Lesson 2

String Conversion and Type Juggling Only the beginning of the string is evaluated as a number. If the string begins with A valid numerical value, the string will evaluate as that value; otherwise it will Evaluate as zero. The string “33 students” would evaluate as 3 if used in a numerical Operation, but the string “students 33” would evaluate 0 (zero). Lesson 2

String Conversion and Type Juggling PHP performs type juggling between the two numeric types. If you perform a numerical operation between integer and double the result will be a double. $a=1; // $a is an integer $b=2.0 //$b is a double; $c=$a+$b; //$c is a double (value 3.0); $d=$c+”33 students” //$d is a double (value 36) Lesson 2

Type Casting Type casting allows you to explicitly change the data type of a variable. $a=12.3; // $a is a double $a=(int) $a; //$a is an integer (value 12); $a=(double) $a; //$a is a double (value 12.0); $b=(string) $a //$b is a string (value “12”) Lesson 2

Useful Functions for variables PHP has a number of build-in functions for working with variables. gettype() Determines the data type of a variable. It returns one of the following values. integer double string array object class unknown type Lesson 2

Useful Functions for variables Lesson 2

Useful Functions for variables settype() The settype() function explicitly sets type of a variable Lesson 2

Useful Functions for variables isset() and unset() Unset() is used to destroy a variable, freeing all the memory that is associated with the variable so it is then available again, The isset() function is used to determine whether A variable has been given a value. If a value has been set then it returns true. Lesson 2

Useful Functions for variables Lesson 2

Useful Functions for variables Lesson 2

Useful Functions for variables empty() Empty() is nearly the opposite of isset(). It returns true if the variable is not set Or has a value of zero or an empty string. Other wise it returns false. Lesson 2

Useful Functions for variables Operators Lesson 2

Arithmetic Operators Like every programming language, PHP uses the basic mathematical operators Operator Operation Performed Example + Addition 3+3 - Subtraction 3-3 * Multiplication 3*3 / Division 3/3 % Modulus 3%3 Lesson 2

Arithmetic Operators The Unary Operator The minus sign(-) is also used with a single numeric value to negate a number ( The is to make a positive number negative, or a negative number positive) $a = 3; $b= - $a; //$b=-3 $c= -5; $d= -$c; //$d=5 Lesson 2

Arithmetic Operators $i=5; $i=5; if($i==8) if($i=8) echo “Eight”; else echo “five”;// this prints five $i=5; if($i=8) echo “Eight”; //”eight” prints every time Lesson 2

Logical Operator Example Operator Name Evaluate true when: $a && $b And Both $a and $b evaluate to true $a || $b Or one or both of $a and $b evaluate to true $a and $b And Both $a and $b evaluate to true $a or $b Or one or both of $a and $b evaluate to true $a xor $b Excusive Or one of $a and $b evaluates to true, but not both ! $a Not $a does not evaluate to true Lesson 2

Logical Operator Lesson 2

String Concatenation Operator Lesson 2

Variable Assignment Shortcuts Lesson 2

Variable Assignment Shortcuts Lesson 2

Variable Assignment Shortcuts Lesson 2

If Statements $country=“us”; if ($country==“us”) echo “USA”; If more one statement is to be executed when the condition is true, curly braces {} Are used to indicate which lines belong inside the if block: $country=“us”; if ($country==“us”) { echo “USA”; echo “Canada”; } Lesson 2

If Statements Any Condition which is not met, zero, an empty string (“”), undefined values, and the Built in constant false all evaluate to false If (3<2) Echo “this will not print”; If (false) If(“0”) if($x) If (3>2) Echo “this will print”; If (“false”) If(“00”) if(0==0) Lesson 2

If Statements Branches Conditions If the tested condition returns false, PHP allows us to specify another block of code to be executed using the else keyword. If($x<0) { echo “Negative” } else echo “Positive”; Lesson 2

If Statements PHP also provides the elseif keyword to test alternative conditions if the condition In the if portion of the statement is not true. Any number of elseif statements may be Used with an if statement. The final else branch then allows us to specify code that Should be executed if none of the if or elseif condition is true If ($x < 0) { echo “Negative”; } elseif ($x==0) echo “Zero”; else echo “Positive” Lesson 2

If Statements It is also common to nest if statements within another if statement $studentName=“Adam”; $pass=‘123; If ($studentName==“Adam”) { if($pass==123) echo “login successful”; } else echo “Retype your password”; echo “login failed”; Lesson 2

If Statements $studentName=“Adam”; $pass=‘123; If ($studentName==“Adam” && $pass==123) { echo “login successful”; } else echo “login failed”; Lesson 2

switch Statements Switch Statement $val=6; $x=5; $y=6; switch ($val) { case $x: echo “five”; break; case $y: echo “six”; default: echo $val; } Lesson 2

Loops While Loops While (condition) { //statements } $x=4; While ($x != 0) { echo $x; echo “<br>”; --$x; } echo “done”; Lesson 2

Loops do..While Loops $x=4; do { echo $x; echo “<br>”; --$x; } while ($x != 0); echo “done”; Lesson 2

Loops for Loops $x=4; for ($i=0;$i<=$x;$i++) { echo $i."<br>"; } Lesson 2

Loops for Loops $x=4; for ($i=0;$i<=$x;$i++) { echo $i."<br>"; } Lesson 2