Home Backend Development PHP Tutorial PHP基础复习之echo print printf sprintf print_r var_dump的用法与区别

PHP基础复习之echo print printf sprintf print_r var_dump的用法与区别

Jun 13, 2016 pm 12:22 PM
com echo print

PHP基础温习之echo print printf sprintf print_r var_dump的用法与区别

一、echo
echo() 实际上不是一个函数,是php语句,因此您无需对其使用括号。不过,如果您希望向 echo() 传递一个以上的参数,那么使用括号会发生解析错误。而且echo是返回void的,并不返回值,所以不能使用它来赋值。
例子:

复制代码代码如下:


<span style="color: #000000;">php  </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">echo</span>("55nav"); <span style="color: #008000;">//</span><span style="color: #008000;"> 错误!不能用来赋值  </span><span style="color: #0000ff;">echo</span> "55nav"; <span style="color: #008000;">//</span><span style="color: #008000;"> 55nav   </span><span style="color: #0000ff;">echo</span> ("55nav"); <span style="color: #008000;">//</span><span style="color: #008000;"> 55nav   </span><span style="color: #0000ff;">echo</span> ("55nav","com"); <span style="color: #008000;">//</span><span style="color: #008000;">发生错误,有括号不能传递多个参数   </span><span style="color: #0000ff;">echo</span> "55nav"," com"," is", " web";  <span style="color: #008000;">//</span><span style="color: #008000;"> 不用括号的时候可以用逗号隔开多个值, 会输出 55nav com is web   </span><span style="color: #0000ff;">echo</span> "55nav is  8 good  9 web.";  <span style="color: #008000;">//</span><span style="color: #008000;"> 不管是否换行,最终显示都是为一行 55nav is good web.  </span><span style="color: #800080;">$fistname</span>="55nav"<span style="color: #000000;">; </span><span style="color: #0000ff;">echo</span> "<span style="color: #800080;">$fistname</span> com"; <span style="color: #008000;">//</span><span style="color: #008000;"> 如果 $firstname = "55nav", 则会输出 55nav com.  </span><span style="color: #0000ff;">echo</span> '$firstname com'; <span style="color: #008000;">//</span><span style="color: #008000;"> 由于使用单引号,所以不会输出$firstname的值,而是输出 $firstname com </span>?> 
Copy after login

 


二、print
print() 和 echo() 用法一样,但是echo速度会比print快一点点。实际上它也不是一个函数,因此您无需对其使用括号。不过,如果您希望向print() 传递一个以上的参数,那么使用括号会发生解析错误。注意print总是返回1的,这个和echo不一样,也就是可以使用print来赋值,不过没有实际意义。
例子:

复制代码代码如下:


<span style="color: #000000;">php </span><span style="color: #800080;">$a</span> = <span style="color: #0000ff;">print</span>("55nav"); <span style="color: #008000;">//</span><span style="color: #008000;"> 这个是允许的  </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$a</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> $a的值是1 </span>?>
Copy after login

 

 
三、print_r 函数
print_r函数打印关于变量的易于理解的信息。
语法:mixed print_r ( mixed $expression [, bool return ] )
如果变量是string , integer or float , 将会直接输出其值,如果变量是一个数组,则会输出一个格式化后的数组,便于阅读,也就是有key和value对应的那种格式。对于object对象类同。print_r有两个参数,第一个是变量,第二个可设为true,如果设为true,则会返回字符串,否则返回布尔值TRUE。
例子:

复制代码代码如下:


<span style="color: #000000;">php  </span><span style="color: #800080;">$a</span>="55nav"<span style="color: #000000;">;  </span><span style="color: #800080;">$c</span> = <span style="color: #008080;">print_r</span>(<span style="color: #800080;">$a</span><span style="color: #000000;">);   </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$c</span>;  <span style="color: #008000;">//</span><span style="color: #008000;"> $c的值是TRUE  </span> <span style="color: #800080;">$c</span> = <span style="color: #008080;">print_r</span>(<span style="color: #800080;">$a</span>,<span style="color: #0000ff;">true</span><span style="color: #000000;">);   </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$c</span>; <span style="color: #008000;">//</span><span style="color: #008000;"> $c的值是字符串55nav  </span> ?> 
Copy after login

 


