Table of Contents
PHP基础温习之echo print printf sprintf print_r var_dump的用法与区别,print_rvar_dump
Home Backend Development PHP Tutorial Basic PHP review: echo print printf sprintf print_r var_dump usage and difference, print_rvar_dump_PHP tutorial

Basic PHP review: echo print printf sprintf print_r var_dump usage and difference, print_rvar_dump_PHP tutorial

Jul 13, 2016 am 09:48 AM
echo

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

一、echo
echo() 实际上不是一个函数,是php语句,因此您无需对其使用括号。不过,如果您希望向 echo() 传递一个以上的参数,那么使用括号会发生解析错误。而且echo是返回void的,并不返回值,所以不能使用它来赋值。
例子:
复制代码代码如下:
<?<span>php  
</span><span>$a</span> = <span>echo</span>("55nav"); <span>//</span><span> 错误!不能用来赋值  </span>
<span>echo</span> "55nav"; <span>//</span><span> 55nav   </span>
<span>echo</span> ("55nav"); <span>//</span><span> 55nav   </span>
<span>echo</span> ("55nav","com"); <span>//</span><span>发生错误,有括号不能传递多个参数   </span>
<span>echo</span> "55nav"," com"," is", " web";  <span>//</span><span> 不用括号的时候可以用逗号隔开多个值, 会输出 55nav com is web   </span>
<span>echo</span> "55nav is  8 good  9 web.";  <span>//</span><span> 不管是否换行,最终显示都是为一行 55nav is good web.  </span>
<span>$fistname</span>="55nav"<span>; 
</span><span>echo</span> "<span>$fistname</span> com"; <span>//</span><span> 如果 $firstname = "55nav", 则会输出 55nav com.  </span>
<span>echo</span> '$firstname com'; <span>//</span><span> 由于使用单引号,所以不会输出$firstname的值,而是输出 $firstname com </span>
?> 
Copy after login


二、print
print() 和 echo() 用法一样,但是echo速度会比print快一点点。实际上它也不是一个函数,因此您无需对其使用括号。不过,如果您希望向print() 传递一个以上的参数,那么使用括号会发生解析错误。注意print总是返回1的,这个和echo不一样,也就是可以使用print来赋值,不过没有实际意义。
例子:
复制代码代码如下:
<?<span>php 
</span><span>$a</span> = <span>print</span>("55nav"); <span>//</span><span> 这个是允许的  </span>
<span>echo</span> <span>$a</span>; <span>//</span><span> $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>php 
 </span><span>$a</span>="55nav"<span>; 
 </span><span>$c</span> = <span>print_r</span>(<span>$a</span><span>);  
 </span><span>echo</span> <span>$c</span>;  <span>//</span><span> $c的值是TRUE  </span>
 <span>$c</span> = <span>print_r</span>(<span>$a</span>,<span>true</span><span>);  
 </span><span>echo</span> <span>$c</span>; <span>//</span><span> $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>php 
</span><span>printf</span>("My name is %s %s。","55nav", "com"); <span>//</span><span> My name is 55nav com。 </span>
<span>printf</span>("My name is %1\$s %1\$s","55nav", "com"); <span>//</span><span> 在s前添加1\$或2\$.....表示后面的参数显示的位置,此行输出 My name is 55nav 55nav因为只显示第一个参数两次。 </span>
<span>printf</span>("My name is %2\$s %1\$s","55nav", "com"); <span>//</span><span> My name is com 55nav  </span>
?> 
Copy after login


五、sprintf函数
此函数使用方法和printf一样,唯一不同的就是该函数把格式化的字符串写写入一个变量中,而不是输出来。
例子:
复制代码代码如下:
<?<span>php 
</span><span>sprintf</span>("My name is %1\$s %1\$s","55nav", "com");  <span>//</span><span>你会发现没有任何东西输出的。  </span>
<span>$out</span> = <span>sprintf</span>("My name is %1\$s %2\$s","55nav", "com"<span>);  
</span><span>echo</span> <span>$out</span>;  <span>//</span><span>输出 My name is 55nav com  </span>
?> 
Copy after login


六、var_dump函数
功能: 输出变量的内容、类型或字符串的内容、类型、长度。常用来调试。
复制代码代码如下:
<?<span>php 
</span><span>$a</span>=100<span>; 
</span><span>var_dump</span>(<span>$a</span>); <span>//</span><span>int(100) </span>
<span>$a</span>=100.356<span>; 
</span><span>var_dump</span>(<span>$a</span>); <span>//</span><span>float(100.356) </span>
?> 
Copy after login

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1022596.htmlTechArticlePHP基础温习之echo print printf sprintf print_r var_dump的用法与区别,print_rvar_dump 一、echo echo() 实际上不是一个函数,是php语句,因此您无需对其使...
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)

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

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

What are the most popular golang frameworks on the market? What are the most popular golang frameworks on the market? Jun 01, 2024 pm 08:05 PM

The most popular Go frameworks at present are: Gin: lightweight, high-performance web framework, simple and easy to use. Echo: A fast, highly customizable web framework that provides high-performance routing and middleware. GorillaMux: A fast and flexible multiplexer that provides advanced routing configuration options. Fiber: A performance-optimized, high-performance web framework that handles high concurrent requests. Martini: A modular web framework with object-oriented design that provides a rich feature set.

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

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:

Let's explore, what are the commonly used frameworks in Go language? Let's explore, what are the commonly used frameworks in Go language? Feb 18, 2024 pm 10:53 PM

As an efficient and modern programming language, Go language is widely favored by developers. In the Go language, there are many excellent frameworks that can help developers quickly build efficient and stable applications. This article will introduce some popular frameworks in the Go language and provide some specific code examples. Gin framework Gin is a very popular Goweb framework that provides the ability to quickly develop APIs. The Gin framework has the characteristics of high performance, ease of use, and lightweight, and is widely used in back-end development of Internet companies.

See all articles