Learn PHP
- Learn Artifact: PhpStudy a key to set up the PHP environment
grammar:
PHP is a scripting language that can be nested in an HTML page
Nested HTML file:
<!DOCTYPE html> <html> <head> <title>PHP</title> </head> <body> <?php // php代码; ?> </body> </html>
PHP also can be saved to a separate "* .php" file and is accessed but in php file, it must be "<?" At the beginning "?>" End! php code will only be parsed and executed here
- PHP is case sensitive identification
PHP supports three mainstream Notes: double slash "#" sign, multi-line comments
variable:
Variable rules:
- Variables begin with $ symbol
Variable names must begin with a letter or an underscore, numbers, letters, underscores the variable name
PHP process does not create a command variable name, variable assignment process is created
Scope:
local (local): Only allows statements to access their own grammatical structure (internal function declaration)
global (overall): Allows access to the current statement in all the grammatical structure of PHP programs (outside the function declaration)
Access global variables:
global keyword is used within the function to access global variables inside the function to access the global variables must use the global keyword before the visit.
<?php $x = 5 ; $y = 10 ; function myTest(){ global $x , $y ; $y = $x + $y ; } myTest(); echo $y ; ?>
static (static): implementation of the outcome variable, the next round is not reset
Static access:
When variable declaration, add static statement, you can not reset the change in number of visits and the variable variable
<?php function myTest(){ static $x = 0 ; echo $x ; $x ++ ; } myTest(); myTest(); myTest(); ?>
Output:
echo output:
- May output more than one string
<?php
echo '<p>Hello,World!</p>';
?>
print output:
type of data:
String:
- String is a sequence of characters, the text string within the quotation marks belong
Integer:
- No decimal number combinations (including negative)
Float:
- All combinations of numbers is not an integer, and scientific notation
Boolean logic:
- true and false
Array:
A variable value stored in one or more of
Creating an array using the keyword "array"
<?php $cars = array ("HUAWEI","China","GO!")s var_dump($cars);//返回数组的类型和值 ?> // 运行结果 // “array(3) { [0]=> string(6) "HUAWEI" [1]=> string(5) "China" [2]=> string(3) "GO!" }”
Keywords: var_dump Returns size of the array, the data type of each parameter value and other information
Object:
Using the "class" keyword to declare object data types
<?php class Car { var $color ; function Car($color = "green") { $this -> color = $color; } function what_color(){ return $this->color; } } ?>
NULL values:
- Represents a variable to a null value (the value set to null, empty the representative variable values)
Analyzing Data type:
var_dump ():
- Type and value of the variable print
- Syntax: void var_dump (mixed $ expression)
No return value
iS function:
- is_bool (): Boolean value determines whether
- is_float (): determines whether the float
- is_int (): determining whether an integer
- is_numeric (): determines whether numeric
- is_string (): determining whether a string
- is_array (): determining whether an array
- is_object (): whether the object is determined
- is_null (): determines whether null
- is_resource (): determine whether the resource type
isset():
- Check the variable exists
- Returns: Returns true variable exists
empty():
- Check if the argument is null (isset () may only detect the presence or absence)
- Returns: present and non-empty variable returns false (var = null was considered empty)
PHP system constants:
System constants | Explanation |
---|---|
__ FILE __ | PHP file name |
__ LINE __ | PHP program current line number |
PHP_VERSION | PHP version number |
PHP_OS | Operating system name |
TRUE | true |
FALSE | false |
E_ERROR | The most recent error |
E_WARNING | Recent warnings |
E_PARSE | Resolve potential problems with grammar |
E_NOTICE | Unusual error |
Operator:
Arithmetic operators:
// + - * / % ++ --
Arithmetic addition from the mold decrementing
String operators:
String concatenation operator: . (Dot)
Connecting equal: = (equal points)
Assignment operator:
Assignment: =
Less like: - =
Et plus: + =
Multiplication and other: * =
In addition other: / =
I like to take:% =
Comparison operators:
Greater than, less than, greater than or equal, less than equal to, equal to, not equal to
Full equal to: ===
Insufficiency:! ==
Logical Operators:
Logical AND: and &&
Logical OR: or ||
Logic Non-: not!
Ternary operator:
Conditional operator: ? :
Control structures:
if the branch condition:
slightly
Switch conditional statement:
slightly
While cycle:
slightly
do ... while loop:
slightly
for loop:
slightly
Control functions:
略(break/continue)
Delivery values: *
:( assignment pass both different memory addresses)
$a = $b ;
Assignment by reference :( two variables the same address)
$a = &$b ;
Array:
Array type:
- Enumerated array: Index subscript is an integer
- Associative array: string superscript index
- Multidimensional array: array element is an array
Create an array:
Keywords: Array () creates an array
- Identifier:
- $arr[key] = value ;
- $arr[] = value ;
Array functions:
print_r (): print variable information
unset (): delete array element
foreach (): traverse the array elements
array_shift():
- The first cell array was removed and returned as a result
array_pop():
- The last element of the array cell and returns removed
array_unshift():
- In an array of one or more elements prepend
array_push():
- Insertion of one or more elements to the end of the array
array_values():
- Returns an array of all the values and the establishment of a numeric index
count (): calculated value of the number of attributes
array_sum (): calculated value and
array_reverse (): returns an array of reverse
list (): the array element assigned to the variable
<?php
$IntArray = array(2,5,9,13,7,9,21,7,9,24,38) ;
$MAX = $IntArray[0];
$MIN = $IntArray[0];
$MAX_i = $MIN_i = 0;
for ($i=0; $i < count($IntArray) ; $i++) {
if ($MAX < $IntArray[$i]) {
$MAX = $IntArray[$i] ;
$MAX_i = $i ;
}
if ($MIN > $IntArray[$i]) {
$MIN = $IntArray[$i] ;
$MIN_i = $i ;
}
}
echo "Array_MAX:".$MAX."<br />MAX_i=".$MAX_i ;
echo "<br /><br />";
echo "Array_MIN:".$MIN."<br />MIN_i=".$MIN_i ;
echo "<hr />";
// 位置交换
echo "位置交换前:".print_r($IntArray) ;
$M = $IntArray[$MIN_i] ;
$IntArray[$MIN_i] = $IntArray[$MAX_i] ;
$IntArray[$MAX_i] = $M ;
echo "<br />";
echo "位置交换后:".print_r($IntArray) ;
echo "<hr />";
// 反转数组
echo print_r(array_reverse($IntArray));
?>
Superglobals :
Elements / Code | description |
---|---|
$_SERVER['PHP_SELF'] | Returns the file name of the script execution. |
$_SERVER['GATEWAY_INTERFACE'] | Returns the CGI specification used by the server version. |
$_SERVER['SERVER_ADDR'] | Returns the current IP address of the server running the script is located. |
$_SERVER['SERVER_NAME'] | Returns the host name of the server currently running script is located (for example www.w3school.com.cn). |
$_SERVER['SERVER_SOFTWARE'] | Returns a string identifying the server (such as Apache / 2.2.24). |
$_SERVER['SERVER_PROTOCOL'] | Name and version (e.g., "HTTP / 1.0") communications protocol return the requested page. |
$_SERVER['REQUEST_METHOD'] | The method used to access the page return request (e.g. POST). |
$_SERVER['REQUEST_TIME'] | Returns the timestamp of the start request (e.g., 1577687494). |
$_SERVER['QUERY_STRING'] | Returns the query string, if it is to access this page via the query string. |
$_SERVER['HTTP_ACCEPT'] | Returns the current request from the request header. |
$_SERVER['HTTP_ACCEPT_CHARSET'] | Accept_Charset head returns from the current request (e.g. utf-8, ISO-8859-1) |
$_SERVER['HTTP_HOST'] | Host head returns from the current request. |
$_SERVER['HTTP_REFERER'] | Returns the current page full URL (unreliable because not all user agents are supported). |
$_SERVER['HTTPS'] | Whether through secure HTTP protocol query script. |
$_SERVER['REMOTE_ADDR'] | Back to Browse user's IP address of the current page. |
$_SERVER['REMOTE_HOST'] | Returns the host name of the user browsing the current page. |
$_SERVER['REMOTE_PORT'] | Back on the user machine connected to the port number used by the Web server. |
$_SERVER['SCRIPT_FILENAME'] | Returns the absolute path of the currently executing script. |
$_SERVER['SERVER_ADMIN'] | This value indicates the SERVER_ADMIN Apache server configuration file. |
$_SERVER['SERVER_PORT'] | Port Web server. The default value is "80." |
$_SERVER['SERVER_SIGNATURE'] | 返回服务器版本和虚拟主机名。 |
$_SERVER['PATH_TRANSLATED'] | 当前脚本所在文件系统(非文档根目录)的基本路径。 |
$_SERVER['SCRIPT_NAME'] | 返回当前脚本的路径。 |
$_SERVER['SCRIPT_URI'] | 返回当前页面的 URI。 |
函数:
- 关键字:function name() { …… }
函数参数:
值传递参数:
在函数调用的时候,允许传递参数值给函数,函数可以自由使用参数进行操作
引用传递参数:
如果希望函数可以修改参数值,可以使用引用参数进行传递
引用传递,只需要在参数前加上 “&” 符号就可以
$arr = array(1,2,3,4); function addElement(&$arr){ $arr[count($arr)] = 100; print_r($arr); //在函数内输出$arr } addElement($arr); print_r($arr); //在函数外输出
默认值传递:
function hobby($who,$style=‘运动’){ echo “$who 喜欢 $style”; } hobby(“张三”,“唱歌”); hobby(“张三”);
global关键字:
引用函数外部的参数(引用传递性质的参数)
$name = "Mary";//初始化变量 function getName(){ global $name; //引入外部的变量 echo "我的名字叫:$name"; } getName();
内置函数:
- echo语句:输出
- print语句:输出
include语句:包含并运行指定文件
require语句:包含并运行指定文件