PHP开发基础

Jun 23, 2016 pm 02:31 PM

一、变量与表达式
PHP变量以美元符$开头,以名称作为变量之间的区分,变量名长度为如:$_name=”zhoulang”;//合法    $name=”zhoulang”;//合法   $1name=”zhoulang”;不合法
二、打印与输出变量
1、echo语句,如:echo “123″;
2、printf函数:用于格式化输出字符串,主要用于字符串中以%开头的格式字符串替换(以%开头的格式字符串,在%后面跟有各种格式字符,以说明输出数据的类型、形式、长度、小数点位数)。语法:boolean printf(string format[,mixed args])
如:printf(”%15s”, “some text”);   //运行结果:some text
3、sprintf函数:基本跟printf函数相同,但是它可以将转换后的结果保存到一个字符串变量中,而不是直接输出。
如:$formatted=sprintf(”%15s”,”some text”);
4、printf和sprintf所支持的格式转换字符表
%???打印出百分比符号,不转换
B???整数转换成二进制数
C???整数转成对应的ASCII字符
D???整数转换成十进制数
F???倍精确度数字转成浮点数
O???整数转换成八进制数
S???整数转成字符串
x/X???整数转成小写/大写的十六进制数
三、显示数组与对象
1、print_r($array/$var):打印数组,不过也可以打印普通 变量。
print_r($_GET);//打印使用GET方法传递的表单内容
2、var_dump($object/$array/$var):可以打印对象、数组、已经标量变量。
var_dump($DB);//打印$DB数据库连接对象的内容。
3、var_export($object/$array/$var):输出或返回一个变量的字符串表示。此函数返回关于传递给该函数的变量的结构信息,它和print_r()类似,不同的似其返回的表示似合法的php代码,可以通过将函数的第二个参数设置为true,从而返回变量的表示。
$a = array(1,2,array(”a”,”b”,”c”));
echo var_export($a);
echo “
”;
echo var_export($a, true);
四、变量的变量
在php中,可以创建一个变量的引用,即一个变量中包含其他的变量,称为变量中的变量,也称为动态变量。由于在脚本中变量值不是确定的,因此使用变量的变量来创建变量名并不一定遵循变量名命名规约。
$$var_name = “php5″;
$$var_name=”php5 web开发”;
echo $php5;//显示 php5 web开发
$$name=’123′;
$$name=’456′;
echo ${’123′};//显示456
function myfunc() {
echo “函数内容!”;
}
$f=’myfunc’;
$f();//将调用myfunc函数
五、超级全局变量数组
1、php超级全局变量列表
$_GET[]???-获得以GET方法提交的变量数组
$_POST[]???-获得以POST方法提交的变量数组
$_COOKIE[]???-获取和设置当前网站的Cookie标识
$_SESSION[]???-取得当前用户访问的唯一标识,以数组形式体现,如sessionid以及自定义session数据
$_ENV[]???-当前php环境变量数组
$_SERVER[]???-当前php服务器变量数组
$_FILES[]???-上传文件时提交到当前脚本的参数值,以数组形式体现
$_REQUEST[]???-包含当前脚本提交的全部请求,包含了$_GET、$_POST、$_COOKIE、$_SESSION的所有动作
$GLOBALS[]???-该超级变量数组包含正在执行脚本所有超级全局变量的引用内容
2、$_SERVER超级全局变量数组
1、$_SESSION['PHP_SELF'] ? 获取当前正在执行脚本的文件名
2、$_SERVER['SERVER_PROTOCOL'] ? 请求页面时通信协议的名称和版本。例如,“HTTP/1.0”。
3、$_SERVER['REQUEST_TIME'] ? 请求开始时的时间戳。从 PHP 5.1.0 起有效。和time函数效果一样。
4、$_SERVER['argv'] ? 传递给该脚本的参数。我试了下,get方法可以得到$_SERVER['argv'][0];post方法无法给他赋值。
5、$_SERVER['SERVER_NAME'] ? 返回当前主机名。
6、$_SERVER['SERVER_SOFTWARE'] ? 服务器标识的字串,在响应请求时的头信息中给出。 如Microsoft-IIS/6.0
7、$_SERVER['REQUEST_METHOD'] ? 访问页面时的请求方法。例如:“GET”、“HEAD”,“POST”,“PUT”。
8、$_SERVER['QUERY_STRING'] ? 查询(query)的字符串(URL 中第一个问号 ? 之后的内容)。
9、$_SERVER['DOCUMENT_ROOT'] ? 当前运行脚本所在的文档根目录。在服务器配置文件中定义。 如E:\server
10、$_SERVER['HTTP_ACCEPT'] [...]

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,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

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 is REST API design principles? What is REST API design principles? Apr 04, 2025 am 12:01 AM

RESTAPI design principles include resource definition, URI design, HTTP method usage, status code usage, version control, and HATEOAS. 1. Resources should be represented by nouns and maintained at a hierarchy. 2. HTTP methods should conform to their semantics, such as GET is used to obtain resources. 3. The status code should be used correctly, such as 404 means that the resource does not exist. 4. Version control can be implemented through URI or header. 5. HATEOAS boots client operations through links in response.

How do you handle exceptions effectively in PHP (try, catch, finally, throw)? How do you handle exceptions effectively in PHP (try, catch, finally, throw)? Apr 05, 2025 am 12:03 AM

In PHP, exception handling is achieved through the try, catch, finally, and throw keywords. 1) The try block surrounds the code that may throw exceptions; 2) The catch block handles exceptions; 3) Finally block ensures that the code is always executed; 4) throw is used to manually throw exceptions. These mechanisms help improve the robustness and maintainability of your code.

What are anonymous classes in PHP and when might you use them? What are anonymous classes in PHP and when might you use them? Apr 04, 2025 am 12:02 AM

The main function of anonymous classes in PHP is to create one-time objects. 1. Anonymous classes allow classes without names to be directly defined in the code, which is suitable for temporary requirements. 2. They can inherit classes or implement interfaces to increase flexibility. 3. Pay attention to performance and code readability when using it, and avoid repeatedly defining the same anonymous classes.

See all articles