PHP简介
PHP是一种用于创建动态WEB页面的服务端脚本语言。如同ASP和ColdFusion,用户可以混合使用PHP和HTML编写WEB页面,当访问者浏览到该页面时,服务端会首先对页面中的PHP命令进行处理,然后把处理后的结果连同HTML内容一起传送到访问端的浏览器。但是与ASP或ColdFusion不同,PHP是一种源代码开放程序,拥有很好的跨平台兼容性。用户可以在Windows NT系统以及许多版本的Unix系统上运行PHP,而且可以将PHP作为Apache服务器的内置模块或CGI程序运行。
除了能够精确的控制WEB页面的显示内容之外,用户还可以通过使用PHP发送HTTP报头。用户可以通过PHP设置cookies,管理用户身份识别,并对用户浏览页面进行重定向。PHP具有非常强大的数据库支持功能,能够访问几乎目前所有较为流行的数据库系统。此外,PHP可以与多个外接库集成,为用户提供更多的实用功能,如生成PDF文件等。
用户可以直接在WEB页面中输入PHP命令代码,因而不需要任何特殊的开发环境。在WEB页面中,所有PHP代码都被放置在“”中。此外,用户还可以选择使用诸如等的形式。PHP引擎会自动识别并处理页面中所有位于PHP定界符之间的代码。
PHP脚本语言的语法结构与C语言和Perl语言的语法风格非常相似。用户在使用变量前不需要对变量进行声明。使用PHP创建数组的过程也非常简单。PHP还具有基本的面向对象组件功能,可以极大的方便用户有效组织和封装自己编写的代码。
PHP语法概述
A. 基本语法
即使初次接触PHP的用户也会发现自己对PHP的语法风格并不陌生。
例如:
echo “Hello!”;
? >
显示结果为“Hello!”。
PHP中,所有的变量都以标识“$”开头。我们可以对上述代码做如下改动:
$greeting = “Hello!”;
echo $greeting;
? >
改变后的代码显示结果不变。
PHP中使用“.”符号连接不同的字符串,而其它的算术运算符则继承了流行编程语言的风格。举例如下:
$greeting = “Hello!”;
$num = 3 + 2;
$num++;
echo “$greeting $num people!”;
? >
显示结果为“Hello!6 people!”。
PHP对各种运算符及运算规则都有一套完整的规定,如果用户具有C或C++语言的编程背景的话,可以发现一切都得心应手。
与Perl语言一样,在PHP中,如果在双引号包含的字符串中含有变量的话,该变量将用相应的变量值替换;如果字符串被单引号包含,则不做替换。例如:
$name = ‘PETER';
$greeting_1 = “Hello, $name!”;
$greeting_2 = ‘Hello, $name!';
echo “$greeting_1\n”;
echo “$greeting_2\n”;
? >
显示结果为:
Hello, PETER!
Hello, $name!
(注:上述代码中的“\n”为换行符,只能在双引号字符串下使用)
B. 变量
PHP允许用户象使用常规变量一样使用环境变量。例如,在页面http://www.nba.com/scores/index.html中包含如下代码:
echo “[$REQUEST_URI]”;
? >
则输出结果为[/scores/index.html]
C. 数组
用户在使用PHP创建数组时,可以把数组索引(包括常规索引或关联索引)加入方括号中。例如:
$fruit[0] = ‘banana';
$fruit[1] = ‘apple';
$favorites['animal'] = ‘tiger';
$favorites['sports'] = ‘basketball';
如果用户在向数组赋值时不指明数组下标,PHP将自动把该对象加入到数组末尾。例如对于上述$fruit数组可以用以下方式赋值而保持结果不变,
$fruit[] = ‘banana';
$fruit[] = ‘apple';
同样,在PHP中,用户还可以根据需要建立多维数组。例如:
$people[‘David'][‘shirt'] = ‘blue';
$people[‘David'][‘car'] = ‘red';
$people[‘Adam'][‘shirt'] = ‘white';
$people[‘Adam'][‘car'] = ‘silver';
在PHP中,用户还可以使用array()函数快速建立数组。例如:
$fruit = array(‘banana',‘apple');
$favorites = array(‘animal' = > ‘tiger', ‘sports' = > ‘basketball');
或者使用array()函数创建多维数组:
$people = array (‘David' = > array(‘shirt' = > ‘blue','car' = > ‘red'),
‘Adam' = > array(‘shirt' = > ‘white',‘car' = > ‘silver'));
此外,PHP还提供了内置函数count()用于计算数组中的元素数量。例如:
$fruit = array(‘banana', ‘apple');
print count($fruit);
显示结果为2。
D. 结构控制
在PHP中,用户可以使用“for”或“while”等的循环结构语句。例如:
for ($i = 4; $i
print “I have eaten $i apples today.\n”; }
或
$i = 4; while ($i
print “I have eaten $i apples today.\n”;
$i++;
}
返回结果为:
I have eaten 4 apples today.
I have eaten 5 apples today.
I have eaten 6 apples today.
I have eaten 7 apples today.
此外,用户还可以使用“if”和“elseif”等的选择性结构语句。例如:
if ($user_count > 200) {
print “The site is busy right now!”;}
elseif ($user_count > 100) {
print “The site is active right now!”;
else {
print “The site is idle - only $user_count user logged on.”;
}

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

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

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
