Home php教程 php手册 php入门教程之常用数据类型和基本语法

php入门教程之常用数据类型和基本语法

Jun 13, 2016 am 09:52 AM
php Getting Started Tutorial and Basic Commonly used number data integer have float type grammar

php中数据类型有整型、小数型(浮动数)、布尔类型、字符及数组,变量,常量等下面我们一起来看看吧。

一 PHP常用数据类型

1.基本数据类型

1.1整型
1.2小数型(浮动数) 包含单精度和双精度
1.3布尔类型 (表示真,和 假)
1.4字符串

2.复合数据类型

2.1数组(array)
2.2对象 (object)

3.特殊数据类型

3.1null
3.2资源类型(resource)

二 PHP的基本语法

1.PHP 定义变量必需以 $ 这个符号开头,区分大小写。
2.变量的名称,应当以字母或者下划线开头,不要用数字开头,也不要用特殊字符开头。

第一个PHP程序

 代码如下 复制代码
echo "Hello World!";
?>

1 注释
1.1多行
/*
xxxx
*/
1.2单行
//xxxxx
2.赋值
$a = 'test';
2.1 检查变量是否已声明
isset($a)
2.2 释放变量
unset($a);
2.3 静态变量
static $a;
静态变量可在一个函数几次调用中保持数值,而不会被系统释放,但只能在声明它的函数集中访问到,只能在第一次声明时初始化。
3. 基本类型
3.1 数字类型
3.1.1整数(integer,关键字int)
.整数可用8进制 10进制 16进制 表示
$a=123; //10进制
$b=0123; //8进制
$c=0x123; //16进制
.由于操作系统不同整数精度变化很大,但32位是最常见的
3.1.2浮点(float,关键字float,64位浮点数,精度14位)
.PHP中float和double是等同的
.使用浮点数切记:他们只是近似值
如:2.5在内部常被表示为2.499999999
又如:
if(0.7+0.1>=0.8){
echo 'a';
}else{
echo 'b';
}
返回值为b,这取决于浮点数的确切实现方式,推荐做法是避免使用浮点值做比较
3.2 字符串
.用单引号或双引号包围
如:echo "Hello"; echo 'Hello';
.双引号中的变量会被解释,单引号中的则不会
如:var $name = 'jano';
echo "my name is $name.";//显示 my name is jano
echo 'my name is $name'; //显示 my name is $name
.双引号中的变量可用{}包围来区分变量和后面的字母
如:var $n = "my name is {$name}Yu";//如果没有{}就无法区分变量和字符
.heredoc
$a = skjdfjsd
lksdfjsdlf
HTML; //后面的表示必须顶在最前面
.获取字符串中的某个字符
$a = 'Hello';
echo $a{1}; // 显示e
echo $a[1]; // 显示e
推荐使用第一种写法可以和数组区分开
3.3 布尔值
true false
4.常用函数
. nl2br 吧字符串中的换行符转化成

如:echo nl2br($a);
.var_dump
显示变量类型和值,如:var_dump($a);
.print_r
var_dump加强版,打印对象类型和内容,数组则打出所有元素,类对象则打印所有成员
如:$a = array(1,2,3,4,5);
print_r($a);
5.数组
数组用array方法声明
例:
$a = array('a','b','c');
$a = array('a','b',array(1,2,3));
.默认从索引0开始赋值
如:$a[]='a'; //$a[0]='a';
$a[]='b'; //$a[1]='b';
.使用字符串值做索引
如:
$a = array('car'=>'Ferrari','Number'=>21,'City'=>'CQ');
echo $a['car'];
.遍历并更改数组元素值
foreach($array as $key=>&$value){// &$value
//...
}
6.特殊类型和值
.NULL 区分大小写,表示无值,从没赋值,用unset清除
.资源
7.强制类型转换
(int)$a
(float)$a
(string)$a
(bool)$a
(array)$a
(object)$a
.特殊的
(int)"0123";//返回123,没有把8进制0123转换为10进制数字
(int)"123 mu tou ren";//返回123
(int)"mu tou ren 123";//返回0,因为转换只从第一个字符开始读,发现非数字立即停止
.转换为布尔值
非空非零则为true(包括正数和负数),0为false
包含0个元素的数组为false
NULL为false
 
