PHP Data Types - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

PHP Data Types

Description:

Same identifier to close the quotation, followed by a semicolon ( ; ) Closing identifier must begin in the first column of the line ... – PowerPoint PPT presentation

Number of Views:669
Avg rating:3.0/5.0
Slides: 25
Provided by: capi7
Category:
Tags: php | data | keys | types

less

Transcript and Presenter's Notes

Title: PHP Data Types


1
PHP Data Types
  • PHP supports 8 data types
  • boolean, integer, float, string (scalar types)
  • array, object (compound types)
  • resource, NULL (special types)

2
PHP booleans
  • Use keywords true or false
  • Both are case insensitive
  • Can typecast a value to boolean, using (bool) or
    (boolean)
  • Usually not necessary automatic conversion to
    boolean if boolean value is needed

3
PHP booleans
  • When converted to boolean, the following values
    are considered false
  • boolean false, integer 0, float 0.0, empty
    string, string 0, an array with 0 elements, an
    object with 0 member variables, the special type
    NULL
  • everything else is true

4
PHP integers
  • Positive or negative
  • Decimal, hex, or octal notation
  • num1 879 // decimal notation
  • num2 0xAB45 // hex notation
  • num3 047 // octal notation

5
PHP floating point numbers
  • Positive or negative
  • a 1.234
  • b 1.2e3
  • c 7E-10
  • / If an integer value overflows, it is
    automatically converted to a float /

6
PHP strings
  • A string is a series of characters (there is no
    size limitation)
  • Single quotes
  • Double quotes
  • heredoc syntax

7
PHP strings single quotes
  • No expansion of variables and escape sequences,
    except and \
  • // Outputs Arnold once said "I'll be back"echo
    'Arnold once said "I\'ll be back"'
  • // Outputs You deleted C\.?echo 'You deleted
    C\\.?'
  • // Outputs You deleted C\.?echo 'You deleted
    C\.?'

8
PHP strings double quotes
  • Expansion of variables and escape sequences
  • \ ?
  • \\ ? \
  • \ ?
  • \t, \n, ? tab, new line, ..
  • num ? value of num

9
PHP strings Heredoc syntax
  • ltltlt followed by an identifier
  • Same identifier to close the quotation, followed
    by a semicolon ( )
  • Closing identifier must begin in the first column
    of the line
  • No space or tab before or after the semicolon

10
PHP strings Heredoc syntax
  • Variables are evaluated
  • Escape sequences are evaluated

11
PHP strings Heredoc
  • name PHP
  • version 4.2
  • echo ltltltEOTMy name is "name\n\n\n\n
    My version is versionEOT

12
PHP strings Heredoc
  • str ltltltEODExample of stringltBRgtspanning
    multiple linesltBRgtusing heredoc syntax.EOD

13
PHP strings Useful functions
  • str This is a test
  • first str0 T
  • strlen returns the length of a string
  • length strlen( str )
  • echo length 14
  • more str.HI
  • echo more This is a testHI

14
PHP back quotes
  • Can use back quotes to execute UNIX command (same
    as Perl)
  • today date
  • today contains output of UNIX command data
  • Can also do
  • echo date

15
PHP arrays
  • An array in PHP is actually an ordered map
  • A map is a type that maps values to keys.
    (associative array in Perl)
  • This type is optimized in several ways
  • ? you can use it as a real array, or a list
    (vector), hashtable (which is an implementation
    of a map), dictionary, collection, stack, queue
    and probably more.

16
PHP arrays
  • Syntax
  • array( key gt value , ... )
  • // key may be an integer or string
  • / if it is a float, it will be truncated to an
    integer /
  • // value may be any value

17
PHP arrays
  • arr array(ct366" gt Perl", 60 gt Pass)
  • echo arrct366" // Perl
  • echo arr60    // Pass

18
PHP arrays
  • If you do not specify a key for a given value,
    then the maximum of the integer indices is taken,
    and the new key will be that maximum value 1.
  • // This array is the same as ...array(5 gt 43,
    32, 56, "b" gt 12)
  • // ...this arrayarray(5 gt 43, 6 gt 32, 7 gt 56,
    "b" gt 12)
  • If you specify a key that already has a value
    assigned to it, that value will be overwritten.

19
PHP arrays Modifying, deleting values
  • arr array( 56, 67, 89 )
  • arr1 100 67 changed into 100
  • arr 300 default is index 3
  • unset( arr2 ) deletes value 89
  • unset( arr ) deletes whole array arr

20
PHP arrays Aggregate printing
  • arr array( 56, 67, 3gt89 )
  • print_r( arr ) aggregate printing
  • Array( 0gt56 1gt67 3gt89 )
  • reindex the array
  • changedIndexes array_values( arr )
  • print_r( changedIndexes )
  • Array( 0gt56 1gt67 2gt89 )
  • original array unchanged
  • works even if keys are strings

21
PHP objects
  • Object instance of a class
  • A class is a collection of variables and
    functions working with these variables. A class
    is defined using the following syntax
  • class Cart // C, Java like syntax
  •    var items  // member variable
  •   function foo( articleNumber, number)
  •        // some code   // more
    code here

22
PHP resource
  • A resource is a special variable, holding a
    reference to an external resource. Resources are
    created and used by special functions
  • As resource types hold special handlers to open
    files, database connections,

23
PHP NULL
  • Only one value
  • Case insensitive keyword NULL
  • Represents that a variable has no value
  • a NULL
  • Function is_null ? returns true if variable is
    null
  • if (is_null( a ) )

24
PHP NULL
  • A variable is considered NULL if
  • It has been assigned the constant NULL, or
  • It has not been set to any value yet, or
  • It has been unset()
Write a Comment
User Comments (0)
About PowerShow.com