ITM 352 Data types Variables ITM 352 Port
ITM 352 Data types, Variables ITM 352 - © Port, Kazman Variables - 1
Announcements �Apache and Net. Beans must be installed and working on your laptop NOW � If you are still have any problems with these GET HELP � During class: � Start Net. Beans and create a new project for the lab � Go to the lab assignment on Laulima. You may copy and paste the exercises in to Word then back into the submission area or work directly on Laulima (just be sure to save a draft frequently) ITM 352 - © Port, Kazman Variables - 2
Agenda �Lecture: � Data types � Outputting to the screen and console � Variables ITM 352 - © Port, Kazman Variables - 3
Two Main Kinds of Data Types Scalar � the simplest types � also called "primitive" or "basic" types � cannot decompose into other types � contain single values only � Examples: � � Integer Floating point (real) String Boolean ITM 352 - © Port, Kazman Compound also call class types � more complex � composed of other types (primitive or class types) � can contain multiple values � Examples: � � � Array Object (more about these in ITM 353) Resource Variables - 4
Which Ones to Know for Now - 1 � integer � floating point just whole numbers � may be positive or negative � no decimal point � may use � Octal: 0755 // starts '0' � Hex: 0 x. FF // starts '0 x' � In PHP these are referred to as int � boolean � only two values – true or false � used for 'conditional' tests (e. g. if, when) � In PHP these are referred to as bool real numbers, both positive and negative � has a decimal point (fractional part) � two formats � number with decimal point, e. g. 514. 061 � e (or scientific, or floatingpoint) notation, e. g. 5. 14061 E 2, which means 5. 14061 x 102 � In PHP these are also referred to as double � null � The 'nothing' type (more on this later) � Not really a data type � ITM 352 - © Port, Kazman � Variables - 5
Which Ones to Know for Now – 2. � A string is a sequence of characters � � � A very common data type � Names, passwords, addresses, histories, etc. Often used to represent complex data � Dates, phone numbers, SS numbers, formatted output A common data-interchange or data-sharing type � key-value pairs, XML, comma delimited data, logs � PHP has a vast and powerful set of functions for working with strings. JS not so much, but there are frameworks such as JQuery that do. � � Manipulation, searching, comparing, translation, etc. Check out php. net � Examples: “Mr. Smith”, ‘ 808 -956 -6948’, ‘ 21. 7’, “ 1202 King St. ” � In PHP or JS anything between quotes (single or double) is considered a string value ITM 352 - © Port, Kazman Variables - 6
Output in JS � Use document. write() for simple output that appends to the current browser page (i. e. the document DOM object) � document. write('hello'); // could use "hello" � To write to a particular location (i. e. page element) set its inner. HTML attribute <div id="place"></div> <script> document. get. Element. By. Id("place"). inner. HTML='hello'; </script> � Use console. log output to the JS console which is not displayed on the browser page. It can only be seen through the browser tools or Net. Beans Output (good for testing) console. log("hello"); ITM 352 - © Port, Kazman Variables - 7
Output in PHP � Use echo for simple output � echo 'hello'; � echo 'hello', ' goodbye'; � echo ('hello'); � print is virtually the same syntax � print 'hello'; � You can use () if you like � echo('hello'); � print('hello'); � New line for console output (we don’t do much of this) � echo "line 1nline 2"; � New line for HTML output � echo 'line 1 line 2'; Do exercise #1 ITM 352 - © Port, Kazman Variables - 8
What is a Variable? � A named location to store data � a container for data (like a box or bucket) 'Dan' 30. 3 $name $age � It can hold only one type of data at a time � for example only integers, only floating point (real) numbers, or only characters � A variable with a scalar type holds one scalar value � A variable with a compound type holds multiple scalar values, BUT the variable still holds only a single (the compound type itself) value � Syntax for a variable is $<identifier> � PHP Example: $name, $age � JS Example: var name, var age � Case sensitive! ITM 352 - © Port, Kazman Variables - 9
Creating Variables � A variable is declared the first time a value is set for it (i. e. assigned) � A variable declaration associates a name with a storage location in memory and specifies the type of data it will store: � � � $a = 1. 1 ; // declares and sets a real number $a = true ; // declares and sets a boolean $a = 'Zip Zap' ; // declares and sets a string var a = 1. 1 ; // declares and sets a real number var a = true; // declares and sets a boolean var a = 'Zip Zap' ; // declares and sets a string ITM 352 - © Port, Kazman Variables - 10
Assigning Values to Variables � The assignment operator: "=" � � "sets" a value for a variable. It is an expression that has the value of the assignment. not the "is equal to" sign; not the same as in algebra � It means - "Assign the value of the expression on the right side to the variable on the left side. " � Can have the variable on both sides of the equals sign: $count = 10; // initialize counter to ten $count = $count - 1; // decrement counter var count = 10; // initialize counter to ten var count = count - 1; // decrement counter � new value of count = 10 - 1 = 9 ITM 352 - © Port, Kazman Variables - 11
Simple Expressions �Data types can be operated on (e. g. arithmetic, string operations) echo 1+2; echo 3*2; echo "Big". "Dude"; printf("5/3 is about %3 d", 5/3); Operators: +, -, . , *, /, % ITM 352 - © Port, Kazman Variables - 12
Simple Expressions With Variables �Variables can be operated on (e. g. arithmetic) // add 1 to value in $a and set in $add = $a + 1; // multiply value in $a by 2 and set in $mult = $a * 2; // concatenate string in $s with 'Fred' and // set in $str = $s. " Fred"; ITM 352 - © Port, Kazman Variables - 13
Variable Names: Identifiers Rules (these must be obeyed) Good Programming Practice (these should be obeyed) � all identifiers must follow the � always use meaningful names from the � � same rules must not start with a digit must contain only numbers, letters, underscore (_) and some other special characters names are case-sensitive (This. Name and this. Name are two different variable names) No spaces! � � problem domain (for example, eggs. Per. Basket instead of n, which is meaningless, or count, which is not meaningful enough) start variable names with lower case capitalize interior words (use eggs. Per. Basket instead of eggsperbasket) use underscore (_) for spaces CAPITALIZE constants (i. e. variables that do not change values) Do exercise #2 ab ITM 352 - © Port, Kazman Variables - 14
Variable Default Values � Variables have default values � $a = $a + 1; // $a=0 by default � $s = $s. "Fred"; // default $s='' � var a = a + 1; // a=0 by default � var s = s + "Fred"; // default s="" � IMPORTANT: It is best to not assume the default value is what you want. Always explicitly set the initial value of a variable!!!! e. g. � $a = 0; $s = ""; $b = false; ITM 352 - © Port, Kazman Variables - 15
NULL �Null is a special type that means "no value" �It can be used to unset a variable �It is used as a place holder within compound types (more on this later…) � $a = NULL; unset($a) // $a is “unset”, also can use � var a = null; // var a; would set a as “undefined” not null Do exercise #2 cd ITM 352 - © Port, Kazman Variables - 16
String Expressions � Even though strings are not numbers, they can still manipulated with operators and they are evaluated as expressions � The main operator for strings is “concatenate” � � PHP uses the dot “. ” operator JS uses the “+” operator (remember. means something else in JS) $add_str = "Jughead". " Jones"; var add_str = "Jughead" + " Jones"; � Doesn’t do anymore than put the characters together. � � No smarts! You have to put that parts together and make the strings output what is desired. Square brackets [] after a string is an operator that can access individual character in position (index) specified in the brackets. � � � "Jughead"[2] evaluates to g "Jughead"[0] evaluates to J add_str[2] evaluates to g, so does $add_str[2] Do exercise #3 ITM 352 - © Port, Kazman Variables - 17
Printf() � Use printf() for more complex formatted output printf('This prints 2 decimal places %. 2 f', 3. 1415927); This prints 2 decimal places 3. 14 � Printf() is a function whose first argument is a string that describes the desired format and the remaining arguments are the values to substitute into the type specifications (anything that starts with %) � It’s a complicated function but you should know it and make use of it because it’s useful Do exercise #4 ITM 352 - © Port, Kazman Variables - 18
- Slides: 18