四、printf函数
printf函数返回一个格式化后的字符串。
语法:printf(format,arg1,arg2,arg++)
参数 format 是转换的格式,以百分比符号 (“%”) 开始到转换字符结束。下面是可能的 format 值:
* %% – 返回百分比符号
* %b – 二进制数
* %c – 依照 ASCII 值的字符
* %d – 带符号十进制数
* %e – 可续计数法(比如 1.5e+3)
* %u – 无符号十进制数
* %f – 浮点数(local settings aware)
* %F – 浮点数(not local settings aware)
* %o – 八进制数
* %s – 字符串
* %x – 十六进制数(小写字母)
* %X – 十六进制数(大写字母)
arg1, arg2, arg++ 等参数将插入到主字符串中的百分号 (%) 符号处。该函数是逐步执行的,在第一个 % 符号中,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。如果 % 符号多于 arg 参数,则您必须使用占位符。占位符被插入 % 符号之后,由数字和 “\$” 组成。可使用数字指定显示的参数,详情请看例子。
例子:

复制代码代码如下:


<span style="color: #000000;">php </span><span style="color: #008080;">printf</span>("My name is %s %s。","55nav", "com"); <span style="color: #008000;">//</span><span style="color: #008000;"> My name is 55nav com。 </span><span style="color: #008080;">printf</span>("My name is %1\$s %1\$s","55nav", "com"); <span style="color: #008000;">//</span><span style="color: #008000;"> 在s前添加1\$或2\$.....表示后面的参数显示的位置,此行输出 My name is 55nav 55nav因为只显示第一个参数两次。 </span><span style="color: #008080;">printf</span>("My name is %2\$s %1\$s","55nav", "com"); <span style="color: #008000;">//</span><span style="color: #008000;"> My name is com 55nav  </span>?> 
Copy after login

 


五、sprintf函数
此函数使用方法和printf一样,唯一不同的就是该函数把格式化的字符串写写入一个变量中,而不是输出来。
例子:

复制代码代码如下:


<span style="color: #000000;">php </span><span style="color: #008080;">sprintf</span>("My name is %1\$s %1\$s","55nav", "com");  <span style="color: #008000;">//</span><span style="color: #008000;">你会发现没有任何东西输出的。  </span><span style="color: #800080;">$out</span> = <span style="color: #008080;">sprintf</span>("My name is %1\$s %2\$s","55nav", "com"<span style="color: #000000;">);  </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$out</span>;  <span style="color: #008000;">//</span><span style="color: #008000;">输出 My name is 55nav com  </span>?> 
Copy after login

 


六、var_dump函数
功能: 输出变量的内容、类型或字符串的内容、类型、长度。常用来调试。

复制代码代码如下:


<span style="color: #000000;">php </span><span style="color: #800080;">$a</span>=100<span style="color: #000000;">; </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$a</span>); <span style="color: #008000;">//</span><span style="color: #008000;">int(100) </span><span style="color: #800080;">$a</span>=100.356<span style="color: #000000;">; </span><span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$a</span>); <span style="color: #008000;">//</span><span style="color: #008000;">float(100.356) </span>?> 
Copy after login

 

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

What is the difference between comcn and com What is the difference between comcn and com May 12, 2023 pm 04:08 PM

The difference between comcn and com: 1. There are differences between comcn and com in terms of meaning, but there is no difference in access speed; 2. comcn is an international domain name and is a global top-level domain name for use by commercial institutions, while cn is a Chinese company domain name , domestic commercial institutions, domestic domain names, only enterprises can register; 3. The search priority is that cn will search for .cn first. After finding the .cn server, the .cn server will then search for .com; 4. cn is provided by cnnic China Internet Center Management, com's management organization is abroad.

Five selected Go language open source projects to take you to explore the technology world Five selected Go language open source projects to take you to explore the technology world Jan 30, 2024 am 09:08 AM

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 Go language development essentials: 5 popular framework recommendations Mar 24, 2024 pm 01:15 PM

&quot;Go Language Development Essentials: 5 Popular Framework Recommendations&quot; 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

Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Laravel development: How to implement WebSockets communication using Laravel Echo and Pusher? Jun 13, 2023 pm 05:01 PM

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

Implementing distributed task scheduling using Golang's web framework Echo framework Implementing distributed task scheduling using Golang's web framework Echo framework Jun 24, 2023 am 11:49 AM

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

Detailed explanation of the role and usage of the echo keyword in PHP Detailed explanation of the role and usage of the echo keyword in PHP Jun 28, 2023 pm 08:12 PM

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

Explore the Go language framework: 5 choices not to be missed! Explore the Go language framework: 5 choices not to be missed! Feb 19, 2024 pm 02:29 PM

As a fast and efficient programming language, Go language has always been favored by programmers. In the Go language ecosystem, frameworks play a vital role in helping developers build applications faster. This article will introduce five Go language frameworks to let you understand their characteristics and usage. 1. Gin framework The Gin framework is a lightweight Web framework with fast and high performance characteristics. Use the Gin framework to quickly build RESTful APIs and web applications. Here is a simple example code:

See all articles