PHP Basics 1

Source: Internet
Author: User
PHP Basics 1. Basic syntax
  1. Start and end mark :" "

     
       

    Note:End mark of the PHP code segment at the end of the file "?> "No. in some cases, it is better to omit include () or require.

  2. Like java and c, each php statement must end with a semicolon.

2. eight basic data types supported by type php

Four scalar types:

  • Boolean (boolean)
  • Integer)
  • String (string)
  • Float (float type)

Two composite types:

  • Array (array type)
  • Object)

Two special types:

  • Resource)
  • Null)

Note:

  • Var_dump () is used to view the expression value and type. is_type () is used to view a type.
  • Integer overflow. If an integer is given or the integer obtained from the calculation result exceeds the int range (2 ^ 32), float is interpreted.
III. string)

Declaration string:A string is usually defined by single or double quotation marks.

  1. Variables and special characters in single quotes are not replaced

    Php> $ age = 12; php> echo "He is very tall \ n his age is only $ age"; he is very tall his age is only 12; php> echo 'the he is tall \ n his age is $ age'; he is tall \ n his age is $ age;
  2. Strings can be connected using the '.' (vertex) operator.

     php > echo "Hello,"."World"; Hello,World
  3. Access and modify characters in strings (similar to arrays)

     php > $str = "Hello,World"; php > echo $str[1]; e php > echo $str{0}; H
  4. Convert other types to strings)

    Add (string) before a value or use the strval () function to convert it into a string type.

     php > $age = 18; php > var_dump($age); int(18) php > $str = (string)$age; php > var_dump($str); string(2) "18"

Common string functions

  • String replacement

    Str_replace ($ search, $ replace, $ subject [, int & $ count])

    $ Search: search for replacement target values

    $ Replace: $ search replacement value

    Subject: Array or string to be replaced

    Count: controls the number of matching and replacement times.

     php > $str = "Hello,My name is Tom,what is your name?"; php > echo str_replace("name","nickname",$str); Hello,My nickname is Tom,what is your nickname?
  • Remove spaces on both sides of the string

    Trim ($ str)

    $ Str: Target string

      php > $str = "  Hello     ";  php > echo trim($str);  Hello
  • Remove the html and php tags from the string.

    Strip_tags ($ str)

    $ Str: Target string

      php > $str = "

    Hello World

    "; php > echo strip_tags($str); Hello World
  • Repeat the target string multiple times

    Str_repeat ($ str, int $ num)

    $ Str: Target string

    $ Num: repeated times

      php > $str = "Hello World!!";  php > echo str_repeat($str,5);  Hello World!!Hello World!!Hello World!!Hello World!!Hello World!!
  • Returns the string length.

    Strlen ($ str)

    $ Str: Target string

      php > $str = "Hello World";  php > echo strlen($str);  11
  • Calculates the number of times a string appears.

    Substr_count (string $ haystack, string $ needle)

    $ Haystack: Target string

    $ Needle: Search for the number of occurrences of a string

      php > $str = "Hello World,Hello China";  php > echo substr_count($str,"Hello");  2
  • Truncate string

    Substr (string $ string, int $ start [, int $ length])

    $ String: Target string

    $ Start: if start is not negative, the returned string starts from the start position of string and starts from 0. If start is a negative number, the returned string starts from the start character at the end of the string. If the string length is less than or equal to start, FALSE is returned.

    $ Length: 1. if a positive length is provided, the returned string starts from start and contains a maximum of length characters (depending on the length of the string ). 2. if a negative length is provided, many characters at the end of the string will be missed (if start is a negative number, it will be counted from the end of the string ). 3. if start is not in this text, an empty string is returned. 4. if the length value is 0, FALSE, or NULL, an empty string is returned. 5. if length is not provided, the returned substring starts from the start position until the end of the string.

      php > $str = "Hello,World";  php > echo substr($str,1);  ello,World  php > echo substr($str,1,3);  ell  php > echo substr($str,1,-3);  ello,Wo  php > echo substr($str,1,10);  ello,World

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.