A summary of PHP basics for beginners

烟雨青岚
Release: 2023-04-08 21:54:01
forward
2670 people have browsed it

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 脚本!";
Copy after login

2. PHP script starts with

Copy after login

3. Example:

   

我的第一张 PHP 页面

Copy after login

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有三种注释:(//或者#或者/* */) // 这是单行注释 # 这也是单行注释 /* 这是多行注释块 它横跨了 多行 */
Copy after login

5. In PHP, all user-defined functions, classes and keywords (such as if, else, echo, etc.) are not case-sensitive.

Example:

   "; echo "Hello World!
"; EcHo "Hello World!
"; ?>
Copy after login

6. However, in PHP, all variables are case-sensitive.

Example

   "; echo "My house is " . $COLOR . "
"; echo "My boat is " . $coLOR . "
"; ?>
Copy after login

7. Variables are containers for storing information:

Copy after login

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:

测试函数内部的变量:

"; echo "变量 x 是:$x"; echo "
"; echo "变量 y 是:$x"; } ----------大括号里面创建的变量属于局部变量 myTest();----------函数调用 echo "

测试函数之外的变量:

"; echo "变量 x 是:$x"; echo "
";-------换行符 echo "变量 y 是:$x"; ?>
Copy after login

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:

Copy after login

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:

Copy after login

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:

"; echo "Study PHP at $txt2"; echo "My car is a {$cars[0]}"; ?>
Copy after login

16.A string in PHP can be any text within quotation marks. You can use single or double quotes to output

Example:

"; $x = 'Hello world!'; echo $x; ?>
Copy after login

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:

"; $x = -345; // 负数 var_dump($x); echo "
"; $x = 0x8C; // 十六进制数 var_dump($x); echo "
"; $x = 047; // 八进制数 var_dump($x); ?>
Copy after login

18. Array in a variable Store multiple values.

Example:

Copy after login

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:

color = $color; } function what_color() { return $this->color; } } ?>
Copy after login

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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!