WEB的应用
当客户端向服务器的程序提出请求时,web服务器根据请求响应对应的页面,当页面中含有PHP脚本时,服务器会交给PHP解释器进行解释执行,将生成的html代码再回传给客户端,客户端的浏览器解释html代码,最终形成网页格式的页面。
PHP能做什么
PHP主要应用于三个领域:
PHP分析器,一个WEB服务器器和一个WEB浏览器。
PHP语法结构
编程语言的词法结构(lexical structure)是指管理如何用语言写程序的基本规则的集合。
用户自定义的函数名或类名都是不区分大小写的,变量区分大小写。这就是说,$name 、$NAME 和$NaMe是三个不同的变量。
PHP用分号来分隔简单的语句。
PHP注释
PHP支持C、C++和Shell脚本风格的注释,如下:
// 单行注释
/* */ 多行注释 (注意:不能嵌套)
# 脚本注释
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
常量
常量是一个固定数值,用一个简单的标识符进行定义,常量默认为大小写敏感。
按照惯例常量标识符总是大写的。
define()用这个函数来定义常量。
string(字符串)常量分为:内置常量和自定义常量。
常数只能包含标量数据(boolean(布尔),integer(整形),float(浮点数))
内置常量:PHP系统提供的在任何页面数值不会改变的常量
PHP_OS:显示服务器的操作系统版本
PHP_VERSION:显示PHP版本
一些常见的系统常量
__FILE__:PHP文件名,假如是引用文件,则显示引用文件名
__LINE__:PHP文件的行数
TRUE FALSE:表示真假的常量
E_ERROR:指明了代码中最近的错误处
E_WARNING:指明了代码中最近的警告处
E_PARSE:分析代码何处有着潜在的问题
E_NOTICE:为发生不寻常但不一定是错误的地方
自定义常量
用define()来定义常量
define(“mycomputer”, “IBM”);
定义常量:mycomputer 常量的值为 IBM
defined(“mycomputer”);
检测常量是否被定义,已定义返回1 否则返回空
变量
PHP 中一个美元符号($)后面跟上一个变量名称,即表示一个变量。变量的名称是对大小写敏感的
$var = ‘Bob';
$Var = ‘Joe';
echo “$var, $Var”; // 输出 “Bob, Joe” 可以同时输出两个变量名
$4site = ‘not yet'; // 非法变量名;以数字开头 变量不能以数字开头
$_4site = ‘not yet'; // 合法变量名;以下划线开头 可以以下划线开头
$i站点is = ‘mansikka'; // 合法变量名; 可以用中文 但是不建议使用
isset($var) //检查变量是否定义
unset($var) //删除变量$var
empty($var) //判断一个变量的值是否存在
echo $var //空
>
可变变量
一个可变变量获取了一个普通变量的值作为这个可变变量的变量名
$a = 'hello'; //普通变量
$$a = 'world'; //可变变量 可变变量用一个普通变量的值作为可变变量的名
echo "$a ${$a}"; //输出:hello world
echo "$a $hello"; //输出:hello world
>
常量和变量不同
常量前面没有美元符号($);
常量只能用 define( ) 函数定义,而不能通过赋值语句;
常量可以不用理会变量范围的规则而在任何地方定义和访问;
常量一旦定义就不能被重新定义或者取消定义;
常量的值只能是标量
数据类型
四种标量类型:
布尔型(boolean)
整型(integer)
浮点型(float)(浮点数,也作double)
字符串(string)
两种复合类型:
数组(array)
对象(object)
最后是两种特殊类型:
资源(resource)
NULL 空
PHP是一种非常弱的类型语言。
在PHP中,变量的类型通常不是由程序员设定的,确切地说,是根据该变量使用的上下文在运行时(即变量的值)决定的。
实例:
$bool = TRUE; // 布尔型
$str = “foo”; // 字符串
$int = 12; // 整型
echo gettype($bool); // 输出 boolean (gettype获取变量的类型)
echo gettype($str); // 输出 string
>
整型
整型值可以用十进制,十六进制或八进制符号指定,前面可以加上可选的符号(- 或者 +)。
$a = 1234; // 十进制数
$a = -123; // 一个负数
$a = 0123; // 八进制数(等于十进制的 83)
$a = 0x1a; // 十六进制数(等于十进制的 26)
>
浮点型
浮点数(也叫浮点数,双精度数或实数)可以用以下任何语法定义:
$a = 1.234;
$a = 1.2e3;
$a = 7E-10;
>
字符串
string是一系列字符。在 PHP 中,字符和字节一样,也就是说,一共有 256 种不同字符的可能性。这也暗示 PHP 对 Unicode 没有本地支持。(关于字符串类型后面专有一章详细讲解)
布尔型
这是最简单的类型。boolean 表达了真值,可以为 TRUE 或 FALSE。
当其他类型转换为 boolean类型 时,以下值被认为是FALSE:
布尔值 FALSE
整型值 0(零)
浮点型值 0.0(零)
空白字符串和字符串 "0"
没有成员变量的数组
没有单元的对象(仅适用于 PHP 4)
特殊类型 NULL(包括尚未设定的变量)
所有其它值都被认为是 TRUE(包括任何资源)。
数组
数组是PHP中的一种重要的数据类型。一个标量只能存放一个数据, 而数组可以存放多个数据。
$my=array(‘1',‘2',‘abc',‘d');
对象(Object)
对象是一种高级的数据类型以后会学
资源(Resource)
资源是由专门的函数来建立和使用的
类型强制转换
PHP 中的类型强制转换:在要转换的变量之前加上用括号括起来的目标类型。
允许的强制转换有:
(int), (integer) - 转换成整型
(bool), (boolean) – 转换成布尔型
(float), (double), (real) - 转换成浮点型
(string) - 转换成字符串
(array) - 转换成数组
(object) – 转换成对象
$foo = 10; // $foo is an integer
$bar = (boolean) $foo; // $bar is a boolean
>

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 uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

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 makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

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


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

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment