search

php 基本函数
定义常数
define('APPCONFIG','plaConfig');
获取常数
echo constant('APPCONFIG')."\n";
echo APPCONFIG."\n";
常数不能定义两次

初始化变量值
ini_set('display_errors',1);
获取变量值。
echo ini_get('display_errors')."\n";


用PHP的
ob_start();

相关函数简介:
1、Flush:刷新缓冲区的内容,输出。
函数格式:flush()
说明:这个函数经常使用,效率很高。
2、ob_start :打开输出缓冲区
函数格式:void ob_start(void)
说明:当缓冲区激活时,所有来自PHP程序的非文件头信息均不会发送,而是保存在内部缓冲区。为了输出缓冲区的内容,可以使用ob_end_flush()或flush()输出缓冲区的内容。
3 、ob_get_contents :返回内部缓冲区的内容。
使用方法:string ob_get_contents(void)
说明:这个函数会返回当前缓冲区中的内容,如果输出缓冲区没有激活,则返回 FALSE 。
4、ob_get_length:返回内部缓冲区的长度。
使用方法:int ob_get_length(void)
说明:这个函数会返回当前缓冲区中的长度;和ob_get_contents一样,如果输出缓冲区没有激活。则返回 FALSE。
5、ob_end_flush :发送内部缓冲区的内容到浏览器,并且关闭输出缓冲区。
使用方法:void ob_end_flush(void)
说明:这个函数发送输出缓冲区的内容(如果有的话)。
6、ob_end_clean:删除内部缓冲区的内容,并且关闭内部缓冲区
使用方法:void ob_end_clean(void)
说明:这个函数不会输出内部缓冲区的内容而是把它删除!
7、ob_implicit_flush:打开或关闭绝对刷新
使用方法:void ob_implicit_flush ([int flag])
说明:使用过Perl的人都知道$|=x的意义,这个字符串可以打开/关闭缓冲区,而ob_implicit_flush函数也和那个一样,默认为关闭缓冲区,打开绝对输出后,每个脚本输出都直接发送到浏览器,不再需要调用 flush()


例子:

ob_start();
echo "hello\n";
$c=ob_get_contents();
ob_end_flush();
echo "cache=".$c;
?>

输出:
hello
cache=hello



$t0=0;
$s0=0;
$t1=0;
$s1=0;
$t2=0;
$s2=0;
$t3=0;
$s3=0;
if (ob_get_level()){
$t0=1;
$s0=ob_get_level();
}
ob_start();
if (ob_get_level()){
$t1=1;
$s1=ob_get_level();
}
ob_start();
if (ob_get_level()){
$t2=1;
$s2=ob_get_level();
}
ob_start();

if ( ob_get_level()){
$t3=1;
$s3=ob_get_level();
}
ob_end_flush();
ob_end_flush();
ob_end_flush();
echo "t0=".$t0."\n";
echo "s0=".$s0."\n";
echo "t1=".$t1."\n";
echo "s1=".$s1."\n";
echo "t2=".$t2."\n";
echo "s2=".$s2."\n";
echo "t3=".$t3."\n";
echo "s3=".$s3."\n";


输出:
t0=0
s0=0
t1=1
s1=1
t2=1
s2=2
t3=1
s3=3

die( )
函数输出一条消息,并退出当前脚本。

该函数是 exit() 函数的别名。
语法

die(status)

参数 描述
status 必需。规定在退出脚本之前写入的消息或状态号。状态号不会被写入输出。
说明

如果 status 是字符串,则该函数会在退出前输出字符串。

如果 status 是整数,这个值会被用作退出状态。退出状态的值在 0 至 254 之间。退出状态 255 由 PHP 保留,不会被使用。状态 0 用于成功地终止程序。


file_exists()
文件是否存在,
is_readable()
文件是否可读
realpath()
绝对路径
extension_loaded()
是否安装了某模块
if(extension_loaded('pcre')){ } 是否安装了正则模块

require require_once include 区别

都是包含另一个php文件的意思。
require,require_once 的php文件必须存在,找不到则退出。
require与require_once 的区别
require_once 会做检查,看看前面是否已经引入了该php文件,若引入了则不再引入。



t.php

echo extension_loaded('pcre');
echo "\n";
require "aa.php";
require_once "aa.php";
echo "end\n";
?>

aa.php

echo "aa\n";
?>

执行 t.php
1
aa
end


isset unset var_dump

isset()
检测变量是否设置
unset()
释放变量
var_dump()
打印变量内存信息

$a=1;
if(isset($a)){

echo "0 isset\n";
}

var_dump($a);
unset($a);
if(isset($a)){

echo "1 isset\n";
}
输出
0 isset
int(1)


explode(split,str)
把字符串分割为数组

$str_a="a b c d e";
$arr_a=explode(' ',$str_a);
var_dump($arr_a);
结果
array(2) {
  [0]=>
  string(10) "0.89340000"
  [1]=>
  string(10) "1305184931"
}

Statement
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
Explain how load balancing affects session management and how to address it.Explain how load balancing affects session management and how to address it.Apr 29, 2025 am 12:42 AM

Load balancing affects session management, but can be resolved with session replication, session stickiness, and centralized session storage. 1. Session Replication Copy session data between servers. 2. Session stickiness directs user requests to the same server. 3. Centralized session storage uses independent servers such as Redis to store session data to ensure data sharing.

Explain the concept of session locking.Explain the concept of session locking.Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

Are there any alternatives to PHP sessions?Are there any alternatives to PHP sessions?Apr 29, 2025 am 12:36 AM

Alternatives to PHP sessions include Cookies, Token-based Authentication, Database-based Sessions, and Redis/Memcached. 1.Cookies manage sessions by storing data on the client, which is simple but low in security. 2.Token-based Authentication uses tokens to verify users, which is highly secure but requires additional logic. 3.Database-basedSessions stores data in the database, which has good scalability but may affect performance. 4. Redis/Memcached uses distributed cache to improve performance and scalability, but requires additional matching

Define the term 'session hijacking' in the context of PHP.Define the term 'session hijacking' in the context of PHP.Apr 29, 2025 am 12:33 AM

Sessionhijacking refers to an attacker impersonating a user by obtaining the user's sessionID. Prevention methods include: 1) encrypting communication using HTTPS; 2) verifying the source of the sessionID; 3) using a secure sessionID generation algorithm; 4) regularly updating the sessionID.

What is the full form of PHP?What is the full form of PHP?Apr 28, 2025 pm 04:58 PM

The article discusses PHP, detailing its full form, main uses in web development, comparison with Python and Java, and its ease of learning for beginners.

How does PHP handle form data?How does PHP handle form data?Apr 28, 2025 pm 04:57 PM

PHP handles form data using $\_POST and $\_GET superglobals, with security ensured through validation, sanitization, and secure database interactions.

What is the difference between PHP and ASP.NET?What is the difference between PHP and ASP.NET?Apr 28, 2025 pm 04:56 PM

The article compares PHP and ASP.NET, focusing on their suitability for large-scale web applications, performance differences, and security features. Both are viable for large projects, but PHP is open-source and platform-independent, while ASP.NET,

Is PHP a case-sensitive language?Is PHP a case-sensitive language?Apr 28, 2025 pm 04:55 PM

PHP's case sensitivity varies: functions are insensitive, while variables and classes are sensitive. Best practices include consistent naming and using case-insensitive functions for comparisons.

See all articles

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function