Home>Article>Backend Development> PHP basic variables and data types

PHP basic variables and data types

little bottle
little bottle forward
2019-04-19 15:31:45 2245browse

Recently, a friend asked me about PHP variables and data types, which made me speechless for a while, so I found resources from the Internet and learned about them together. Friends who are interested can also come and learn more!

PHP Variables

Variables

During the execution of the program, the amount that can change is a variable.

Declare variables

  • Declare with dollar sign

  • Note : (PHP is strictly case-sensitive)

    • The variable name starts with a letter or underscore, followed by numbers/letters/underscores, and cannot contain special characters

    • It is best for variable names to have clear meaning

    • It is best for variable names to use camel case or underline Method

      • Hump

        • ## Small hump: firstName

        • Big hump: FirstName

      • ##Underscore

        • first_name

    #Variable variable

    Equal substitution
  • Use variables

    Write the name of the variable directly
    • $Variable name

##PHP data types

8 main data types

Scalar type (feature: can only store a single data)
  • Integer int | integer
    • ##Integer

      • Category

        • decimal

          • octal

          • Hex

          • ##Storage range
        • Signed (-2.1 billion to 2.1 billion)

          • Unsigned (0 to 42 billion)

          • If the integer storage range is exceeded, overflow will occur

        Float type float | double | real
    • With decimal point

      • Scientific notation, e or E

      • ##Note:

      • Floating point numbers have errors, do not compare the sizes of two floating point numbers

        • Boolean bool | boolean
    • true

      • false

      • String type string can only store a single data

    • Delimiter

      • ' ' 不解析变量

      • " " 解析变量

      • heredoc “”

        • <<<名称

        • <<<"名称"
      • nowdoc

    • 转义符

      • \n 换行 \r 回车 \t 水平制表符 \\ \ \' ' \" " \$ $
    • 花括号 {}

      • 可以将PHP中的变量扩成一个整体来解析

        • {$变量名}

        • ${变量名}

      • 可以将字符串中指定字符进行增删改查的操作

        • 字符串的下标 从0开始

        • 根据下标找到对应的字符进行操作

  • 复合类型

    • 数组 array

    • 对象 object

  • 特殊类型

    • 资源 resource

    • 空 null | NULL

  • 5种伪类型

    • number

    • mixed

    • callback

    • void

    • ...

    PHP数据类型转换

    自动转换(隐式转换)

    程序根据上下文自动转换

    • 其他类型转换为数值型

      • true -> 1

      • false -> 0

      • null -> 0

      • 字符串如果以 非法数值开始,直接转换成0

      • 如果字符串以合法数值开始,一直取到第一个非法数值结束

    • 其他类型转换为字符串类型

      • 数值型直接转换成数值本身

      • true -> 1

      • false -> 空字符串

      • null -> 空字符串

      • 数组 -> array

      • 资源 -> resource

      • 对象 不能转换为字符串

    • 其他类型转换成布尔型

      • 0 -> false

      • 0.0 -> false

      • 空字符串 ‘’ 或者"" , ‘0’或者 “0” , -> false

      • null -> false

      • 空数组 -> false

      if (条件) { 执行条件为真的代码段; }else { 执行条件为假的代码段; }

    强制转换(显示转换)

    • 临时转换 (不会改变变量本身的类型)

      • (变量类型)$变量名称

        • 整型 (int | integer)$变量名称

        • 浮点型 (float | double | real)$变量名称

        • 字符型 (string)$变量名称

        • 布尔型 (bool | bollean)$变量名称

        • 空 (unset)$变量名称

        • 数组 (array)$变量名称

        • 对象 (object)$变量名称

      • 通过系统函数实现

        • intval

        • floatval

        • ...

    • 永久转换

      • settype($var,$type)
      • gettype($var)
    • 通过变量函数库检查变量的类型

      想学习更多PHP相关知识,请关注PHP中文网的PHP视频教程

    The above is the detailed content of PHP basic variables and data types. For more information, please follow other related articles on the PHP Chinese website!

    Statement:
    This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete