I. Several styles of embedding PHP code in Web pages
Recommended for use in standard or short style
Copy CodeThe code is as follows:
Standard Style
Echo ' Hello world! ';
?>
Short style
Echo ' Hello world! ';
?>
two. Four ways to code comments
Copy CodeThe code is as follows:
Single-line Comment
/*
* Multi-line Comment
*/
#shell风格注释
/**
* Phpdoc Style Notes
*/
?>
three. Several ways to output strings to the browser
Copy CodeThe code is as follows:
/*
* Function of Echo function: output string to Browser
* Function return value: void
*/
Echo ' echo function! ';
Echo ('
');
/*
* Function of Echo function: output string to Browser
* Function return value: int
*/
print ' Print function ';
Echo ('
');
echo print ' echo value of print function. ';
Echo ('
');
/*
* Function of printf function: output string to Browser
* Function return Value: The length of the printed string
*/
printf ("A weekend has%d days", 7);
Echo ('
');
echo printf ("A weekend has%d days", 7);
Echo ('
');
/*
* Function of the sprintf function: Save the string in memory
* Function return Value: The saved string itself
*/
sprintf (' sprintf function ');
Echo ('
');
echo sprintf (' sprintf function ');
Echo ('
');
?>
Output Result:
echo function test!
Print function test.
Print function test. 1
A weekend has 7 days
A weekend has 7 days. 23
sprintf function test
Common Type Indicator
|
Type |
Describe |
|
%b |
Integer, displayed as binary |
|
%c |
integer, shown as ASCII character |
|
%d |
Integer, displayed as a signed decimal number |
|
%f |
Floating-point numbers, displayed as floating-point numbers |
|
%o |
Integer, shown as octal number |
|
%s |
String, displayed as a string |
|
%u |
Integer, displayed as unsigned decimal number |
|
%x |
Integer that shows the hexadecimal number as lowercase |
|
%x |
Integer, hexadecimal number shown as uppercase |
four. Identifiers and variables
1. Basic rules for identifiers:
1) An identifier can be any length and can consist of any letter, number, or underscore.
2) identifiers cannot begin with a number.
3) in PHP, identifiers are case-sensitive.
4) A variable name can be the same as a function name.
2. Variable assignment:
Copy CodeThe code is as follows:
$sum = 0;
$total = 1.22;
$sum = $total;
Echo $sum; 1.22
?>
3. The data type of the variable:
Basic data types
|
Type |
Name |
|
Integer |
Integer |
|
Float |
Single-precision floating-point number |
|
Double |
Precision floating point number |
|
String |
String |
|
Boolean |
Boolean |
|
Array |
Array |
|
Object |
Object |
4. Type strength
PHP is a dynamic language, is a very weak type of language, when the program runs, you can dynamically change the type of variables.
5. Type conversion:
Implicitly-Typed conversions:
Copy CodeThe code is as follows:
$sum = 0;
$total = 1.22;
$sum = $total;
Echo GetType ($sum);//double
?>
An explicit type conversion:
Copy CodeThe code is as follows:
$sum = 100;
$total = (string) $sum;
Echo GetType ($sum);//string
?>
Using the Settype () function for type conversion, the return value of 1 indicates success and NULL indicates failure.
Copy CodeThe code is as follows:
$sum = 58;
Echo Settype ($sum, "float");
Echo $sum; 58
Echo GetType ($sum); Double
?>
6. Functions for detecting variables:
|
Function |
Function |
return value |
|
Gettype () |
Gets the type of the variable |
One of the basic data types |
|
Settype () |
Set the type of the variable |
Bool (1:true 0:false (or ")) |
|
Isset () |
Used to determine whether a variable exists |
Bool |
|
Unset () |
Releases the given variable |
Void |
|
Empty () |
Detects whether the value of a variable is empty |
Bool |
|
Is_int () Is_integer () |
Detects if a variable is an integer |
Bool |
|
Is_string () |
Detects if a variable is a string |
bool |
|
Is_numeric |
Detects if a variable is a numeric or numeric string |
bool |
|
Is_null |
detects if the variable is NULL |
bool |
|
Intval () |
Gets the integer value of the variable |
Int |
Basic use of Isset ()
Copy CodeThe code is as follows:
$a = 10;
echo isset ($a);//1
?>
echo isset ($b);//'
?>
Basic use of Usset ()
Copy CodeThe code is as follows:
$a = 10;
unset ($a);
echo isset ($a);//'
?>
Basic use of Empty ()
Copy CodeThe code is as follows:
$a = 5;
$b = 1;
$c = 0;
$d = "";
$e = Array ();
$f = null;
$g = "0";
$h = false;
echo empty ($a);//' (FALSE)
Echo '
';
echo Empty ($b);//' (FALSE)
Echo '
';
echo Empty ($c);//1 (True)
Echo '
';
echo Empty ($d);//1 (True)
Echo '
';
echo Empty ($e);//1 (True)
Echo '
';
echo Empty ($f);//1 (True)
Echo '
';
echo Empty ($g);//1 (True)
Echo '
';
echo Empty ($h);//1 (True)
Echo '
';
echo Empty ($f);//1 (True)
?>
Basic use of is_int (). Similar functions are: Is_float (), is_double (), is_string (), Is_bool (), Is_array (), Is_null (), Is_long (), Is_object (), Is_resource (), Is_numeric (), is_real () and so on.
Copy CodeThe code is as follows:
$a = 11;
$b = 1.23;
$c = 3.1415926;
$d = "Hello";
$e = false;
$f = Array ();
$g = null;
echo is_int ($a);//1
Echo '
';
echo is_float ($b);//1
Echo '
';
echo is_double ($c);//1
Echo '
';
echo is_string ($d);//1
Echo '
';
echo Is_bool ($e);//1
Echo '
';
echo Is_array ($f);//1
Echo '
';
echo Is_null ($g);//1
Echo '
';
echo is_numeric ($a);//1
?>
Basic use of the intval () function. Similar functions are: Floatval (), Strval ()
Copy CodeThe code is as follows:
$a = 22.23;
Echo GetType ($a);//double
Echo '
';
$b = intval ($a);//Type after conversion does not change the original type of $ A
Echo GetType ($a);//double
Echo '
';
?>
$a = 22.23;
Echo GetType ($a);//double
Echo '
';
Settype ($a, "integer");//Type conversion will change $AA original type
Echo GetType ($a);//integer
Echo '
';
?>
7. Scope of variables
Super Global variables
|
Variable name |
Role |
|
$GLOBALS |
All global variable arrays |
|
$_server |
Array of server environment variables |
|
$_get |
Array of variables passed to the script by Get method |
|
$_post |
Array of variables passed to the script by post |
|
$_cookie |
Array of cookie variables |
|
$_files |
Array of variables related to file uploads |
|
$_env |
Array of environment variables |
|
$_request |
Array of variables used by the user input |
|
$_session |
Array of Session Variables |
8. Constants
Once defined, it cannot be changed again.
Copy CodeThe code is as follows:
Define ("Total", 100);
Echo total;//100
Echo '
';
Define ("Total", 200);
Echo total;//100
?>
Ways to view the predefined constants of PHP
Copy CodeThe code is as follows:
Phpinfo ();
?>
Methods for referencing PHP predefined constants
Copy CodeThe code is as follows:
echo $_server["SERVER_NAME"];//localhost
Echo '
';
echo $_server["Server_port"];//8090
Echo '
';
echo $_server["Document_root"];//d:/appserv/www
Echo '
';
?>
Five. Accessing form variables
Three common ways
Copy CodeThe code is as follows:
echo $username;//short style, easy to confuse with variable name, not recommended.
Echo '
';
echo $_post[' username '];//medium style, 4.1. Post 0 support, recommended
Echo '
';
echo $HTTP _post_vars[' username '];//lengthy style, obsolete, may be removed in the future
?>
Posttest.html
Copy CodeThe code is as follows:
<title>How to get form data</title>
Six. String connection.
Copy CodeThe code is as follows:
echo "The Student name is:". $_post[' username ');
echo "
";
echo "Welcome to". " School ";
?>
http://www.bkjia.com/PHPjc/324815.html www.bkjia.com true http://www.bkjia.com/PHPjc/324815.html techarticle I. Several styles of embedding PHP code in Web pages recommend using standard style or short style copy code code as follows:? PHP//Standard style echo ' Hello world! ';??//Short style echo ' ...