search
HomeBackend DevelopmentPHP TutorialA summary of PHP basics for beginners

A summary of PHP basics for beginners

Jun 15, 2020 pm 01:28 PM
phpnewbie

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 id="我的第一张-nbsp-PHP-nbsp-页面">我的第一张 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 = &#39;Hello world!&#39;;
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!

Statement
This article is reproduced at:CSDN. If there is any infringement, please contact admin@php.cn delete
What is the difference between unset() and session_destroy()?What is the difference between unset() and session_destroy()?May 04, 2025 am 12:19 AM

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

What is sticky sessions (session affinity) in the context of load balancing?What is sticky sessions (session affinity) in the context of load balancing?May 04, 2025 am 12:16 AM

Stickysessionsensureuserrequestsareroutedtothesameserverforsessiondataconsistency.1)SessionIdentificationassignsuserstoserversusingcookiesorURLmodifications.2)ConsistentRoutingdirectssubsequentrequeststothesameserver.3)LoadBalancingdistributesnewuser

What are the different session save handlers available in PHP?What are the different session save handlers available in PHP?May 04, 2025 am 12:14 AM

PHPoffersvarioussessionsavehandlers:1)Files:Default,simplebutmaybottleneckonhigh-trafficsites.2)Memcached:High-performance,idealforspeed-criticalapplications.3)Redis:SimilartoMemcached,withaddedpersistence.4)Databases:Offerscontrol,usefulforintegrati

What is a session in PHP, and why are they used?What is a session in PHP, and why are they used?May 04, 2025 am 12:12 AM

Session in PHP is a mechanism for saving user data on the server side to maintain state between multiple requests. Specifically, 1) the session is started by the session_start() function, and data is stored and read through the $_SESSION super global array; 2) the session data is stored in the server's temporary files by default, but can be optimized through database or memory storage; 3) the session can be used to realize user login status tracking and shopping cart management functions; 4) Pay attention to the secure transmission and performance optimization of the session to ensure the security and efficiency of the application.

Explain the lifecycle of a PHP session.Explain the lifecycle of a PHP session.May 04, 2025 am 12:04 AM

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

What is the difference between absolute and idle session timeouts?What is the difference between absolute and idle session timeouts?May 03, 2025 am 12:21 AM

Absolute session timeout starts at the time of session creation, while an idle session timeout starts at the time of user's no operation. Absolute session timeout is suitable for scenarios where strict control of the session life cycle is required, such as financial applications; idle session timeout is suitable for applications that want users to keep their session active for a long time, such as social media.

What steps would you take if sessions aren't working on your server?What steps would you take if sessions aren't working on your server?May 03, 2025 am 12:19 AM

The server session failure can be solved through the following steps: 1. Check the server configuration to ensure that the session is set correctly. 2. Verify client cookies, confirm that the browser supports it and send it correctly. 3. Check session storage services, such as Redis, to ensure that they are running normally. 4. Review the application code to ensure the correct session logic. Through these steps, conversation problems can be effectively diagnosed and repaired and user experience can be improved.

What is the significance of the session_start() function?What is the significance of the session_start() function?May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

DVWA

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

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)