Home php教程 php手册 php中echo、print、print_r、printf、sprintf、var_dump用法介绍

php中echo、print、print_r、printf、sprintf、var_dump用法介绍

Jun 13, 2016 am 10:15 AM
dump echo php print printf var introduce article usage

文章介绍了echo、print、print_r、printf、sprintf、var_dump,有需要了解的朋友可参考一下。

一、echo
定义和用法
PHP echo() 函数输出一个或多个字符串。

echo "" 这种方法也可以,不需要括号都行

语法
echo(strings)
参数 描述
strings 必需。一个或多个要发送到输出的字符串。

提示和注释
  注释:echo() 实际上不是一个函数,因此您无需对其使用括号。不过,如果您希望向 echo() 传递一个或多个参数,那么使用括号会发生解析错误。

提示:echo() 函数比 print() 函数快一点点。

提示:echo() 函数可以使用简化语法。参见例子 5。

 代码如下 复制代码

例子
例子 1
$str = "Who's John Adams?";
echo $str;
echo "
";
echo $str."
I don't know!";
?>

  输出:

Who's John Adam?
Who's John Adam?
I don't know!

例子 2
echo "This text spans multiple lines.";
?>

  输出:

This text spans multiple lines.

例子 3
echo 'This ','string ','was ','made ','with multiple parameters';
?>

  输出:

This string was made with multiple parameters

例子 4
  单引号和双引号的不同之处。单引号仅输出变量名,而不是值:

$color = "red";
echo "Roses are $color";echo "
";
echo 'Roses are $color';?>

  输出:

Roses are red Roses are $color

例子 5
  简化语法:

$color = "red";
?>

Roses are =$color?>

  


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

 代码如下 复制代码
 $a = print("55nav"); // 这个是允许的
 echo $a; // $a的值是1
?>

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

 代码如下 复制代码

 $a="55nav";
 $c = print_r($a);
 echo $c;  // $c的值是TRUE
 $c = print_r($a, ture);
 echo $c; // $c的值是字符串55nav
 ?>

四、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 参数,则您必须使用占位符。占位符被插入 % 符号之后,由数字和 “$” 组成。可使用数字指定显示的参数,详情请看例子。
例子:

 代码如下 复制代码

 printf("My name is %s %s。","55nav", "com"); // My name is 55nav com。
 printf("My name is %1$s %1$s","55nav", "com"); // 在s前添加1$或2$.....表示后面的参数显示的位置,此行输出 My name is Ricky Ricky因为只显示第一个参数两次。
 printf("My name is %2$s %1$s","55nav", "com"); // My name is com 55nav
 ?>

五、function/43020.htm target=_blank >sprintf函数
     参数 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, ++ 等参数将插入到主字符串中的百分号 (%) 符号处。该函数是逐步执行的。在第一个 % 符号中,插入 arg1,在第二个 % 符号处,插入 arg2,依此类推。

提示和注释
  注释:如果 % 符号多于 arg 参数,则您必须使用占位符。占位符插到 % 符号后面,由数字和 "$" 组成。请参见例子 3。

  提示: 相关函数: fprintf()、 printf()、 vfprintf()、 vprintf() 以及 vsprintf()。

 代码如下 复制代码

例子
例子 1
$str = "Hello";
$number = 123;
$txt = sprintf("%s world. Day number %u",$str,$number);
echo $txt;
?>

输出:

Hello world. Day number 123

例子 2
$number = 123;
$txt = sprintf("%f",$number);
echo $txt;
?>

输出:

123.000000

例子 3
$number = 123;
$txt = sprintf("With 2 decimals: %1$.2f
With no decimals: %1$u",$number);
echo $txt;
?>

输出:

With 2 decimals: 123.00 With no decimals: 123

PHP String 函数


六、var_dump函数
var_dump   (PHP 3 >= 3.0.5, PHP 4, PHP 5)   

var_dump -- 打印变量的相关信息

  void var_dump ( mixed expression [, mixed expression [, ...]] )   

此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。   

提示: 为了防止程序直接将结果输出到浏览器,可以使用输出控制函数(output-control functions)来捕获此函数的输出,并把它们保存到一个例如 string 类型的变量中。   


可以比较一下 var_dump() 与 print_r()。


例子

 代码如下 复制代码


  

<p>  <?php </p>
</p><p>  $a = array (1, 2, array ("a", "b", "c"));</p>
<p>  var_dump ($a);</p>
<p>  /* 输出:</p>
<p>  array(3) {</p>
<p>  [0]=></p>
<p>  int(1)</p>
<p>  [1]=></p>
<p>  int(2)</p>
<p>  [2]=></p>
<p>  array(3) {</p>
<p>  [0]=></p>
<p>  string(1) "a"</p>
<p>  [1]=></p>
<p>  string(1) "b"</p>
<p>  [2]=></p>
<p>  string(1) "c"</p>
<p>  }</p>
<p>  }</p>
<p>  */</p>
<p>  $b = 3.1;</p>
<p>  $c = TRUE;</p>
<p>  var_dump($b,$c);</p>
<p>  /* 输出:</p>
<p>  float(3.1)</p>
<p>  bool(true)</p>
<p>  */</p>
<p>  ?></p>
<p>  </p>
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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles