? PhpechoHelloWorld; echoThisspansmultiplelines. Thenewlineswillbeoutputaswell; echoThisspans \ nmultiplelines. Thenewlineswillbe \ noutputasw
echo "Hello World";
echo "This spans
multiple lines. The newlines will be
output as well";
echo "This spans\nmultiple lines. The newlines will be\noutput as well.";
echo "Escaping characters is done \"Like this\".";
// You can use variables inside of an echo statement
$foo = "foobar";
$bar = "barbaz";
echo "foo is $foo"; // foo is foobar
// You can also use arrays
$baz = array("value" => "foo");
echo "this is {$baz['value']} !"; // this is foo !
// Using single quotes will print the variable name, not the value
echo 'foo is $foo'; // foo is $foo
// If you are not using any other characters, you can just echo variables
echo $foo; // foobar
echo $foo,$bar; // foobarbarbaz
// Some people prefer passing multiple parameters to echo over concatenation.
echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";
echo <<
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;
// Because echo does not behave like a function, the following code is invalid.
($some_var) ? echo 'true' : echo 'false';
// However, the following examples will work:
($some_var) ? print 'true' : print 'false'; // print is also a construct, but
// it behaves like a function, so
// it may be used in this context.
echo $some_var ? 'true': 'false'; // changing the statement around
?>
================================
PHP die () function
PHP Miscellaneous functions
Definition and usage
The die () function outputs a message and exits the current script.
This is the alias of the exit () function.
Syntax
Die (status) parameter description
Status is required. Specifies the message or status number written before exiting the script. The status number is not written to the output.
Description
If status is a string, the function outputs the string before exiting.
If status is an integer, this value is used as the exit status. The exit status ranges from 0 to 254. Exit status 255 is retained by PHP and will not be used. Status 0 is used to successfully terminate the program.
Tips and comments
Note: If PHP version is greater than or equal to 4.2.0, this parameter is not output when status is an integer.
Example
$ Site = "http://www.w3school.com.cn /";
Fopen ($ site, "r ")
Or die ("Unable to connect to $ site ");
?>
================
Var_dump
(PHP 3> = 3.0.5, PHP 4, PHP 5)
Var_dump -- print information about a variable
Description
Void var_dump (mixed expression [, mixed expression [,...])
This function displays the structure information about one or more expressions, including the expression type and value. The array recursively expands the value and displays its structure through indentation.
Tip: to prevent the program from directly outputting the results to the browser, you can use the output-control function to capture the output of this function, and save them to a variable of the string type, for example.
You can compare var_dump () with print_r ().
Example 1. var_dump () example
$ A = array (1, 2, array ("a", "B", "c "));
Var_dump ($ );
/* Output:
Array (3 ){
[0] =>
Int (1)
[1] =>
Int (2)
[2] =>
Array (3 ){
[0] =>
String (1) ""
[1] =>
String (1) "B"
[2] =>
String (1) "c"
}
}
*/
$ B = 3.1;
$ C = TRUE;
Var_dump ($ B, $ c );
/* Output:
Float (3.1)
Bool (true)
*/
?>
========================
Print_r
(PHP 4, PHP 5)
Print_r -- print easy-to-understand information about variables.
Description
Bool print_r (mixed expression [, bool return])
Note: The return parameter is added in PHP 4.3.0.
Print_r () displays easy-to-understand information about a variable. If the value is string, integer, or float, the variable value is printed. If array is provided, keys and elements are displayed in a certain format. Objects are similar to arrays.
Remember, print_r () will move the array pointer to the last edge. Use reset () to bring the pointer back to the beginning.
$a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x','y','z'));
print_r ($a);
?>
The above code will be output:
Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
To capture the output of print_r (), use the return parameter. If this parameter is set to TRUE, print_r () does not print the result (this is the default action), but returns its output.
Example 1. return parameter example
$ B = array ('M' => 'monkey', 'foo' => 'bar', 'x' => array ('X', 'y ', 'Z '));
$ Results = print_r ($ B, true); // $ results contains the output result of print_r
?>
Note: If you want to capture the output of print_r () in a version earlier than PHP 4.3.0, you can use the output control function.
Note: In versions earlier than PHP 4.0.4, if the given array or object contains a reference pointing directly or indirectly to itself, print_r () will continue forever. Print_r ($ GLOBALS) is an example, because $ GLOBALS itself is a global variable and contains references pointing to itself.
See ob_start (), var_dump (), and var_export ().
======================================
Var_export
(PHP 4> = 4.2.0, PHP 5)
Var_export -- string representation of the output or returned variable
Description
Mixed var_export (mixed expression [, bool return])
This function returns the structure information about the variables passed to this function. it is similar to var_dump (). The difference is that the returned representation is legal PHP code.
You can set the second parameter of the function to TRUE to return the expression of the variable.
Compare var_export () and var_dump ().
$ A = array (1, 2, array ("a", "B", "c "));
Var_export ($ );
/* Output:
Array (
0 => 1,
1 => 2,
2 =>
Array (
0 => 'A ',
1 => 'B ',
2 => 'C ',
),
)
*/
$ B = 3.1;
$ V = var_export ($ B, TRUE );
Echo $ v;
/* Output:
3.1
*/
?>
======================================
The echo () function outputs one or more strings.
Syntax
Echo (strings) parameter description
Strings is required. One or more strings to be sent to the output.
Tips and comments
Note: echo () is actually not a function, so you do not need to use parentheses for it. However, if you want to pass one or more parameters to echo (), a parsing error occurs when brackets are used.
Tip: The echo () function is a little faster than the print () function.
Tip: The echo () function can use the simplified syntax. See example 5.
Example
Example 1
$ Str = "Who's John Adams? ";
Echo $ str;
Echo"
";
Echo $ str ."
I don't know! ";
?> Output:
Who's John Adam?
Who's John Adam?
I don't know! Example 2
Echo "This text
Spans multiple
Lines .";
?> Output:
This text spans multiple lines. Example 3
Echo 'this', 'string', 'was', 'Made ', 'With multiple parameters ';
?> Output:
This string was made with multiple parameters Example 4
The difference between single quotes and double quotes. Single quotes only output variable names, not values:
$ Color = "red ";
Echo "Roses are $ color ";
Echo"
";
Echo 'roses are $ color ';
?> Output:
Roses are red
Roses are $ color Example 5
Simplified syntax:
$ Color = "red ";
?>
Roses are
======================================
The print () function outputs one or more strings.
Syntax
Print (strings) parameter description
Strings is required. One or more strings sent to the output.
Tips and comments
Note: The print () function is not actually a function, so you do not need to use parentheses for it.
Note: The print () function is a little slower than echo ().
Example
Example 1
$ Str = "Who's John Adams? ";
Print $ str;
Print"
";
Print $ str ."
I don't know! ";
?> Output:
Who's John Adams?
Who's John Adams?
I don't know! Example 2
Print "This text
Spans multiple
Lines .";
?> Output:
This text spans multiple lines. Example 3
$ Color = "red ";
Print "Roses are $ color ";
Print"
";
Print 'roses are $ color ';
?> Output:
Roses are red
Roses are $ color