1 / 27

Operations & Strings

Operations & Strings. CSCI 116 Web Programming I. Arithmetic Operators. Arithmetic operators are used to perform mathematical calculations. Arithmetic Operators. $num1 = 15; $num2 = 6; $divisionResult = $num1 / $num2; $modulusResult = $num1 % $num2;

gil
Download Presentation

Operations & Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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<br />"; echo "$num1 modulo $num2 is $modulusResult.";

  4. Practice $num1 = 10; $num2 = 5; print $num1 + $num2; print $num1 - $num2; print $num1 / $num2; print $num1 % $num2; 15 5 2 0

  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; • Careful! Prefix and postfix have differentmeanings when used in a complex expression!

  6. Assignment Operators

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

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

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

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

  11. 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

  12. 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 3.5 99,999 2 1 1.2

  13. Strings • Manipulate strings • Parse strings • Compare strings • Handle form submissions

  14. 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 "<p>Dr. Livingstone, I presume?</p>"; $explorer = "Henry M. Stanley"; print $explorer;

  15. 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.";

  16. 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;

  17. 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

  18. 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

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

  20. 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") ? • str_word_count("Have a nice day") 4 • str_word_count("Huh?") ?

  21. 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");  ?

  22. 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);

  23. 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'

  24. 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

  25. More Useful Functions • nl2br() converts line breaks into <br> 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

  26. nl2br() htmlentities() strip_tags()

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

More Related