search
HomeBackend DevelopmentPHP TutorialPHP中常用的输出函数总结_PHP

echo();  "输出内容";

可以同时输出多个字符串,可以多个参数,并不需要圆括号 ,无返回值。

print();   有返回值1,0切只能包含一个参数

只可以同时输出一个字符串,一个参数, 需要圆括号,有返回值, 当其执行失败时返 flase。
print 的用法和 C语言很像,所以会对输出内容里的 % 做特殊解释。

代码如下:


$a=print(‘hi');
echo $a;
//—————————-

hi 1 //1  是 $a 的值。

//—————————–


 

die();   一般用于数据库连接的判断,一旦执行die()后面的内容将不执行

die(); //    和 exit() 区别。

有两个功能:先输出内容,然后退出程序。(常用在链接服务器,数据库)

代码如下:


mysql_connect(“locahost”,”root”,”root”) or die(”链接服务器失败!“);


 

printf();   与C语言的用法一样,可以格式化输出

printf();   //f 指 format 格式化

printf(“参数 1″,参数 2) :参数 1= 按什么格式输出;参数 2= 输出的变量。

(% s: 按字符串; %d: 按整型整型 ; %b: 按二进制; % x: 按 16 进制; %X:按16 进制大写输出; %o: 按八进制; % f: 按浮点型 )
函数,返回输出字符个数,把文字格式化以后输出,如:

代码如下:


printf (“$%01.2f” , 43.2); //$43.20

$ 表示填充的字符
0 表示位数不够在不影响原值的情况下补
1 表示输出的总宽度
2 表示小数位数 ,有四舍五入
%f 是表示显示为一个浮点数

格式化命令及说明:

%% 印出百分比符号,不转换。
%b 整数转成二进位。
%c 整数转成对应的 ASCII 字符。
%d 整数转成十进位。
%f 倍精确度数字转成浮点数。
%o 整数转成八进位。
%s 整数转成字符串。
%x 整数转成小写十六进位。
%X 整数转成大写十六进位。

代码如下:


$num=100.001;
printf(“%d”,$num); //100
printf(“%s”,$num); //100.001
printf(“%s—%d—%b—%x—%o—%f”,$num,$num,$num,$num,$num,$num)
//100.001—100—1100100—64—144—1001.00100
printf(“%.2f”,$num); //100.00 ( 小数点保留 2 位)
printf(“%.1f”,$num); //100.0 ( 小数点保留 1 位)
printf(“%`#10s”,$num); //#10s
printf(“%#10s”,$num); //10s
?>

sprintf;   把输出内容存放在变量中

此并不能直接输出,先赋给一个变量,然后再输出变量。

代码如下:


$num=100.001;
$a=sprintf(“%d”,$num);
echo $a; //100
?>


print_r();   用于输出数组

功能:只用于输出数组。

代码如下:


$a = array (1, 2, array (“a”, “b”, “c”));
print_r ($a);


返回:

代码如下:


Array ( [0] => 1 [1] => 2 [2] => Array ( [0] => a [1] => b [2] => c ) )

var_dump();   可以输出任何内容

输出变量的容,类型或字符串的内容,类型,长度。常用来调试。

代码如下:


$a=100;
var_dump($a); //int(100)

$a=100.356;
var_dump($a); //float(100.356)

?>


 

var_export ();
返回关于传递给该函数的变量的结构信息,它和var_dump()类似,不同的是其返回的表 示是合法的 PHP 代码。

您可以通过将函数的第二个参数设置为TRUE ,从而返回变量的值。

代码如下:


$a = array (1, 2, array (“a”, “b”, “c”));
var_export ($a);
/*
输出:

array (
0 => 1,
1 => 2,
2 =>
array (
0 => ‘a',
1 => ‘b',
2 => ‘c',
),
)
*/
$b = 3.1;
$v = var_export($b, TRUE);
echo $v;
/*
输出:

3.1
*/
?>


 

简化用法:

代码如下:


$color = "red";
?>

Roses are


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
Optimize PHP Code: Reducing Memory Usage & Execution TimeOptimize PHP Code: Reducing Memory Usage & Execution TimeMay 10, 2025 am 12:04 AM

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

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 Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools