[PHP] Basic Grammar & Variables & Constants

Introduction to PHP

PHP Definition: A server -side HTML scripting/programming language that is simple, object-oriented, interpretable, robust, secure, very high-performance, architecture-independent, portable, dynamic scripting language. Is a widely used Open Source (open source) multi-purpose scripting language especially suitable for Web development and can be embedded in HTML. Its syntax is close to C, Java and Perl, and it is easy to learn. The language lets web developers quickly write dynamically generated web pages .

pre-knowledge

Features of a static website

1. Once the webpage content is published on the website server, the content of each static webpage is stored on the website server regardless of whether there is a user accessing it. That is to say, the static webpage is actually a file stored on the server. Each page is a separate file;

2. The content of static web pages is relatively stable, so it is easy to be retrieved by search engines;

3. Static webpages do not have the support of databases, and the workload in website production and maintenance is relatively large, so it is difficult to completely rely on static webpage production methods when the website has a large amount of information;

4. Static web pages have poor interactivity and have relatively large limitations in terms of functionality.


Dynamic website features

1. Interactivity: Web pages will dynamically change and respond according to user requirements and choices. As a client, the browser becomes a bridge for dynamic communication. The interactivity of dynamic web pages is also the trend of Web development in the future.

2. Automatic update: that is, there is no need to manually update the HTML document, and a new page will be automatically generated, which can greatly save the workload.

3. Change from time to time and from person to person: that is, different pages will appear when different users visit the same website at different times.

In addition, dynamic webpages correspond to static webpages, that is to say, the suffixes of webpage URLs are not common forms of static webpages such as .htm, .html, .shtml, and .xml, but . The .perl, .cgi, etc. forms are suffixes. There is an iconic symbol in the URL of a dynamic webpage - "?"


Basic syntax of PHP

First of all: PHP is a scripting language that runs on the server side and can be embedded in HTML

In order to prevent garbled characters from being displayed in Chinese, it is recommended to add the following sentence

//处理脚本让浏览器按照指定字符集解析的方法
header('Content-type:text/html;charset=utf-8');

code markup

Throughout the history of PHP, various tags have been used to distinguish PHP scripts

  • ASP tag: <% php code %>
  • Short tag: <? Php code?>

The above two are basically deprecated, and the more commonly used flags are:<?php php代码?>

image-20230731095213555


note

There are two types of comments in PHP: line comments and block comments

  • Line Comments: Comment one line at a time. Writing 1: #Writing 2://
  • Block comments: comment multiple lines at once/* 中间直到*/出现之前,全部都是注释 */

statement separator (end)

In PHP, the code is in the unit of behavior, and the system needs to judge the end of the line, which is usually a symbol分号;

Special Instructions:

1. The tag terminator in PHP?> has the effect of its own statement terminator, and the last line of PHP code can have no statement terminator

<?php
	echo "hello I am Mango ! PHP yyds!";
	echo "我可以没有分号"
?>   

2. In fact, a lot of code writing in PHP is not embedded in HTML, but exists alone. Usually, it is not recommended to use the tag terminator ?> in writing habits, and PHP will automatically consider it as PHP code from the beginning to the end, so as to parse

<?php
	echo "hello I am Mango ! PHP yyds!";
	echo "hello world";

variable

Basic concept of variables

Variables come from mathematics and are abstract concepts in computer languages ​​that can store calculation results or represent values. Variables can be accessed by variable name . In imperative languages, variables are usually mutable

1. Variables are used to store data

2. Variables have names

3. Variables are accessed by name (data)

4. Variables can be changed (data)

use of variables

All variables in PHP must use the "$" symbol

1. Definition: Add the corresponding variable name (memory) in the system

2. Assignment: Data can be assigned to the variable name (can be done at the same time as the definition)

3. The stored data can be accessed by variable name

4. Variables can be deleted from memory

image-20230731101054100


Variable Naming Rules

1. In PHP, the variable name must start with the "$" symbol;

2. The name is composed of letters, numbers and underscore "_", but cannot start with a number;

3. Chinese variables are also allowed in PHP (not recommended)

$1var;//err    
$中国 = 'China'; #中文变量 ok

predefined variable

Predefined variables: variables defined in advance, variables defined by the system, store many data that needs to be used (predefined variables are essentially arrays)

  • $_GET: Get the data submitted by all forms in get mode
  • $_POST: The data submitted by POST will be saved here
  • $_REQUEST: Both GET and POST submissions will be saved
  • $GLOBALS: All global variables in PHP
  • $_SERVER: server information
  • $_SESSION: session session data
  • $_COOKIE: cookie session data