转换为整数
.浮点转换为整型
小数点后的数字舍弃,如果超过证书有效位,结果可能是0或者最小负数
.布尔转换为整型
true为1
false为0
.字符串转换为整型
对字符串左侧第一位进行判断。如果第一位是数字,则会从第一位开始将读取到数字转换成整数。如果第一位不是数字结果为0.
.PHP没有提供其他类型转换为整数的方法
 
转换为浮点数
.整数转换为浮点数
结果不变
.布尔转换为浮点数
true为1
false为0
.字符串转换成浮点数
与整数类似
.PHP没有提供其他类型转换为浮点数的方法
 
转换为字符串
将非字符串转换为字符串的方法是在变量前使用“(string)”强制转换。
规则如下:
1.整型或浮点型:结果为其值
2.布尔型:true转换为'1' , false转换为空字符串
3.对象或数组:如果被转换的变量是对象或数组,则转换结果将为字符串对象或字符串数组,需要根据实际情况进行分析。
4.资源类型:返回资源标识号
8.类型判断与获取
 
.转换成数组
在变量前用“(array)”强制转换。将变量转换成员变量数据类型相同的数组,数组中只有一个元素。
如:
$a=1;
print_r((array)$a);
结果:
Array
(
    [0]=> 1
)
 
.转换为对象
在变量前用“(object)”强制转换。将生成一个新对象,其中名为scalar的成员变量将包含原变量的值。如:
$a=1;
$o = (object)$a;
echo $o->scalar;
 
使用函数进行数据转换
bool settype(var, string type)
 
type值: boolean,integer,float,string,array,object,null
 
.判断类型函数
is_integer 如:is_integer($a); //返回true或false
is_float
is_double
is_real
is_int
is_long
is_numeric
is_string
is_bool
is_array
is_object
is_null
is_resource
is_scalar  是否为一个标量
.类型获取
gettype($a);
9.变量和常量
.常量
define('NUM_USR','0');
$u = NUM_USR;
.引用
$a=0;
$b = &$a;
$b++;
echo $a;//显示1,因为$b是$a的引用,$b改变就表示$a改变
10.操作符
10.1 数学操作符
+ - * / %(取余)
10.2 比较运算符
==
=== 值相同,类型相同
!=
和!=一样都是不等于
!== 值相同,类型不同
>
>=
10.3 逻辑运算符
and && 与
or || 或
xor 异或,若一个为true,但不是两个都为true,则结果为true
! 非
10.4 按位操作
& 按位与
| 按位或
^ 按位异或
~ 按位非
>> 右移位
10.5 三元操作符
表示问号前面的表达式是否为true,是则返回冒号前面的值,不是则返回冒号后面的值
如:
$c = $a > $b ? 1:2;
echo $a>$b ? "hello":"no";
.下面两个语句是等价的
$a = ($b != 'china') ? true : false;
$a = $b != 'china';
10.6 其他操作符
++ 自增
-- 自减
@ 忽略特定函数的调用失败报错,如:$u=@file(xxx);
. 字符串连接操作,如:$a = 'hello'.'world'; $a = 'hello'.$a;
11.7 特殊的逻辑运算符表达式
$a = 0;
$b = 100;
echo $a || $b;//$a转换为bool值为true时,echo $a,否则echo $b,不管$b表达式是否为true,此表达式会始终显示100
echo $a && $b;//将什么都不显示,因为整个表达式$a && $b返回false
$a = 1;
$b = 0;
echo $a && $b;//将什么都不显示,因为整个表达式$a && $b返回false
echo $a && $b;//始终显示$a
$a = 1;
$b = 0;
$a && $b=12;
echo $b;//显示12,$a是否为true,为true就会执行$b=12,系统首先读取&&,知道这是个与,于是开始执行&&前面的语句,发现返回true就再 执行&&后面的语句,发现返回false就不会再执行&&后面的语句了,因为&&逻辑,只要有一个false 整个表达式就变成false

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)

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

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.

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

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

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.

See all articles