Home php教程 php手册 php基础总结(一)

php基础总结(一)

Jun 13, 2016 am 10:46 AM
echo lang php script xml one use Base Summarize recommend support mark style

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'] 用户连接到服务器时所使用的端口


摘自 青春华航的专栏

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)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1257
29
C# Tutorial
1231
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.

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

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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

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.

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.

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP vs. Python: Core Features and Functionality PHP vs. Python: Core Features and Functionality Apr 13, 2025 am 12:16 AM

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

See all articles