php基础总结(一)
php一共支持4种标记风格:
1)
echo "这是xml风格标记"; //推荐使用
?>
2)
3)
echo "简短风格的标记"; ?>
4)
echo "这是ASP风格的标记";
%>
/*
如果要使用简短风格和asp风格,需要在php.ini中对其进行配置,然后将short_open_tag和asp_tags设置为ON
*/
php支持三种注释:
// c++风格的单行注释
/* */ c风格的多行注释
# shell风格的注释
/*
在单行注释里不要出现?>标志,因为解释器会认为php脚本结束,而去执行其后面的内容
*/
php中的数据类型:
php一共支持8中原始类型,包括4种标量类型(boolean,integer,float,string)
两种复合类型:array,object
两种特殊类型:resource,null
关于boolean
布尔类型有两个值:true/false
*在php中不只false才是假,0 0.0 "0" "" 只声明没有赋值的数组都是false
关于string
有三种定义字符串的方式:单引号(推荐)、双引号、界定符
单引号与双引号区别:
1)使用单引号时,只要对单引号进行转义,使用双引号则需要对" $等字符通过\转义
2)使用单引号,里面的内容会原样输出,使用双引号,php将花一些时间来处理字符串的转义和变量的解析
界定符:
$s =
字符串内容
str; //前面不能有空格,和双引号没什么区别
关于integer
整形数可以用十进制,八进制,十六进制表示,如果是八进制,加前导0,如果是十六进制,加0x
如果在八进制中出现了非法数字(8,9),则后面的数字会忽略掉
如果给定的数值超出了int类型所能表示的最大范围,将会当做float类型处理,这种情况称为整数溢出
关于float
浮点数的数值只是一个近似值,尽量避免浮点数的比较,因为结果往往是不准确的
关于null
不区分大小写,
被赋予空值的情况有3种:还没有赋任何值,被赋值为null,被unset()函数处理过的变量?
判断是否为null的函数是is_null(),返回值为boolean类型
从php4开始,unset()不再有返回值
强制类型转换:
(boolean) 转换为boolean
(string) ;
(integer) ;
(float) ;
(array) ;
(object) ;
*转换成boolean时,null,0和未赋值的变量或数组会被转换成false,其他的为真
转换成整形时,布尔型的false为0,true为1,浮点数的小数会被舍去,字符型如果以数字开头就截取到非数字位,否则输出0
类型转换还可以通过settype()函数来完成,
bool settype(mixed var,string type)
参数var为指定的变量,参数type为指定的类型,有7个可选值(boolean,float,integer,array,null,object,string)
settype函数会把原变量的类型转换了,而强制类型转换只是产生一个临时变量,原变量类型不会改变
监测数据类型的函数:
is_bool() is_string() is_float()/is_double() is_integer()/is_int() is_null() is_array() is_object() is_numeric()
is_numeric()检查变量是否为数字或由数字组成的字符串
php常量:
用define()函数来定义常量:
define(string constant_name,mixed value,case_insensitive)
constant_name 常量名称
value 常量值
case_sensitive 可选,指定是否大小写敏感,true为不敏感
获取常量的值有两种方法,一是使用常量名直接获取值,另一种是使用constant()函数
mixed constant(string const_name) //返回常量的值
要判断一个常量是否已经定义,可以使用defined()函数
bool defined(string constant_name)
php预定义常量:
__FILE__ php程序文件路径
__LINE__ 这个常量所在行
PHP_VERSION 程序版本
PHP_OS 执行php解析器的操作系统名称
php变量:
php中有引用赋值,用不同的名字访问同一个变量的内容,当改变其中一个变量的值时,另一个也跟着发生变化,使用&符号来表示
$i = "Hello";
$s = &$i;
变量作用域
局部变量 在函数的内部定义的变量,其作用域是所在函数
全局变量 被定义在所有函数以外的变量,其作用域为整个php文件,但是在用户自定义函数内无法访问,如果希望在用户自定义函数内使用全局变量,则要使用global声明
静态变量 能够在函数调用结束后仍保留变量值
如:
$i = 'hello';
function fun(){
global $i;
echo $i; //输出hello
}
?>
可变变量:
可变变量是一种独特的变量,它允许动态改变一个变量名称,其工作原理是该变量的名称是由另外一个变量的值来确定的,实现过程就是在变量的前面再多加一个美元符号
$i = 'abc';
$abc = 'hello';
echo $$i; //hello
www.2cto.com
php预定义变量:
$_SERVER['SERVER_ADDR'] 当前运行脚本所在的服务器的ip地址
$_SERVER['SERVER_NAME'] 当前运行脚本所在的服务器主机的名称
$_SERVER['REQUEST_METHOD'] 访问页面时的请求方法
$_SERVER['REMOTE_ADDR'] 正在浏览当前页面的用户ip
$_SERVER['REMOTE_HOST'] 正在浏览当前页面用户的主机名
$_SERVER['REMOTE_PORT'] 用户连接到服务器时所使用的端口
摘自 青春华航的专栏

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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,

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

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

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

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.
