Operations & Strings CSCI 116 Web Programming I. 2 Arithmetic Operators Arithmetic operators are used to perform mathematical calculations.

Slides:



Advertisements
Similar presentations
This is Java Jeopardy.
Advertisements

PHP Introduction.
Chapter 3 Manipulating Strings PHP Programming with MySQL 2nd Edition
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The Class String. String Constants and Variables  There is no primitive type for strings in Java.  There is a class called String that can be used to.
Introduction to Python
Manipulating Strings. 2 Topics Manipulate strings Manipulate strings Parse strings Parse strings Compare strings Compare strings Handle form submissions.
JavaScript, Fourth Edition
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Manipulating Strings. Name of Book2 Objectives Manipulate strings Parse strings Compare strings Handle form submissions.
1 Lecture 3  Lexical elements  Some operators:  /, %, =, +=, ++, --  precedence and associativity  #define  Readings: Chapter 2 Section 1 to 10.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Variables & Math Operators CE 311 K - Introduction to Computer Methods Daene C. McKinney.
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
Chapter 9 Creating Formulas that Manipulate Text Microsoft Office Excel 2003.
Expressions, Data Conversion, and Input
PHP Workshop ‹#› Data Manipulation & Regex. PHP Workshop ‹#› What..? Often in PHP we have to get data from files, or maybe through forms from a user.
Chapter 3 Manipulating Strings PHP Programming with MySQL 2nd Edition
Chap 3 – PHP Quick Start COMP RL Professor Mattos.
2440: 211 Interactive Web Programming Expressions & Operators.
PHP Controlling Program Flow Mohammed M. Hassoun 2012.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
1 Working with Data Types and Operators. 2 Using Variables and Constants The values stored in computer memory are called variables The values, or data,
Chapter 2: Using Data.
Strings in PHP Working with Text in PHP Strings and String Functions Mario Peshev Technical Trainer Software University
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
ITM © Port, KazmanVariables - 1 ITM 352 Expressions, Precedence, Working with Strings.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Strings, output, quotes and comments
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
CS4 –lecture 6 Wednesday, Jan 19, 2011 Roxana Gheorghiu.
COMP 110: Spring Announcements Lab 1 due Wednesday at Noon Assignment 1 available on website Online drop date is today.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Session 2: PHP Language Basics iNET Academy Open Source Web Development.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Introduction to PHP and MySQL (Creating Database-Driven Websites)
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
PHP Programming with MySQL Slide 5-1 CHAPTER 5 Manipulating Strings.
Chapter 16 Introduction to Perl Perl (Practical Extraction and Report Language) Developed by Larry Wall as a Unix scripting language. Popular server-side.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Lesson 4 String Manipulation. Lesson 4 In many applications you will need to do some kind of manipulation or parsing of strings, whether you are Attempting.
CSC 4630 Meeting 7 February 7, 2007.
JavaScript, Sixth Edition
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
ITM 352 Expressions, Precedence, Working with Strings Class #5
Chapter 3 Manipulating Strings PHP Programming with MySQL 2nd Edition Modified by Anita Philipp –Spring 2011.
Lecture 3 Expressions Richard Gesick.
Embedded PHP in HTML 5 & Strings in PHP
String functions
Data Manipulation & Regex
elementary programming
Web Programming Language
Introduction to Computer Science
String methods 26-Apr-19.
C++ Programming Basics
String functions
Presentation transcript:

Operations & Strings CSCI 116 Web Programming I

2 Arithmetic Operators Arithmetic operators are used to perform mathematical calculations.

3 Arithmetic Operators $num1 = 15; $num2 = 6; $divisionResult = $num1 / $num2; $modulusResult = $num1 % $num2; echo "$num1 divided by $num2 is $divisionResult "; echo "$num1 modulo $num2 is $modulusResult.";

4 Practice $num1 = 10; $num2 = 5; print $num1 + $num2; print $num1 - $num2; print $num1 / $num2; print $num1 % $num2;

5 Increment & Decrement ++ is used to increment a variable – $days++ is equivalent to – $days = $days is used to decrement a variable can be used as prefix or postfix operators Prefix : ++num1 Postfix: num1++ $days = 1; $days++; print $days; $days = 1; $days++; print $days; Careful! Prefix and postfix have different meanings when used in a complex expression!

6 Assignment Operators This…is equivalent to… $x += $y;$x = $x + $y; $x -= $y;$x = $x - $y; $x *= $y;$x = $x * $y; $x /= $y;$x = $x / $y;

7 Practice $num1 = 10; $num2 = 5; $num1 += $num2; print $num1; $num1 *= $num2; print $num1;

8 Practice Problems $num = 5; $num++; $num += 2; $num--; $num *= 3; $num -= 5; $num /= 2; $num %= 3;

9 Type Casting Type casting copies a value contained in one type of variable into a variable of another type Syntax $newVariable = (new_type)$oldVariable; Example $miles = “55 mph”; $kilometers = (int)$miles * 1.6; int, integer bool, boolean double string CASTINGOPERATORS

10 Operator Precedence Pre-Increment and decrement (++, --) Multiplication and division (*, /, %) Addition and subtraction (+, -,.) When in doubt, use parens!

