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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks 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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

See all articles