example:

image-20230731101624474


mutable variable

Variable variable: If the value stored in a variable happens to be the name of another variable, then you can directly access a variable to get the value of another variable: add an additional $ symbol in front of the variable

$a = 'b';
$b = 'I am b';
echo $$a;   # $$a == > $b ==> 'I am b'
  • 1. Find $a first, and parse out the result as b
  • 2. Bind the previous $ symbol to b, which is $b, and then output

variable value

Assign a variable to another variable: variable value

  • There are two ways of passing variables: passing by value and passing by reference.

Value transfer: assign a value saved by a variable, and then save the new value to another variable (the two variables have nothing to do with each other)

example:

<?php
	$a = 1;
	$b = $a; //值传递
	
	$b = 2;
	echo $a,'<hr/>',$b; //输出1,2

image-20230731102045621


Pass by reference: pass the memory address where the value saved by the variable is located to another variable: the two variables point to the same memory space (the two variables have the same value)

  • $新变量 = &$老变量
<?php
	$a = 1;
	$b = &$a; //值传递
	
	$b = 2;
	echo $a,'<hr/>',$b; //输出2,2

image-20230731102127858


memory partition

In memory, there are usually the following partitions

Stack area: the part of the memory that the program can operate (do not store data, run the program code), small but fast

Code Segment: The part of memory where the program is stored (not executed)

Data segment: store common data (global area and static area)

Heap area: store complex data, large but inefficient


constant

basic concept

Constants, like variables, are used to store data.

Constant: const/constant is a quantity (data) that cannot be changed during the running of the program. Once the constant is defined, usually the data cannot be changed (user level)

constant definition form

There are two ways to define constants in PHP (only after 5.3)

  • 1. Use the function of defining constants: define('constant name', constant value);
  • Only available after version 2.5.3: const constant name = value;
define('PI',3.14);
const PII = 3.14;

Naming rules

1. There is no need to use the "$" symbol for constants. Once used, the system will consider them variables;

2. The name of the constant is composed of letters, numbers and underscores, and cannot start with a number;

3. The names of constants are usually capital letters (to distinguish them from variables);

4. The naming rules of constants are looser than those of variables, and some special characters can be used, which can only be defined by define;

image-20230731102438245

detail:

1. There is a difference between the constants defined by define and const: the difference in access rights

2. The definition of constants is usually not case-sensitive, but it can be distinguished. You can refer to the third parameter of the define function

image-20230731102701222

The first parameter specifies the name of the constant, also called the identifier

The second parameter specifies the value of the constant, which is a scalar data type that does not want to be changed.

The third parameter is an optional parameter , used to specify whether the constant name is case-sensitive


form of use

The use of constants is the same as that of variables: the value cannot be changed ** (must be assigned when it is defined) **. Constants do not need to use the "$" symbol, once used the system will be considered as a variable

echo PI;
echo PII;

Sometimes you need to use another form to access (for constants with special names), you need to use another function to access constants: constant('constant name')

<?php
	define('-_-','smile');
	
	//echo  '-_-';// 特殊符号不能直接使用
	echo constant('-_-');
?>

Description: Use of constants and variables

1. Whenever the data may change, then it must be variable

2. The data does not necessarily change, you can use constants or variables (variables are mostly)

3. If the data is not allowed to be modified, constants must be used


system constant

System constants: constants defined by the system to help users, users can use directly

Commonly used system constants:

  • PHP_VERSION: PHP version number
  • PHP_INT_SIZE: integer size
  • PHP_INT_MAX: The maximum value that can be represented by integers (in PHP, integers are allowed to have negative numbers: signed)
echo '<hr/>',PHP_VERSION,'<hr/>',PHP_INT_SIZE,'<hr/>',PHP_INT_MAX;  #5.4.8  4   2147483647

magic constant

There are some special constants in PHP, they start with double underscore + long two digits + end with double underscore, this kind of constant is called system magic constant: the value of magic constant will usually change with the environment, but the user cannot change

  • _ DIR _: the absolute path of the computer where the currently executed script is located
  • _ FILE _: The absolute path of the computer where the currently executed script is located (with its own file name)
  • _ LINE _: the number of line the current belongs to
  • __NAMESPACE__ : the current namespace
  • __CLASS__: the current class__
  • _METHOD_ : The method the current belongs to
  • __FUNCTION__: the name of the current function

image-20230731140030853

Guess you like

Origin blog.csdn.net/chuxinchangcun/article/details/132423950