_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 10.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
Chapter 9 Formatted Input/Output Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Data types and variables
JavaScript, Fourth Edition
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to Python Programming Outline 2.1 Introduction 2.2 First Program in Python: Printing.
Chapter 2 Data Types, Declarations, and Displays
Guide To UNIX Using Linux Third Edition
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
Introduction to C Programming
Basic Elements of C++ Chapter 2.
 2007 Pearson Education, Inc. All rights reserved C Formatted Input/Output.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Chapter 4 – The Building Blocks Data Types Literals Variables Constants.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Streams Streams –Sequences of characters organized.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
 Pearson Education, Inc. All rights reserved Formatted Output.
 2005 Pearson Education, Inc. All rights reserved Formatted Output.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Writing Web Pages By Shyam Gurram. Agenda Writing Web Pages Delimiting PHP Program Units. Displaying Output to Web Pages Putting Comments in PHP Programs.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Input, Output, and Processing
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
The Fundamentals of C++ Chapter 2: Basic programming elements and concepts JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Introduction to Java Java Translation Program Structure
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
C++ Basics Tutorial 5 Constants. Topics Covered Literal Constants Defined Constants Declared Constants.
Chapter 4 Literals, Variables and Constants. #Page2 4.1 Literals Any numeric literal starting with 0x specifies that the following is a hexadecimal value.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
 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.
