PHP Learning Output string (ECHO,PRINT,PRINTF,PRINTR and Vardump)

Source: Internet
Author: User
Tags constant definition php programming scalar variable scope

Here's a description.
1. Echo
echo is a keyword in PHP that has no return value. In the notation, it can omit parentheses. The following code:
Copy the Code code as follows:
Echo ' Test String ';
Echo (' Test String ');
2. Print
Print is also a keyword in PHP, it has a return value, generally returns true, the case should not return false. As with Echo, it is possible to omit the parentheses in the notation. The following code:
Copy the Code code as follows:
print ' Test String ';
Print (' Test String ');
3. printf
printf can format the output of a string like the C-language printf. It has the same format as the C language and is preceded by%. Its descriptor is defined as follows.
The b parameter is an integer that shows the binary
The c parameter is an integer that displays the corresponding ASCII character
The d parameter is an integer that displays its decimal
The f parameter is double and is displayed as a floating-point number
The e parameter is double precision and is shown as scientific counting type
The G parameter is double, displayed as a floating-point number or scientific counting type
The o parameter is an integer that shows its octal
The s parameter is a string, displayed as a string
The U parameter is an unsigned integer that displays its decimal
The x/x parameter is an integer that displays its hexadecimal (case-insensitive)
% output% to illustrate:
F,e default six digits after the decimal point, g in more than six digits (plus decimal), will be rounded, if the value after rounding is less than 1000000 will be directly output, greater than 1000000 will be displayed as scientific counting type. f The result of a value greater than 1.2e23 output is incorrect.
In addition to the above, the other can specify the total number of outputs (decimal point, E is one), and you can specify 0 or a space as a complement, you can also specify whether the complement is left or right.
F,e can specify the number of digits after the decimal point.
Such as%5d indicates that the total number of output is 5, insufficient left to fill the space,%05d indicates that the total output number is 5, insufficient left 0,%05.1f indicates the total output number is 5, insufficient left to fill 0, 1 digits after the decimal point,%-05.1f indicates the total output number is 5, insufficient right to fill 0, 1 digits after the decimal point;
Example code:
Copy the Code code as follows:
printf ("%7.2f", 1.2); "1.20"
printf ("%-07.2f", 1.2); "1.20000"
4. sprintf
sprintf and format conversion are like printf, the difference is that printf is output directly, and sprintf returns a formatted string.
5. Print_r and Var_dump
Both the Print_r and var_dump can output arrays and objects, but the output of the Print_r to the boolean is not obvious; the var_dump output is verbose and much more commonly used for debugging.
The following code:
Copy the Code code as follows:
$v = new test ();
Print_r ($v);
Var_dump ($v);
Class Test {
public $num = 1;
Public $str = "222";
Public $bln = true;
The result is:
Copy the Code code as follows:
Test Object
(
[num] = 1
[str] = 222
[BOOL] = 1
)
Object (Test) #1 (3) {
["Num"]=>
Int (1)
["Str"]=>
String (3) "222"
["BOOL"]=>
BOOL (TRUE)
}
Resources:
PHP Programming, 2003, fourth string, output string PHP learning data types between the conversion code Copy the Code code as follows:


/* Data types convert to and from each other
* One is a cast
* SETTYPE (variable, type); Int,integer,float,double and so on.
* This function changes the type of the original variable, using Var_dump (); You can view the variable
*
* Used in the form of (type) before assignment, does not change the type of the original variable
* $a = (int) "123ABC";
*
* $ variable =intval (variable or value);
* $ variable =floatval (variable or value);
* $ variable =stringval (variable or value);
*
* Note: integer type is 4 bytes in memory 2.147e9
* Floating-point type 8 bytes in memory
*
*
* One is automatic conversion (the most common way), the variable will be more run environment automatic conversion

* Some common functions related to variables and types
* Isset ();//Determine if a variable exists, and the value if NULL also indicates null.
* Empty ();//Determine if a variable is null "", NULL
* unset ();//Delete a variable
* SetType ();//Set a variable type
* GetType ();//Get a variable type var_dump (); Get type and value
*
* Variable type test function
* Is_bool ();//Whether the Boolean type is determined
* Is_int () Is_integer () Is_long ()//Determine if the integer type
* Is_float (), is_double () is_real ()//...
* Is_array ()
* Is_object ()
* Is_resource ()
* Is_null ()
* Is_scalar ()//Determine if it is a scalar
* Is_numberic ()//judge whether it is any kind of number, or numeric string
* Is_callable ()//Determine if the function name is valid

* Declaration and use of constants
* 1. Constant is an identifier for a simple value
* 2. The constant definition cannot change its value, nor can it use unset () or any other function to cancel
* 3. Constants can be defined and accessed anywhere by ignoring the rules of variable scope
* 4. Constants use define ("Constant name", value);
* 5. The constant name does not use "$" when declaring and using
* 6. Constant names are used in uppercase
* 7. The value of a constant can only be in scalar type (int,float,bool,string)
* 8. Constants must be given values at the time of declaration
* 9.defined ("constant");//Determine if the constant exists
*
* Predefined Constants and Magic constants
* Echo __file__;//output Current file name Directory _ Magic constant
* echo case_lower;//Output Fixed value _ pre-defined constant
*

*/
This function changes the type of the original variable, using Var_dump (); You can view the variable
$str = "100.12345ABC";
SetType ($str, int);
Var_dump ($STR);

Used in the form of (type) before assignment
$str = "100.12345ABC";
$a = (int) $str;
Var_dump ($a);//output int (100)
Var_dump ($STR);//output value unchanged, "100.12345ABC"
If the string does not start with a number, it is converted to 0

Different types of operations
$a = 10;
$b = "100ABC";
$c =true;
$d = 12.34;
$sum = $a + $c;//Boolean auto-Convert to 1, result 11
$sum = $a + $b;//result is 110
$sum = $a + $b + $c;//result is 111
$sum = $a + $b + $c + $d;//The result is 123.34 because the floating-point memory space is large and small memory is turned into large memory.

Determines whether an array is
$a =array ("one", "one", "1,3,6,8");
if (Is_array ($a)) {
Print_r ($a);//print array
}else{
echo $a;
}

Defining constants, using constants
Define ("Home", "This is a Home");
$a = 100;
Function Demo ()
{
Global $a;//Because $ A is a global variable, it is called with the GLOBALS tag
echo $a;
Echo home;//constants can be ignored, accessed directly or defined
}
Demo ();

Determine if a constant exists
if (Defined ("Home")
{
Echo home;
}
Else
{
Define ("Home", "This is a Home");
}

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.