Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Operations & Strings CSCI 116 Web Programming I. 2 Arithmetic Operators Arithmetic operators are used to perform mathematical calculations."— Presentation transcript:

1 Operations & Strings CSCI 116 Web Programming I

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

3 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 4 Practice $num1 = 10; $num2 = 5; print $num1 + $num2; print $num1 - $num2; print $num1 / $num2; print $num1 % $num2; 15 5 2 0 15 5 2 0

5 5 Increment & Decrement ++ is used to increment a variable – $days++ is equivalent to – $days = $days + 1 -- 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 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 7 Practice $num1 = 10; $num2 = 5; $num1 += $num2; print $num1; $num1 *= $num2; print $num1; 15 75 15 75

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

9 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 10 Operator Precedence Pre-Increment and decrement (++, --) Multiplication and division (*, /, %) Addition and subtraction (+, -,.) When in doubt, use parens!

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

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

13 13 Practice print round(3.549, 1); print number_format(99999.33333); 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 3.5 99,999 2 1 1.2 3.5 99,999 2 1 1.2

14 14 Strings Manipulate strings Parse strings Compare strings Handle form submissions

15 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 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 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 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 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

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

21 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 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 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 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 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 26 substr() Function Extracts characters from the beginning or middle of a string $email = "president@whitehouse.gov"; $name_end = strpos($email, "@");  9 print substr($email, 0, $name_end); $email = "president@whitehouse.gov"; $name_end = strpos($email, "@");  9 print substr($email, 0, $name_end);

27 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 $email = "president@whitehouse.gov"; $newEmail = str_replace("president", "vice.president", $email); print $newEmail;  'vice.president@whitehouse.gov' $email = "president@whitehouse.gov"; $newEmail = str_replace("president", "vice.president", $email); print $newEmail;  'vice.president@whitehouse.gov'

28 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

29 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

30 htmlentities() strip_tags() nl2br()

31 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


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

Similar presentations


Ads by Google