search
HomeBackend DevelopmentPHP TutorialSummary of PHP basic syntax, basic php syntax_PHP tutorial

Summary of basic syntax of PHP, basic syntax of PHP

1. What can PHP do?

What can PHP do? I think it is very powerful. It can do anything I can think of, but my technical skills are not good enough╮(╯﹏╰)╭. Okay, let’s have a basic understanding with a picture (ps: PHP’s functions are not limited to this (^_^))

The image is a bit blurry, try to make it better, (≧▽≦)/

2. PHP language tags

1. End and start tags

1.1 : It belongs to xml style and is the standard style of PHP. It is recommended to use.

1.2 : Long style tag, not commonly used. If your weird editor doesn’t support other php tags, use this

1.3 //code ?>: Short style, follow SGML processing. You need to turn on the short_open_tag directive in php.ini, or add –enable-short-tags when compiling PHP. If you want your program to be more portable, abandon this style. It will have one less PHP than 1.1.

2. Location

How to say it? Anyway, you can put the PHP language anywhere in the HTML file with the suffix .php. Note that it is an HTML file ending with .php.

Copy code The code is as follows:


1:
2:
3: & lt; meta http-equiv = "Content-Type" Content = "Text/HTML; Charset = UTF-8" & GT;
4: & lt;!-Embedded in the HTML mark-& gt;
5: & lt; title & gt; & lt;? PHP Echo "PHP language mark"? 6:
7: & lt;!-Embeds in the attribute position-& gt;
8: >
9: & lt;!-Let's have a high-level point-& gt;
10:                                                                       11: If($exp){
12: ?>
13: & lt;!-PHP-& gt;
14:         

What to do if the condition is true


15:                                                                        16:                                                                                                                                                                                                 }else{
17: ?>
18: & lt; p & gt; conditions are & lt;/p & gt;
19:                                                                         20:           }
21: ?>
22:
23:



3. Comments
3.1 Single-line comment: // or # Multi-line comment: /* Description*/

3.2 Multi-line comments cannot be nested, but they can contain single-line comments; single-line comments can also contain multi-line comments. This is what I want

Copy code

The code is as follows: 1: 2: //echo "test";/*Single line contains multi-line comment characters*/
3: /*echo 'test'; //Multi-line comments include single-line comments*/
4: ?>




3. Variables

1. Use of variables

Copy code

The code is as follows:

1: 2: $a = 1; //Declare a variable a
3: $b = "php"; //Declare a variable b
4: $8d = 2; //Illegal variable name, it can only start with a letter or underscore and does not contain spaces
5:
6: $i site is = "php"; //Legal variable name, you can use Chinese
7: /*
8: *The following three function calling methods are equivalent
9: *Keywords, built-in functions and user-defined class names, function names are not case-sensitive
10: */
11: phpinfo();
12: PhpInfo();
13: PHPINFO();
14:
15:
16: /*
17: *The following three variables are different
18: *Variable names are case-sensitive
19: */
20: $name = "php1";
21: $Name = "php2";
22: $NAME = "php3";
23:
24: //Variable variables: variable names can be dynamically set
25: $hi = "hello";
26: $$hi = "world";
27: //The following output hello world
28: echo "$hi $hello";
29: echo "$hi ${$hi}";
30:
31: //Variable assignment
32: $foo = "B" //Assignment by value
33: $bar = &$foo //Reference assignment
34: $bar = "LZ";
35: echo "$foo"; //Output LZ
36: $cde = $foo; //Assignment by value
37: $cde = "E";
38: echo "$foo"; //Output LZ
39: ?>

2. Type of variable

4. Constants

1. Definition and usage

Copy code The code is as follows:

1: 2: /*
3: *boolean define(string name,mixed value[,bool case_insensitive)
4: *name: constant name; value: constant value; the third is an optional Boolean value, the default is FALSE (case-insensitive)
5: */
6: define("FLO",1000);
7: echo FLO; //Output 1000
8:
9: //Use the define function to check whether the FLO constant exists, and if it exists, output the constant value
10: if(define("FLO"))
11: {
12: echo FLO;
13: }
14: ?>

2. Constants and variables

2.1 The scope of constants is global, and constants can be declared and accessed anywhere in the script.

2.2 Constants do not have $ in front of them, and constants cannot be defined through assignment statements.

2.3 Once a constant is defined, it cannot be redefined or undefined until it is automatically released after the script runs.

2.4 The value of a constant can only be a scalar (a type of boolean, integer, float, string)

3. System predefined constants

4. Commonly used magic constants

Basic php syntax issues

Double quotes are used to output strings. For example: echo "Data insertion failed, error message:
";
And "INSERT INTO testtable VALUES('".$xm."',".$nl.")"; in INSERT INTO testtable VALUES It is a string, which means inserting into the database. The two "" are a group, which separates .$xm.. $xm in (.$xm.) is a variable. Echo is used when displaying a variable in PHP.

The basic syntax of PHP and database has been basically mastered. Recommend a PHP book with a complete project? ? I really want to learn php

"PHP and MySQL Web Development" is the "php bible". In comparison, "Learning PHP from Zero Basics" is more suitable for beginners. Reading this book, it is easy to feel that the author wrote it carefully.
This book is actually one of the series "Learning Programming from Zero Basics", the others are very good.
The content is well designed. There is a preface that is very pertinent. It is advice given by experienced programmers to newbies. Here is the excerpt:
++++++++++++++++++++++++ +++++++++++++++++++++++++++++++++++++++++++
Things to note when learning programming A few points:
1. Don’t memorize grammar: Many beginners try to memorize various grammars. In fact, this is extremely wrong. There are so many grammars and specifications in program development that it is impossible to remember them all. You only need to know that there are Such a function is enough. You can browse books or find help files when needed, which saves time and effort.
2. Use multiple hands and practice more: People who only know how to chew books will not become masters of development. Only by writing programs on the computer more can you improve your understanding of programming in practice.
3. When you encounter a problem, try to solve it yourself first: use your own time to try your best to solve it. If it doesn’t work, go to someone for help. Never ask someone for help immediately when you encounter a problem. This will never improve much. .
4. Use Google and Baidu more: The Internet is a large knowledge base and the best teacher. The problems you encounter have been encountered by others, so search more.
4. Read more other people’s source codes: Understand other people’s design ideas and constantly integrate them into your own use.
++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++
The shortcoming of this book is that there are no line markings in front of the code, and "at line xxx," is used extensively in the explanation of the code, A big mistake.

Summary: In terms of knowledge points, "Learning PHP from Zero Basics" may not be complete, but what we need is not a dictionary, what we need is a step-by-step book that will make you interested in learning, right? ?

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/875391.htmlTechArticleSummary of PHP basic syntax, basic php syntax 1. What can PHP do? What can PHP do? I think it is very powerful, as long as I can think of it, it can do it, but my technical ability is not good╮(╯...
Statement
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
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

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 Article

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools