A summary of PHP basics for beginners
Summary of PHP basic knowledge that is helpful for novices
I just started learning PHP, please give me some advice in the future:
Learning the backend is a long process. I just learned PHP and summarized a small part. Some people may ask why you copy and share W3C stuff?
My answer is: W3C is all about introductory basics, which are very meaningful, and many people don’t want to go to W3C to learn because there are too many things.
Everyone is willing to read blogs or check information to learn, so I share it with some beginners like me to learn. I hope it will be helpful to everyone!
PHP learning syntax:
1. echo ---------Output statement
echo "我的第一段 PHP 脚本!";
2. PHP script starts with
<?php // 此处是 PHP 代码 ?>
3. Example:
<!DOCTYPE html> <html> <body> <h1>我的第一张 PHP 页面</h1> <?php echo "Hello World!"; ?> </body> </html>
PHP statements end with a semicolon (;). The closing tag of a PHP code block also automatically indicates a semicolon (so you don't have to use a semicolon on the last line of a PHP code block).
4. Comments in PHP code will not be read and executed as a program. Its only purpose is to be read by code editors.
PHP有三种注释:(//或者#或者/* */) // 这是单行注释 # 这也是单行注释 /* 这是多行注释块 它横跨了 多行 */
5. In PHP, all user-defined functions, classes and keywords (such as if, else, echo, etc.) are not case-sensitive.
Example:
<!DOCTYPE html> <html> <body> <?php ECHO "Hello World!<br>"; echo "Hello World!<br>"; EcHo "Hello World!<br>"; ?> </body> </html>
6. However, in PHP, all variables are case-sensitive.
Example:
<!DOCTYPE html> <html> <body> <?php $color="red"; echo "My car is " . $color . "<br>"; echo "My house is " . $COLOR . "<br>"; echo "My boat is " . $coLOR . "<br>"; ?> </body> </html>
7. Variables are containers for storing information:
<?php $x=5; $y=6; $z=$x+$y; echo $z; ?>
Description: $x, $y, $z represent three different variables. Finally, output the value of $z
8. PHP variable rules:
Variables start with the $ symbol, followed by the name of the variable
Variable names must begin with a letter or underscore
Variable names cannot begin with numbers
Variable names can only contain alphanumeric characters and underscores (A-z, 0 -9 and _)
Variable names are case-sensitive ($y and $Y are two different variables)
PHP variable names are case-sensitive!
9. $txt="Hello world!";--------If the value you assign to the variable is text, please surround the value with quotation marks.
PHP automatically converts the variable to the correct data type based on its value.
10. The scope of a variable refers to the part of the script where the variable can be referenced/used.
PHP has three different variable scopes:
local (local)
global (global)
static
11. Local and Global Scope
#Variables declared outside the function have Global scope and can only be accessed outside the function.
Variables declared inside a function have LOCAL scope and can only be accessed inside the function.
Example:
<?php $x=5; // 全局作用域 function myTest()-----------实现函数,用于下面的函数调用 { $y=10; // 局部作用域 echo "<p>测试函数内部的变量:</p>"; echo "变量 x 是:$x"; echo "<br>"; echo "变量 y 是:$x"; } ----------大括号里面创建的变量属于局部变量 myTest();----------函数调用 echo "<p>测试函数之外的变量:</p>"; echo "变量 x 是:$x"; echo "<br>";-------换行符 echo "变量 y 是:$x"; ?>
12. The global keyword is used to access global variables within a function.
To do this, use the global keyword in front of the variable (inside the function):
Example:
<?php $x=5; $y=10; function myTest() { global $x,$y; $y=$x+$y; } myTest(); echo $y; // 输出 15 ?>
global $x,$y; ----Equivalent to----- $GLOBALS['y']=$GLOBALS['x'] $GLOBALS['y'];
13. Usually, when the function After completion/execution all variables are deleted. However, sometimes I need to not delete a local variable. Achieving this will require further work.
To accomplish this, use the static keyword when you first declare the variable:
Example:
<?php function myTest() { static $x=0; echo $x; $x++; } myTest(); myTest(); myTest(); ?>
The variable is still a local variable of the function.
14. The difference between echo and print:
echo - can output more than one string
print - can only output one character string, and always returns 1
15. echo command to display strings and variables
Example:
<?php $txt1="Learn PHP"; $txt2="W3School.com.cn"; $cars=array("Volvo","BMW","SAAB"); echo $txt1; echo "<br>"; echo "Study PHP at $txt2"; echo "My car is a {$cars[0]}"; ?>
16.A string in PHP can be any text within quotation marks. You can use single or double quotes to output
Example:
<?php $x = "Hello world!"; echo $x; echo "<br>"; $x = 'Hello world!'; echo $x; ?>
17. Integers are numbers without decimals.
Floating point numbers are numbers with a decimal point or exponent.
Logic is true or false.
PHP var_dump() will return the data type and value of the variable:
Example:
<?php $x = 5985; var_dump($x); echo "<br>"; $x = -345; // 负数 var_dump($x); echo "<br>"; $x = 0x8C; // 十六进制数 var_dump($x); echo "<br>"; $x = 047; // 八进制数 var_dump($x); ?>
18. Array in a variable Store multiple values.
Example:
<?php $cars=array("Volvo","BMW","SAAB"); var_dump($cars); ?>
19. Objects are data types that store data and information about how to process the data.
In PHP, objects must be declared explicitly.
First we must declare the class of the object. For this we use the class keyword. A class is a structure containing properties and methods.
Then we define the data type in the object class and then use this data type in the instance of that class:
Instance:
<?php class Car { var $color; function Car($color="green") { $this->color = $color; } function what_color() { return $this->color; } } ?>
Thank you everyone for reading, I hope you will benefit a lot.
Original link: https://blog.csdn.net/u013808667/article/details/51669990
Recommended tutorial: "PHP Tutorial"
The above is the detailed content of A summary of PHP basics for beginners. For more information, please follow other related articles on the PHP Chinese website!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