11 Practice $val1 = * 5 – 3 / 2; $val2 = (1 + 2) * (5 – 3) / 2; print $val1; print $val2; --$val2; print $val2;

Functions round( )  1235 round( , 2)  number_format( )  1,235 number_format( , 2)  1, gettype( )  double rand(1, 10)  a random number between 1 and 10, inclusive

13 Practice print round(3.549, 1); print number_format( ); print ceil(1.2); print floor(1.2); print abs(-1.2); Look up these functions in the PHP manual: php.net/manual Look up these functions in the PHP manual: php.net/manual , ,

14 Strings Manipulate strings Parse strings Compare strings Handle form submissions

15 Strings A text string contains zero or more characters A string may be surrounded by double or single quotation marks Text strings can be used as literal values or assigned to a variable print " Dr. Livingstone, I presume? "; $explorer = "Henry M. Stanley"; print $explorer;

16 Strings Double quotation marks may be embedded in single quotation marks $greet = 'I said, "Hello! "'; print $greet; Single quotation marks may be embedded in double quotation marks $phrase = "You're the bomb.";

17 Combining Strings Concatenation operator (.) $destination = "Paris"; $location = "France"; $destination = $destination. " is in ". $location; print $destination; Concatenation assignment operator (.=) $destination = "Paris "; $destination.= "is in France"; print $destination;

18 Escape Characters Tells the compiler or interpreter that following character has a special purpose In PHP, the escape character is a backslash \ echo 'You\'re the best!'; – Not needed before an apostrophe if the text string is surrounded with double quotes echo " You're the best! " ; No backslash needed

19 Escape Sequences echo " File location: C:\\My Documents\\CSci116\\ " ; $name = "Fred"; echo '"Hi!," said $name.';  "Hi!," said $name. echo "\"Hi!,\" said $name.";  "Hi!," said Fred. echo '"Hi!,"'. " said $name.";  "Hi!," said Fred. this won’t work solutions

Practice 1.Write two different statements that print: Let's go! 2.Write a statement that prints: Columbus arrived on 10/12/1492

21 Simple and Complex String Syntax Simple string syntax $veg1 = "broccoli"; $veg2 = "carrot"; echo "Do you have any $veg1?"; echo "Do you have any $veg2s?"; Complex string syntax echo "Do you have any {$veg2}s?"; this works this doesn’t

22 strlen() & str_word_count() strlen() returns the number of characters in a string str_word_count() returns the number of words in a string strlen("Howdy!")  6 strlen("Hi there")  ? strlen("Howdy!")  6 strlen("Hi there")  ? str_word_count("Have a nice day")  4 str_word_count("Huh?")  ? str_word_count("Have a nice day")  4 str_word_count("Huh?")  ?

23 substr_count() substr_count() returns the number of occurrences of a substring in a string substr_count("Yabba Dabba Doo", "abba")  2 substr_count("Persephone", "Phone")  ? substr_count("Yabba Dabba Doo", "abba")  2 substr_count("Persephone", "Phone")  ?

24 strpos() Function Performs a case-sensitive search and returns the position of the first occurrence of one string in another string – Returns false if the search string is not found strpos("Hello", "lo");  3 strpos("Hello", "la");  false strpos("More cowbell", "cow");  ? strpos("Hello", "lo");  3 strpos("Hello", "la");  false strpos("More cowbell", "cow");  ?

25 strchr() and strrchr() Return a substring from the specified characters to the end of the string – strchr() starts searching at the beginning of a string – strrchr() starts searching at the end of a string strchr("Hello there", "e");  ello there strrchr("Hello there”, "e");  e strchr("More cowbell", "cow");  ? strchr("Hello there", "e");  ello there strrchr("Hello there”, "e");  e strchr("More cowbell", "cow");  ?

26 substr() Function Extracts characters from the beginning or middle of a string $ = $name_end = strpos($ ,  9 print substr($ , 0, $name_end); $ = $name_end = strpos($ ,  9 print substr($ , 0, $name_end);

27 str_replace() str_replace() accepts three arguments: – The string you want to replace – The new replacement string – The string you are searching str_ireplace() is case-insensitive $ = $new = str_replace("president", "vice.president", $ ); print $new ;  $ = $new = str_replace("president", "vice.president", $ ); print $new ; 

More String Functions ucfirst() - capitalizes the first letter of a string – ucfirst(“hello world”)  Hello world ucwords() - capitalizes the first letter of each word in a string – ucwords(“hello world”)  Hello World strtoupper() - converts a string to upper case – ucwords(“hello world”)  HELLO WORLD strtolower() - converts a string to lower case trim() - removes white space before and after a string

More Useful Functions nl2br() converts line breaks into tags – $message = nl2br($message); htmlentities() turns HTML tags into their entity versions strip_tags() removes all HTML, JS and PHP tags – A good security measure

htmlentities() strip_tags() nl2br()

Practice Create a string variable phrase that contains ' I like PHP ', and then print: a)The string, with white space at the beginning and end removed b)The length of the string c)The position of 'PHP' within the string d)The string with 'love' replacing 'like' e)The string, all upper case