270 likes | 359 Views
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;
E N D
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; echo "$num1 divided by $num2 is $divisionResult<br />"; echo "$num1 modulo $num2 is $modulusResult.";
Practice $num1 = 10; $num2 = 5; print $num1 + $num2; print $num1 - $num2; print $num1 / $num2; print $num1 % $num2; 15 5 2 0
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!
Practice $num1 = 10; $num2 = 5; $num1 += $num2; print $num1; $num1 *= $num2; print $num1; 15 75
Practice Problems • $num = 5; • $num++; • $num += 2; • $num--; • $num *= 3; • $num -= 5; • $num /= 2; • $num %= 3;
Operator Precedence • Pre-Increment and decrement (++, --) • Multiplication and division (*, /, %) • Addition and subtraction (+, -, .) When in doubt, use parens!
Practice $val1 = 1 + 2 * 5 – 3 / 2; $val2 = (1 + 2) * (5 – 3) / 2; print $val1; print $val2; --$val2; print $val2; 9.5 3 2
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
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
Strings • Manipulate strings • Parse strings • Compare strings • Handle form submissions
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;
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.";
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;
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
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 • Write two different statements that print: Let's go! • Write a statement that prints: Columbus arrived on 10/12/1492
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?") ?
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"); ?
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);
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'
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 <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
nl2br() htmlentities() strip_tags()
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