Home php教程 php手册 PHP入门教程之PHP变量与常量学习

PHP入门教程之PHP变量与常量学习

Jun 21, 2016 am 08:53 AM
php quot test

  上个月我专门介绍了PHP入门教程中关于PHP基本语法的入门学习,主要介绍了常用的几种PHP标记符,PHP语句的构成,PHP的注释等,今天的PHP入门教程我们主要学习PHP基本语法中PHP变量和常量的基础知识。

  针对PHP变量入门学习,本篇入门教程分以下几部分介绍:PHP变量如何标识、PHP变量如何声明、如何给PHP变量赋值、PHP变量的类型介绍、常用PHP变量函数介绍。

  针对PHP常量入门学习,主要介绍PHP常量的定义和使用方式。

  一、PHP变量如何标识

  所谓标识符,其实也就是PHP变量名,主要以字母、数字、下划线和美元符($)组成,长度可以任意长,不能以数字开头,切记在PHP中,变量是区分大小写的(PHP系统自带的函数是个例外,不区分大小写)。

  特别提醒:在定义PHP变量时最好不要使用和PHP系统自带函数或者自带系统变量一样的名称,容易搞混,另外在定义PHP变量时,为了保持比较好的编码习惯,当变量由多个单词组成时第一个单词开头字母小写,第二个单词开头字母大写…,依此类推,这是我的个人建议。

  二、PHP变量声明和赋值

  和C++等编程语言不同,PHP在使用变量时,不需要事先声明,在你给变量赋值时就可以使用了,赋值时使用’='。如

1

$test = '欢迎访问www.leapsoul.cn,这里有最新的PHP入门教程';

  三、PHP变量的类型

  和其他语言一样,PHP的变量类型同样支持整型、字符串、数组、对象等,区别在于其他语言,比如C语言,在使用变量之前需要事先声明变量的数据类型,而PHP变量的数据类型不需要事先声明,在你给他赋值的时候就已经确定了。如

1
2
3

$leapsoul = 1; //定义PHP变量的数据类型为整型
$leapsoul = array();//定义PHP变量的数据类型为数组
$leapsoul = "欢迎访问www.leapsoul.cn,本文主要介绍PHP入门教程之PHP变量与常量学习";//定义PHP变量的数据类型为字符串

  在PHP中有一种特殊的数据类型-不定变量,可以使我们动态修改变量名,之前我们说到PHP变量的定义是以美元符($)开始的,如果在开头再加一个美元符($),就变成不定变量了,即

1
2

$test = 'leapsoul';
$$test = "欢迎访问www.leapsoul.cn,本文主要介绍PHP入门教程之PHP变量与常量学习";

等同于

1

$leapsoul = "欢迎访问www.leapsoul.cn,本文主要介绍PHP入门教程之PHP变量与常量学习";

  四、PHP变量函数

  PHP变量函数主要用来对变量的数据类型、变量的存在性进行判断,测试PHP变量类型的函数有:

  gettype():返回传递过来的变量的数据类型,如果不是标准数据类型,如整型、字符串、数组、对象等,则返回unknown type;

  settype():改变传递过来的变量的数据类型,类似于强制类型转换。

1
2
3
4
5
6
7
8
9
10
11

$test;

$leapsoul = "PHP入门教程之PHP变量与常量学习";

echo gettype($test);//输出NULL

echo gettype($leapsoul);//输出变量类型为string

settype($leapsoul,"int");//设定$leapsoul变量类型为int

echo gettype($leapsoul);//数据变量类型为integer

  判断PHP变量是否为具体数据类型的函数有

  is_array():判断PHP变量类型是否为数组类型

  is_string():判断PHP变量类型是否为字符串型

  is_object():判断PHP变量类型是否为对象类型

  更多类似的函数你可以参考PHP的帮助文档

  测试PHP变量存在性的函数

  主要用到isset和empty这两个函数,区别在于isset函数用来判断这个变量是否存在,如果存在则返回true,否则返回false,而empty函数主要用来判断这个变量的值是否为空,或者说这个变量有没有赋值,如果为空则返回true,否则返回false,这两个函数在PHP表单变量提交到后台处理时非常有用,原则上先使用isset对变量的存在性进行判断,变量如果存在,则根据需要对必填选项的变量值可以使用empty函数来进行判断。

  至此PHP入门教程之PHP变量的相关知识就介绍完了,下面我们看下PHP常量如何使用和定义

  PHP常量如何定义和使用

  PHP常量通过define函数来进行定义,常量名一般使用大写字母,一旦常量被定义,则在脚本过程中就不能更改了,通常在开发大型项目是,我们一般将一些常用的函数,常量事先放在一个配置文件中,在使用时将它包含进来,这样也便于管理。

1
2
3

define("INTRO","这段代码展示了PHP入门教程之PHP常量该如何定义与使用");

echo INTRO;

  通过上面的代码实例,我们可以看到PHP常量和变量的区别在于,在使用常量时它前面没有美元符,只需要使用它的名字就可以,而变量在使用时是带美元符的。

  更多的PHP系统常量和环境变量你可以通过phpinfo()函数看到。PHP环境变量$_SERVER和系统常量详细说明

  至此,PHP入门教程之基本语法中的PHP变量和常量的基础知识就介绍完毕了,在下一篇PHP入门教程中我将主要介绍PHP函数的定义与使用,并顺带介绍PHP变量作用域的相关知识。

  :PHP网站开发教程-leapsoul.cn版权所有,转载时请以链接形式注明原始出处及本声明,谢谢。



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 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)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

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.

See all articles