One of PHP introductory study notes_PHP tutorial
1. Basic syntax
The way to embed a PHP script in HTML code is to write the code in . Output data to the browser using the echo or print function. echo can accept multiple parameters, print can only accept one. The form of echo is
void echo(string arg1,[,...string argn]);
PHP syntax allows the omission of parentheses. For example,
$my =' my ';
echo 'Hello',$my,'world'
?>
will output Hello my world on the browser
PHP also supports a and C language A very similar printf function, such as printf(' %d apples',100), will output 100 apples. The usage of sprintf is the same as printf, but instead of outputting to the browser, it returns a string.
2. Data types and variables
PHP is weakly typed. A variable does not need to be declared in advance, nor does it need to specify a type. Variables in PHP are $ plus the variable name. PHP variables are case-sensitive. For example, $my='my' in the above example.
The variable types supported by PHP include: Boolean, integer, floating point, string, array and object. The first four are very commonly used and are similar to other languages, so I won’t introduce them in detail. Arrays and objects will be introduced in detail later.
There are functions in PHP to detect the type of objects, they are getttype. gettype returns a string whose value can be array, boolean, double, integer, object, resource, string and unknown type. PHP also supports explicit type conversion, and the syntax is similar to C. The
conversion operator converts to
(array) array
(bool) (boolean) boolean
(int) (integer) integer
(object) object
( float), (double), (real) floating point number
(string) string
For example:
$str ='a string';
$num=15;
$numstr='123.3';
echo gettype($ str),'
';
echo gettype($num),'
';
echo gettype($numstr),'
';
$numstr=(float)$numstr;
echo gettype($numstr);
?>
The output result is:
string
integer
string
double
There are also functions that can be used to determine whether a variable is of a certain type, such as is_array(), is_bool(), etc. The usage is similar.
3. Function and variable scope
The method of declaring functions in PHP is very simple, in the following form:
function functionName(parameters){
function body
}
No need to specify the return type , there is no need to specify the variable type in the brackets, as long as there is a variable name. For example:
function taxedPrice($price,$taxrate) {
return $price*(1+$taxrate);
}
echo taxedPrice(100, 0.03);
?>
By default, PHP Parameters are passed by value. Changing the value of the parameter within the function will not change the value of the variable outside the function. However, PHP also supports passing by reference. The syntax is consistent with C, &$paramName. For example, the following is a classic example:
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
",$a,$b);
swap2($a,$b);
printf("a is %d, b is %d
?>
Output result:
a is 3, b is 5
a is 5, b is 3
PHP functions also support default values for parameters, and the syntax is the same as C. For example:
function taxedPrice($price,$taxrate= 0.03){
return $price*(1+$taxrate);
}
echo taxedPrice(100);
?>
The scope of variables is introduced below. PHP's variable scope is very similar to C. There are four types: local variables, function parameters, global variables, and static variables. Local variables are variables declared within the function, and function parameters are variables declared at the beginning of the function; variables not declared in the function are global variables, and global variables can be accessed anywhere, but unlike C, if they are in a function To modify the value of a global variable, 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 overwrite it. For example:
$taxrate=0.03; //global
function change1() {
$taxrate+=1;
}
function change2() {
GLOBAL $taxrate;
$taxrate+=1;
}
change1 ();
echo $taxrate,'
';
change2();
echo $taxrate,'
';
?>
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, user operating environment and local environment. The super global variable is an array. For example, $_SERVER stores server-related information. $_GET, $_POST, $_FILES, and $_COOKIE respectively store information submitted by the client using get and post, uploaded files, cookie information, etc. The use of these variables is very simple. If you need any information, you only need to look up
4. The variables of variables
are different from static languages such as C. The variable name of PHP itself can be a Variables, which are very convenient when many variables need to be generated dynamically. For example:
$r="hello";
$$r="I am hello";
echo $hello;
?>
The output result is: I am hello
5. Flow control statements
mainly include, if else, while, for, do while, switch. These are very similar to C language and are basically the same. Not much introduction. There are some differences. PHP's elseif is a keyword that is connected together, while C language is else if.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

Key players in HTTP cache headers include Cache-Control, ETag, and Last-Modified. 1.Cache-Control is used to control caching policies. Example: Cache-Control:max-age=3600,public. 2. ETag verifies resource changes through unique identifiers, example: ETag: "686897696a7c876b7e". 3.Last-Modified indicates the resource's last modification time, example: Last-Modified:Wed,21Oct201507:28:00GMT.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version
Recommended: Win version, supports code prompts!

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version