Home Backend Development PHP Tutorial PHP基础知识点汇总(一)

PHP基础知识点汇总(一)

Jun 23, 2016 pm 01:17 PM

一、PHP的基本语法

PHP(Hypertext Preprocessor,超文本预处理器)是一种运行在服务器端的脚本语言。

1.PHP语言标记
  
  
   短风格的标记 ?>
  

2.PHP指令分割符
  PHP需要在每个语句(指令)后用分号结束!

3.程序注释
  // 单行注释
  # 单行注释
  /* 多行注释 */
  /**多行文档注释 */

4.变量
  简言之,变量是用于临时存储值的容器。(变量在任何语言中都处于核心地位)

  变量的命名:
  PHP中声明变量必须是使用一个美元符号"$"加上后面的变量名来表示,使用赋值操作符(=)来给一个 变量赋值。

  变量的命名:
  一个有效的变量名是由字母或下划线开头,后面跟上任意数量的字母、数字或者下划线。要注意的是,变量名一定不能以数字开头,并且中间不可以使用空格,不能使用点分开  等!

  按照正常的正则表达式,他将被表示成:'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'。

  可变变量:
  $str = 'hello';
  $$str = 'world';

  echo "$str $hello"; //输出hello world
  echo "$str $$str"; //输出hello world

  变量的引用赋值:
  简单的使用"&"加到将要赋值的变量前。这意味着新的变量简单的引用了原始变量。(换言之,“成为其别名”或者“指向”)。
  $foo = 'Bob';
  $bar = &$foo;

  $bar = '世界,你好!';
  echo $bar; //输出世界,你好!
  echo $foo; //输出世界,你好!

  $foo = 'hello world';
  echo $foo; //输出hello world
  echo $bar; //输出hello world

  变量的类型:
  

                     |-----boole布尔型
                   |-----integer整形
        |-----四种标量类型----  |-----float浮点型,也称double

        |             |-----string字符串

        |

   数据类型--|
        |             |-----array数组 
        |-----两种复合类型-----|
        |           |-----object对象

        |             |-----resource资源
        |-----两种特殊类型-----|
                     |-----NULL

  布尔型(TRUE or FALSE):
  布尔值FALSE
  整型值0为假,其他非零值不论正负均为TRUE
  浮点型0.0
  空白字符串和字符串'0'
  没有成员变量的数组
  没有单元的对象(仅适用于PHP4)
  特殊类型NULL

  整型:如果给定数超出整型范围,将会被解释成float。

  浮点型:范围在1.7E-38~1.7E+38之间,精确到小数点15位。

  字符串:可以使用单引号、双引号和定界符三种方法定义!

  数组:可以存放多个数据,并且可以存入任何类型的数据。

  对象:由属性和方法构成。属性表示对象状态,方法表示对象功能!

  资源类型:保存在外部资源的一个引用,通过专门的函数进行建立和使用!

  NULL类型:NULL不表示空格,不表示零,也不表示空字符串,而是表示一个变量的值为空。
  将变量直接赋值为NULL;
  声明的变量未被赋值
  被unset()函数销毁的变量

  伪类型:
  mixed:说明一个参数可以接受多种不同的(但并不必须是所有的)类型。
  number:说明一个参数可以是integer后者float。
  callback:接受用户自定义的函数作为参数。

  数据类型相互转换:

  自动类型转换
  布尔型TRUE将转化为1,FALSE转化为0。
  NULL转化为0。
  整型和浮点型进行运算,先将整型自动转化为浮点型,再进行运算
  字符串和数字型参与预算,字符串先转化为数字,再进行运算。

  强制类型转换
  (int),(integer):转换成整型
  (bool),(boolean):转换成布尔型
  (float),(double),(real):转换成浮点型
  (string):转换成字符串
  (array):转换成数组
  (object):转换成对象
  或使用具体的转换函数:intval(),floatval()和strval()。
  注:整型转换为浮点型,由于其精度范围小于浮点型,所以转换后精度不会改变,但是浮点型
  转换为整型时,会自动舍弃其小数部分。

检测变量类型:
  is_bool():是否为布尔型
  is_int(),is_integer(),is_long():是否为整型
  is_float(),is_double(),is_real():是否为浮点型
  is_string():是否为字符串
  is_array():是否为数组
  is_object():是否为对象
  is_resource():是否为资源类型
  is_null():是否为空
  is_scalar():是否是标量,也就是是否为整数、浮点数、布尔型或字符串。
  is_numeric():是否是任何类型的数字或数字字符串
  is_callable():判断是否是有效的函数名

常量:用于一些固定的值!

常量的声明:通过使用define()函数声明常量,常量名照样区分大小写,按照惯例,一般常量名全大写,常量名前不要加"$"。
example:define('NAME','xiaozhang');

echo NAME; //输出xiaozhang

常量和变量的区别:
  常量前没有"$"符号
  常量只能通过define()函数定义,不能通过赋值
  常量可以不用理会变量范围的规则而在任何地方定义和访问
  常量一旦定义就不能被重新定义或者取消定义,直到脚本运行结束自动释放
  常量的值只能是标量类型

PHP中常用魔术常量:
  __FILE__:当前的文件名
  __LINE__:当前的行数
  __FUNCTION__:当前的函数名
  __CLASS__:当前的类名
  __METHOD__:当前对象的方法名

运算符
  算数运算符:
    + 加
    - 减
    * 乘
    /  除
    % 取余(求模)
    ++ 累加
    -- 累减

   注:$a++先计算表达式然后再执行递增的操作,++$a先执行递增操作,再计算表达式的值。累减同理!

  赋值运算符:
    = 将一个值或表达式计算结果赋给变量
    += 将变量与所赋值相加后的结果再赋给该变量
    -= ......
    *= ......
    /= ......
    %= ......
    .= 将变量与所赋值相连后的结果再赋给该变量

  比较运算符:
    >  大于
         >= 大于等于
         == 等于
    === 全等于
    或!= 不等
    !== 不全等
    注:==和===的区别在于==只关心参与比较的数的值是否相等,而不管类型是否相同!

  逻辑运算符:
    and或&& 逻辑与 两边必须都为TRUE才为TRUE
    or 或|| 逻辑或 两边只要一个为TRUE就为TRUE
    not或! 逻辑非 取反,若表达式为TRUE则结果为FALSE
    xor 逻辑异或 两边不同时为TRUE

  表达式:PHP的基石,几乎所编写的任何代码都可以看做是一个表达式,通常是变量、常量和运算符的组 合等!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles