A summary of PHP basics for beginners

烟雨青岚
Release: 2023-04-08 21:54:01
forward
2719 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

<?php
// 此处是 PHP 代码
?>
Copy after login

3. Example:

<!DOCTYPE html>
<html>
<body>
<h1>我的第一张 PHP 页面</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
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:

<!DOCTYPE html>
<html>
<body>
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
</body>
</html>
Copy after login

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>
Copy after login

7. Variables are containers for storing information:

<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>
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:

<?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";
?>
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:

<?php
$x=5;
$y=10;
function myTest() {
  global $x,$y;
  $y=$x+$y;
}
myTest();
echo $y; // 输出 15
?>
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:

<?php
function myTest() {
  static $x=0;
  echo $x;
  $x++;
}
myTest();
myTest();
myTest();
?>
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:

<?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]}";
?>
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:

<?php 
$x = "Hello world!";
echo $x;
echo "<br>"; 
$x = &#39;Hello world!&#39;;
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:

<?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);
?>
Copy after login

18. Array in a variable Store multiple values.

Example:

<?php 
$cars=array("Volvo","BMW","SAAB");
var_dump($cars);
?>
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:

<?php
class Car
{
  var $color;
  function Car($color="green") {
    $this->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
Popular Tutorials
More>
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!