One of the PHP study notes

  • 2020-03-31 21:33:26
  • OfStack

The process of configuring PHP and MySQL is omitted. If you are under Ubuntu, refer to the Ubuntu 10.04 Installation Memo.

1. Basic grammar

The way to embed PHP scripts in HTML code is in < ? PHP? > Write code in. Output data to the browser using the echo or print function. Echo can accept multiple parameters, and print can accept only one. Echo takes the form of
Void echo (string arg1, [,... string argn]);
PHP syntax allows you to omit parentheses. For example,
 
<?php 
$my =' my '; 
echo 'Hello',$my,'world' 
?> 

Hello my world will be printed in the browser
PHP also supports a printf function similar to C, such as printf(' %d apples',100), which will output 100 apples. Sprintf is used in the same way as printf, but instead of output to the browser, it returns a string.
2. Data types and variables
PHP is weakly typed, and a variable does not need to be predeclared or typed. Variables in PHP are $plus the name of the variable. Variables in PHP are case-sensitive. For example, $my='my' in the example above.
PHP supports Boolean, integer, floating point, string, array, and object types. The first four are commonly used and are similar to other languages, not introduced much. Arrays and objects are described later.
There are functions in PHP that detect the type of an object, which is getttype. Gettype returns a string, the value of it can be an array, Boolean, double, integer, object, resource, string and unknow the PHP also support explicit type conversion, syntax similar to C.
Conversion operator convert (array) An array of (bool) (Boolean) The Boolean (int) (integer) The integer (object) object (float), (double), and (real) Floating point Numbers (string) String such as:
 
<?php 
$str ='a string'; 
$num=15; 
$numstr='123.3'; 
echo gettype($str),'<br/>'; 
echo gettype($num),'<br/>'; 
echo gettype($numstr),'<br/>'; 
$numstr=(float)$numstr; 
echo gettype($numstr); 
?> 

The output result is:

The string
The integer
The string
A double

There are also functions that can be used to determine whether a variable is of a type, such as is_array(),is_bool(), and so on.

3. Scope of functions and variables
PHP's method of declaring functions is simple, as follows:
 
function functionName(parameters){ 
function body 
} 

You don't need to specify a return type, and you don't need to specify a variable type in parentheses, as long as you have a variable name. Such as:
 
<?php 
function taxedPrice($price,$taxrate){ 
return $price*(1+$taxrate); 
} 
echo taxedPrice(100, 0.03); 
?> 

By default, PHP passes parameters by value. Changing the value of parameters in a function does not change the value of variables outside the function, but PHP also supports passing by reference, syntax is consistent with C, &$paramName, for example, the following is a classic example:
 
<?php 
function swap1($x,$y){ 
$t=$x;$x=$y;$y=$t; 
} 
function swap2(&$x,&$y){ 
$t=$x;$x=$y;$y=$t; 
} 
$a=3;$b=5; 
swap1($a,$b); 
printf("a is %d, b is %d <br/>",$a,$b); 
swap2($a,$b); 
printf("a is %d, b is %d <br/>",$a,$b); 
?> 

Output results:

A is 3, b is 5
A is 5, b is 3

PHP's functions also support default values for arguments, and the syntax and C are the same. Such as:
 
<?php 
function taxedPrice($price,$taxrate=0.03){ 
return $price*(1+$taxrate); 
} 
echo taxedPrice(100); 
?> 

The scope of a variable is described below. The scope of variables in PHP is very similar to that in C, with local variables, function parameters, global variables, and static variables. A local variable is a variable declared in a function, and a function parameter is a variable declared at the beginning of the function. Variables that are not declared in a function are GLOBAL variables, which can be accessed from anywhere, but unlike C, if you want to change the value of a GLOBAL variable in a function you need to explicitly specify it as a GLOBAL variable using the GLOBAL keyword, otherwise PHP will declare a local variable with the same name and override it. Such as:
 
<?php 
$taxrate=0.03; //global 
function change1() { 
$taxrate+=1; 
} 
function change2() { 
GLOBAL $taxrate; 
$taxrate+=1; 
} 
change1(); 
echo $taxrate,'<br/>'; 
change2(); 
echo $taxrate,'<br/>'; 
?> 

The output result is:

0.03

1.03

PHP also has a super global variable. Super global variables are predefined by the PHP system and are mainly used to access environment-related information, such as the current user session, the user operating environment, and the local environment. A super global variable is an array, such as $_SERVER, that stores server-related information. $_GET,$_POST,$_FILES, and $_COOKIE respectively store the information submitted by clients by get and post, as well as the uploaded files and cookie information. These variables are easy to use, what information do you need just look up

4 variables of variables
Unlike static languages like C, PHP's variable name can be a variable itself, which is handy when you need to generate many variables dynamically. Such as:
 
<?php 
$r="hello"; 
$$r="I am hello"; 
echo $hello; 
?> 

The output is: I am hello

5. Flow control statement
Basically,if, else, while,for, do while,switch. All of these are very similar to C. Not much introduction. In PHP, elseif is a keyword that is linked together, while C is elseif.

Related articles: