php-output data to the browser (print, Echo, printf, sprintf, etc.)

Source: Internet
Author: User
Tags php website sprintf

1. Print(Language structure)

Pirnt-Output string

int print ( string $arg )

Example:

输出:Hello World!print("Hello World!");等价于:print "Hello World!";

Attention:

    • Print is a language structure and can enclose the argument list without parentheses;
    • Print supports only one parameter ;
    • Pirnt always returns 1
2. Echo(Language structure)

echo-output one or more strings

Description

void echo ( string $arg1 [, string $... ] )

Example:

echo "Hello World!";$a = "a";$b = "b";echo $a,$b; // 输出 abecho "a is $a"; // 输出 a is a

Attention:

    • Echo is a language structure that does not have to use parentheses to indicate parameters, single quotes, and double quotes can
    • Echo accepts the parameter list, which is a number of parameters
    • The parentheses cannot be used when echo passes multiple arguments
    • echo outputs all parameters, no line break
    • Echo has no return value , so efficiency is higher than print
3. printf ()function

printf-Output formatted string
Description

int printf ( string $format [, mixed $args [, mixed $... ]] )

Example:

$num = 2.12; printf("%.1f",$num);  // 输出: 2.1$name = "jack";printf("my name is %s", $name); // 输出:my name is jack

Attention:

    • printf () is a function
    • printf () returns the length of the output string .
4. sprintf ()function

sprintf ()-Output formatted string
Description

string sprintf ( string $format [, mixed $args [, mixed $... ]] )

Example:

$str = "jack";$name = sprintf("my name is %s", $name);echo $name; // 输出:my name is jack

Attention:

    • sprintf () returns the formatted string
    • sprintf () does not print output
    • Format formats See official website: http://php.net/manual/zh/function.sprintf.php
5. Print_r ()function

print_r ()-Print easy-to-understand information about variables .

Description

bool print_r ( mixed $expression [, bool $return ] )

Attention:

    • Print_r () is a function
    • If a string, integer, or float is given, the value of the variable is printed .
    • If an array is given, the keys and elements are displayed in a certain format.
    • object is similar to an array
    • Set the return parameter, Print_r () will not print the result, but return its output

Example:

1、打印变量本身:$name = "michael";print_r($name); // 输出:michael2、打印数组:<?php    echo "<pre>";        $a = array (‘a‘ => ‘apple‘, ‘b‘ => ‘banana‘, ‘c‘ => array (‘x‘,‘y‘,‘z‘));    print_r ($a);        echo "</pre>";?>上述代码输出如下结果:    Array    (        [a] => apple        [b] => banana        [c] => Array            (                [0] => x                [1] => y                [2] => z            )    )



The rest, such as vsprintf () and so on are not summed up, the PHP official website (http://php.net) can be viewed.

View method: If you want to see a function on the PHP website and add the function name, press ENTER.

For example:

查看printf函数http://php.net/printf




php-output data to the browser (print, Echo, printf, sprintf, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.