PHP中几种输出函数的差别
PHP中几种输出函数的区别
?
echo:返回值是void,可以一次输出多个值,多个值之间用逗号分隔。echo是语言结构(language construct)也就是关键字,而并不是真正的函数,因此不能作为表达式的一部分使用。使用的时候不用加括号,加上也可以。只支持基本类型,布尔型除外,echo true的时候显示1,echo false的时候啥都没有。echo不可以输出数组类型;
print:返回值是int,只可以同时输出一个字符串,一个参数,还需要圆括号,不支持逗号分隔多个显示变量的语法有返回值;当执行失败时返回flase。print的用法和C语言相似,所以会对输出内容的%做特殊解释。print能输出数组类型的数据;
print_r:不仅可以打印变量的的值,还能显示变量类型,而且也可以显示数组和对象这样复杂的变量类型。print_r() 将把数组的指针移到最后边。使用reset()可让指针回到开始处,print_r()可以把字符串和数字简单地打印出来,而数组则以括起来的键和值得列表形式显示,并以Array开头;
printf:源于C语言中的printf()。该函数输出格式化的字符串。语法:printf("参数1",参数2):参数1=按什么格式输出;参数2=输出的变量。(%s:按字符串;%d: 按整型;%b:按二进制;%x:按16进制;%X:按16进制 大写输出;%o:按八进制; %f:按浮点型)。占位符被插入%符号之后,由数字和"\$" 组成。f=format:规定字符串以及如何格式化其中的变量;如下:printf("$%01.2f",43.2); //$43.20;$表示填充的字符;0表示位数不够在不影响原值的情况下补0;1表示输出的总宽度;%f则显示为一个浮点数;
sprintf:此并不能直接输出,先赋给一个变量,然后再输出变量。如下:
<?php $num=100.001;$a=sprintf("%d",$num);echo $a; //100?>;
?
var_dump:void var_dump ( mixed expression [, mixed expression [, ...]] )功能:输出变量的内容,类型或字符串的内容,类型,长度。常用来调试。如下:
<?php $a=100;var_dump($a); //int(100) $a=100.356;var_dump($a); //float(100.356)?>;
?
var_export:mixed var_export ( mixed $expression [, bool $return ] )返回关于传递给该函数的变量的结构信息,它和var_dump()类似,不同的是其返回的表示是合法的PHP 代码。你可以通过将函数的第二个参数设置为TRUE,从而返回变量的值。如下:
<?php $a = array (1, 2, array ("a", "b", "c"));//没有定义TRUE的情况;var_export ($a);$b = 3.1;$v = var_export($b, TRUE);echo $v;?>
?
输出:array (0 => 1,1 => 2,2 => array (0 => 'a',1 => 'b',2 => 'c',),),3.1;
总结:echo、print、printf可以打印变量内容,但不能显示数组及系统超级变量数组;print_r和var_dump不仅可以打印数组、标量变量,还可以打印对象的内容。
?

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics



A dump file usually refers to a binary file, also known as a dump file or core dump file. This kind of file is generated by the computer system when it encounters a serious error or abnormal situation. It is used to store the status, stack, registers, memory images, logs and other information of the system or application.

In today's era of rapid technological development, programming languages are springing up like mushrooms after a rain. One of the languages that has attracted much attention is the Go language, which is loved by many developers for its simplicity, efficiency, concurrency safety and other features. The Go language is known for its strong ecosystem with many excellent open source projects. This article will introduce five selected Go language open source projects and lead readers to explore the world of Go language open source projects. KubernetesKubernetes is an open source container orchestration engine for automated

"Go Language Development Essentials: 5 Popular Framework Recommendations" As a fast and efficient programming language, Go language is favored by more and more developers. In order to improve development efficiency and optimize code structure, many developers choose to use frameworks to quickly build applications. In the world of Go language, there are many excellent frameworks to choose from. This article will introduce 5 popular Go language frameworks and provide specific code examples to help readers better understand and use these frameworks. 1.GinGin is a lightweight web framework with fast

This article brings you relevant knowledge about JavaScript. It mainly introduces the differences between var, let and const, as well as the relationship between ECMAScript and JavaScript. Interested friends can take a look at it. I hope Helpful to everyone.

Laravel is a popular PHP framework that is highly scalable and efficient. It provides many powerful tools and libraries that allow developers to quickly build high-quality web applications. Among them, LaravelEcho and Pusher are two very important tools through which WebSockets communication can be easily implemented. This article will detail how to use these two tools in Laravel applications. What are WebSockets? WebSockets

Detailed explanation of the role and usage of the echo keyword in PHP PHP is a widely used server-side scripting language, which is widely used in web development. The echo keyword is a method used to output content in PHP. This article will introduce in detail the function and use of the echo keyword. Function: The main function of the echo keyword is to output content to the browser. In web development, we need to dynamically present data to the front-end page. At this time, we can use the echo keyword to output the data to the page. e

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

With the development of the Internet and the advancement of information technology, the era of big data has arrived, and fields such as data analysis and machine learning have also been widely used. In these fields, task scheduling is an inevitable problem. How to achieve efficient task scheduling is crucial to improving efficiency. In this article, we will introduce how to use Golang's web framework Echo framework to implement distributed task scheduling. 1. Introduction to the Echo framework Echo is a high-performance, scalable, lightweight GoWeb framework. It is based on HTTP
