1.PHP syntax
- The PHP script starts with <?php, ends with?>, and each line of code must end with a semicolon.
- PHP files usually contain HTML tags and some php script code, the default file name extension is ". PHP".
- Save the PHP file under the project folder and enter "localhost/project folder name/php file name" in the address bar of the browser to execute.
- There are two basic output modes in PHP: Echo and print. The difference is that echo can output one or more strings, no return value, and print allows only one string to be output, with a return value of 1. The echo output is faster than print. 2.PHP variables
- The variable starts with the $ sign, followed by the name of the variable
- Variable names must start with a letter or underscore character and can contain only alphanumeric characters and underscores (A-Z, 0-9, and _), cannot contain spaces, and are case-sensitive
- There is no need to declare variables, which are created the first time a variable is assigned.
- Variables defined outside of all functions, with global scope. In addition to functions, global variables can be accessed by any part of the script, to access a global variable in a function, and to use the Global keyword.
- PHP stores all global variables in an array called $GLOBALS [index]. Index holds the name of the variable. This array can be accessed inside the function, or it can be used to update global variables directly. Example code:
<?php$num_1=5;$num_2=3;function Add(){ global $num_1,$num_2; $num_1=$num_1+$num_2;}function Subtract(){ $GLOBALS[‘num_2‘]=$GLOBALS[‘num_1‘]-$GLOBALS[‘num_2‘];}Add();Subtract();echo $num_1; //输出变量之和echo "<br>"; //换行标签echo $num_2; //输出变量之差?>
- Variables declared inside a function are local variables and can only be accessed inside the function. When a function is complete, all its variables are usually deleted. Calling the static keyword guarantees that a local variable is not deleted and retains the value of the variable the first time the function was invoked.
3.PHP Data types
String (String), Integer (integer), float (floating-point), Boolean (Boolean), Array (array), Object (objects), null (null value).
Integral type:
Integers must have at least one number (0-9), cannot contain commas or spaces, no decimal points, and can be positive or negative numbers;
Integer types can be specified in three formats: decimal, hexadecimal (prefixed with 0x), or octal (prefixed with 0);
The Var_dump () function returns the data type and value of the variable:
Declare the class object in PHP using the class keyword, then define the data type in the class, and then use the data type in the instantiated class: The keyword This is a pointer to the current object instance and does not point to any other object or class.
Constant:
To set constants, use the Define () function with the following function syntax:
BOOL Define (String $name, mixed $value [, bool $case _insensitive = false])
The function has three parameters:
Name: Required parameter, constant name, or marker.
Value: A required parameter, a constant.
Case_insensitive: Optional parameter, if set to TRUE, the constant is case insensitive. The default is case-sensitive.
Array:
In PHP, the array() function is used to create arrays:
There are three types of arrays:
(1) Array of values-an array with a numeric ID key
<?php$cars=array("Volvo","BMW","Toyota");echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";?>
(2) Associative array-an array with the specified key, with each key associated with a value
<?php$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");echo "Peter is " . $age[‘Peter‘] . " years old.";?>
(3) Multidimensional arrays-an array containing one or more arrays
Traversing and printing all values in a numeric array, you can use a for loop, traverse and print all values in the associative array, use the Foreach Loop
<?php$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");foreach($age as $x=>$x_value){ echo "Key=" . $x . ", Value=" . $x_value; echo "<br>";}?>
The count () function returns the length of the array (number of elements):
Sort function:
Sort ()-ascending order of arrays
Rsort ()-Descending order of the array
Asort ()-Sets the array in ascending order based on the values of the associative arrays
Ksort ()-an array is sorted in ascending order based on the keys of the associative arrays
Arsort ()-sorts arrays in descending order based on the values of the associative array
Krsort ()-Sorts the arrays in descending order based on the keys of the associative array
PHP Basics: syntax, variables, and data types