Programming Fundamentals. Summary of previous lectures Programming Language Phases of C++ Environment Variables and Data Types.
PHP Programming with MySQL Slide 3-1 CHAPTER 3 Working with Data Types and Operators.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Basic Scripting & Variables Yasar Hussain Malik - NISTE.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 5 The Selection Structure.
Introduction to PHP and MySQL (Creating Database-Driven Websites)
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
28 Formatted Output.
Chapter Topics The Basics of a C++ Program Data Types
Java Primer 1: Types, Classes and Operators
Basic Elements of C++.
TMF1414 Introduction to Programming
The Selection Structure
Basic Elements of C++ Chapter 2.
Introduction to Primitive Data types
Introduction to Primitive Data types
Presentation transcript:

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition1  Wiley and the book authors, 2002 PHP Bible Chapter 6: Types in PHP

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition2  Wiley and the book authors, 2002 Summary Understanding the eight PHP types: integer, double, boolean, NULL, string, array, object, and resource Creating, reading, printing, and manipulating objects of different types Converting from one type to another

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition3  Wiley and the book authors, 2002 Don’t worry, be happy All programming languages have some kind of “type” system which specifies the different kinds of values that can appear in programs PHP makes it easy not to worry too much about typing of variables and values because it does not require variables to be typed and it handles a lot of type conversions for you PHP will generally do the “right” thing converting types when necessary  $pi = ; results in a floating-point number (double) with the integer 3 implicitly converted into floating point before the addition is performed  $sub = substr(12345, 2, 2); results in a string and the integer is converted to a string “12345” prior to executing the substr function which requires a string as the first argument

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition4  Wiley and the book authors, 2002 Type summary PHP has a total of eight types used implicitly in its code:  Integers: whole numbers, without a decimal point (e.g. 495)  Doubles: floating-point numbers (e.g or 49.0)  Booleans: only two possible values, TRUE and FALSE  NULL: special type that only has one value, NULL  Strings: sequences of characters (e.g. ‘PHP 4.0 supports string ops’)  Arrays: named and indexed collections of other values  Objects: instances of programmer-defined classes which can package up both other kinds of values and functions that are specific to that class  Resources: special variables that hold references to resources external to PHP (e.g. database connections, file connections) Integers, doubles, booleans, NULL, and strings are simple types Arrays and objects are compound types (can package up arbitrary values of arbitrary types) Resources are special types that programmers do not manipulate directly other than requesting resources via special functions and passing them onto other functions that need them

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition5  Wiley and the book authors, 2002 Integers Simplest type, corresponding to whole numbers, both positive and negative Can be read in 3 formats:  decimal (base 10) – default if no other base is specified  octal (base 8) – specified with a leading 0  hexadecimal (base 16) – specified with a leading 0x PHP integers correspond to the C long type  Max values depend on the word-size of your machine  For most common platforms, the largest integer is 2 31 – 1 (2,147,483,647)  Smallest negative integer is –(2 31 – 1) or -2,147,483,647  No MAXINT constant to check on the system you are running PHP on

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition6  Wiley and the book authors, 2002 Doubles Floating-point numbers (e.g , 0.456, 2.0) By default, doubles print with the minimum number of decimal places needed  Just because a numeric variable prints out as an integer does not necessarily mean that it is stored as an integer Doubles can be entered in normal or scientific notation  $small_negative = ;  $small_positive = 5.5e-3;

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition7  Wiley and the book authors, 2002 Booleans Booleans are true-or-false values generally used in control statements like the “testing” portion of an if statement All other PHP types can be interpreted as booleans  Numbers are false if exactly equal to zero, otherwise they are true  Caveat: Don’t use doubles as booleans since they rarely equal exactly 0.0 (e.g. sqrt(2.0) * sqrt(2.0) – 2.0 does not equal 0)  Strings are false if they are empty (no characters) or the string ‘0’, true otherwise  NULL is always false  Arrays and objects are false if they contain no other values  Valid resources are true, false when the requested resource could not be assigned

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition8  Wiley and the book authors, 2002 NULL Only 1 possible value for the NULL type, NULL To assign the NULL value to a variable, simply assign it  $my_var = NULL; NULL is capitalized by convention, but is case-insensitive NULL represents the lack of a value  A variable that has been assigned the NULL value is nearly indistinguishable from a variable that has not been set at all  It evaluates to FALSE in a boolean context  Returns FALSE when tested with IsSet()  PHP will not print warnings if you pass the variable to functions and back again, unlike variables that have never been set  Use it when you want to make it clear to both the reader of your code and to PHP that this is what you want

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition9  Wiley and the book authors, 2002 Single-quoted Strings Strings are sequences of characters May be singly or doubly-quoted Single-quoted strings are read and store their characters literally $variable = ‘name’; $literally = ‘My $variable will not print!\n’; print($literally); would print “My $variable will not print!\n”  To embed a single quote in a singly-quoted string, precede it with a \  To embed a backslash in a singly-quoted string, precede it with another \ Single-quoted strings execute much quicker than double-quoted strings since very little of the string has to be interpreted

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition10  Wiley and the book authors, 2002 Double-quoted Strings Strings delimited with double-quotes are preprocessed in both the following ways in PHP  Certain character sequences beginning with a backslash are replaced with special characters  \n is replaced with the newline character  \r is replaced with the carriage return character  \t is replaced with the tab character  \$ is replaced with the dollar-sign character itself  \” is replaced with a double-quote  \\ is replaced with a backslash  Variable names (anything starting with an un-escaped $) are replaced with string representations of their values  Undefined variables are replaced with the empty string In both types of strings, newline characters can be inserted literally as well as with the \n escaped sequence in double-quoted strings making it easy to enter entire blocks of HTML code into a single string

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition11  Wiley and the book authors, 2002 Arrays Arrays give a programmer a means to group a bunch of different values together and index them by number or by name Can be used to replace variables like $thing1, $thing2, $thing3 with a single indexed variable array like $thing[1], $thing[2], $thing[3] Array elements are referred to via indices in brackets and elements of different types can be assigned into the same array To create an array element or change an existing elements value, simply assign a value to its index like a normal variable $thing[1] = “value”; $thing[2] = 3.111; print(“$thing[1] is $thing[2] - $thing”);  Would output “value is – Array”

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition12  Wiley and the book authors, 2002 Arrays (cont.) Arrays in PHP are implemented differently than arrays in other languages  Instead of reserving contiguous blocks of predefined space, PHP arrays are allocated on the fly as space is needed  Implemented as a hash rather than indexed memory locations  PHP array elements can be created with arbitrary indices  Array elements created without specifying the index begin with an index of 0 and are incremented automatically $things[] = 1; $things[] = 2; print(“$things[0]:$things[1]”);  would output 1:2  Array indices can also be strings instead of integers and are known as associative arrays $food[‘Spanish’] = ‘paella’; $food[‘Scottish’] = ‘haggis’;

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition13  Wiley and the book authors, 2002 Objects PHP supports the use of objects in its attempt to utilize object- oriented programming techniques (OOP) Objects are similar to arrays in that it is a compound type which lets you package other data values together in a single unit Objects, however, have many more complex properties including the ability to package functions as wells as data and the capability to define new object types in terms of previously defined object types

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition14  Wiley and the book authors, 2002 Resources Resources are special values that refer to memory or system resources that are external to the PHP language itself Resources are not created by the programmers, they call special functions that return values of the resource type, and then pass these values to other functions that may require resources of that type $db_resource = mysql_connect(‘db_name’); mysql_query(‘SELECT * FROM table’,$db_resource);

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition15  Wiley and the book authors, 2002 Type Testing Because variables can change types due to reassignment or you cannot easily find where a variable’s type was set, PHP provides several functions to find out what the type of a value assigned to a variable is at execution time  gettype(arg) : returns a string representing the type of the argument passed in ('integer', 'double', 'string', 'array', 'object', or 'unknown type')  is_int(arg), is_integer(arg), is_long(arg) : returns TRUE if arg is an integer, false otherwise  is_double, is_float, is_real, is_bool, is_null, is_string, is_array, is_object, is_resource all perform the same type checking as is_int for the appropriate type in the function name

_______________________________________________________________________________________________________________ PHP Bible, 2 nd Edition16  Wiley and the book authors, 2002 Assignment and Coercion PHP generally automatically converts from one type to another when the content demands it. Generally, these automatic conversions result in the behavior the programmer wants Some conversions that don't make sense are undefined (e.g. Object type to numeric type) and the current behavior for your version of PHP may change with other versions of PHP Complete list of automatic conversions on page 95 Explicit conversions are also support in 3 ways  Functions intval(arg), doubleval(arg), and strval(arg) will convert arg no matter what its source type to the target type in the name of the function  Any expression can be type cast converting the result of the expression into the specified type by putting the name of the type in parentheses preceding the expression (e.g. (integer)$string_var * 5)  settype(variable,target_type_string) works complimentary to the gettype(arg) function where you specify the type you want as a string in the second argument