PHP: Hypertext Preprocessor - PowerPoint PPT Presentation

About This Presentation
Title:

PHP: Hypertext Preprocessor

Description:

The easiest way to specify a simple string is to enclose it in single quotes (the character ' ... If the string is enclosed in double-quotes ('), PHP ... – PowerPoint PPT presentation

Number of Views:196
Avg rating:3.0/5.0
Slides: 21
Provided by: raje49
Category:

less

Transcript and Presenter's Notes

Title: PHP: Hypertext Preprocessor


1
PHP Hypertext Preprocessor
2
About PHP
  • PHP is a widely-used Open Source general-purpose
    scripting language that is especially suited for
    Web development and can be embedded into HTML

3
String In PHP
  • A string is series of characters. In PHP, a
    character is the same as a byte, that is, there
    are exactly 256 different characters possible.
  • It is no problem for a string to become very
    large. There is no practical bound to the size of
    strings imposed by PHP,

4
  • A string literal can be specified in three
    different ways.
  • Single quoted
  • double quoted
  • Heredoc

5
Single quoted
  • The easiest way to specify a simple string is to
    enclose it in single quotes (the character ').
  • Eg
  • To specify a literal single quote, you will need
    to escape it with a backslash (\)
  • Eg

6
Double quoted
  • If the string is enclosed in double-quotes ("),
    PHP understands more escape sequences for special
    characters like (\n,\t etc)
  • But the most important feature of double-quoted
    strings is the fact that variable names will be
    expanded.
  • eg

7
Heredoc Syntax
  • Another way to delimit strings is by using
    heredoc syntax ("ltltlt"). One should provide an
    identifier after ltltlt, then the string, and then
    the same identifier to close the quotation.
  • Heredoc support was added in PHP 4.
  • eg

8
String Operations
  • There are two string operators. The first is the
    concatenation operator ('.'), which returns the
    concatenation of its right and left arguments.
    The second is the concatenating assignment
    operator ('.'), which appends the argument on
    the right side to the argument on the left side.
  • eg

9
String Functions
  • These functions all manipulate strings in various
    ways.
  • Using the strlen() function
  • The strlen() function is used to find the length
    of a string.
  • Eg

10
  • Using the strpos() function
  • The strpos() function is used to search for a
    string or character within a string.
  • If a match is found in the string, this function
    will return the position of the first match. If
    no match is found, it will return FALSE.
  • eg

11
  • the strcmp() function compares two strings.
  • This function returns
  • 0 - if the two strings are equal
  • lt0 - if string1 is less than string2
  • gt0 - if string1 is greater than string2
  • Syntax
  • strcmp(string1,string2)
  • Eg

12
  • strncmp() function
  • This function is similar to strcmp(), with the
    difference that you can specify the (upper limit
    of the) number of characters from each string to
    be used in the comparison.
  • Note that this comparison is case sensitive.
  • 0 - if the two strings are equal
  • lt0 - if string1 is less than string2
  • gt0 - if string1 is greater than string2
  • Syntaxstrncmp(string1,string2,length)
  • Eg

13
  • The strncasecmp() function
  • String comparison of the first n characters
    (case-insensitive)
  • This function returns
  • 0 - if the two strings are equal
  • lt0 - if string1 is less than string2
  • gt0 - if string1 is greater than string2
  • Eglt?php echo strncasecmp("Hello world!","hello
    earth!",6) ?gt

14
  • The strrev() function reverses a string.
  • Syntax
  • strrev(string)
  • Eglt?php echo strrev("Hello World") ?gt
  • ResdlroW olleH

15
  • The substr() function returns a part of a string.
  • Syntax
  • substr(string,start,length)string
  • Required. Specifies the string to return a part
    of
  • start
  • Required. Specifies where to start in the string
  • A positive number - Start at a specified position
    in the string
  • A negative number - Start at a specified position
    from the end of the string
  • 0 - Start at the first character in string
  • length
  • Optional. Specifies the length of the returned
    string. Default is to the end of the string.
  • A positive number - The length to be returned
    from the start parameter
  • Negative number - The length to be returned from
    the end of the string
  • eg

16
  • The trim() function removes whitespaces and other
    predefined characters from both sides of a
    string.
  • Syntax
  • trim(string,charlist)
  • string
  • Required. Specifies the string to check
  • charlist
  • Optional. Specifies which characters to remove
    from the string. If omitted, all of the following
    characters are removed
  • "\0" - NULL
  • "\t" - tab
  • "\n" - new line
  • "\x0B" - vertical tab
  • "\r" - carriage return
  • " " - ordinary white space
  • eg

17
  • The strstr() function searches for the first
    occurrence of a string inside another string.
  • This function returns the rest of the string
    (from the matching point), or FALSE, if the
    string to search for is not found.
  • Syntax
  • strstr(string,search)
  • string
  • Required. Specifies the string to search
  • search
  • Required. Specifies the string to search for. If
    this parameter is a number, it will search for
    the character matching the ASCII value of the
    number
  • eg

18
  • The str_replace() function replaces some
    characters with some other characters in a
    string.
  • Syntax
  • str_replace(find,replace,string,count)
  • find
  • Required. Specifies the value to find
  • replace
  • Required. Specifies the value to replace the
    value in find
  • string
  • Required. Specifies the string to be searched
  • count
  • Optional. A variable that counts the number of
    replacements
  • eg

19
  • The str_pad() function pads a string to a new
    length.
  • Syntax
  • str_pad(string,length,pad_string,pad_type)
  • string
  • Required. Specifies the string to pad
  • length
  • Required. Specifies the new string length. If
    this value is less than the original length of
    the string, nothing will be done
  • pad_string
  • Optional. Specifies the string to use for
    padding. Default is whitespace
  • pad_type
  • Optional. Specifies what side to pad the string.
    Possible values
  • STR_PAD_BOTH
  • STR_PAD_LEFT
  • STR_PAD_RIGHT
  • Eg

20
  • The str_word_count() function counts the number
    of words in a string.
  • Syntax
  • str_word_count(string,return,char)
  • string
  • Required. Specifies the string to check
  • return
  • Optional. Specifies the return value of the
    str_word_count() function. Possible values
  • 0 - Default. Returns the number of words found
  • 1 - Returns an array with the words from the
    string
  • 2 - Returns an array where the key is the
    position of the word in the string, and value is
    the actual word
  • char
  • Optional. Specifies special characters to be
    considered as words
  • eg
Write a Comment
User Comments (0)
About PowerShow.com