Home Backend Development PHP Problem PHP basic grammar rules sorting out

PHP basic grammar rules sorting out

Sep 05, 2019 am 11:21 AM
php basic grammar rule

PHP basic grammar rules sorting out

Basic PHP syntax:

1. PHP variables

php variables must start with "$", for example: $name, $age

cannot start with numbers, and finally follow the camel case naming method

Variables written in "" quotation marks are best enclosed in { } to avoid characters at the end that are not parsed.

2. PHP data type

Scalar data type: string, integer, floating point, Boolean

Composite data type: array , Object

Special data type: resource, NULL

1, Integer type

Value range: -2.1 billion~2.1 billion

2. Floating point type

##Value range: -1.7E-308~1.7E 308

Since floating point numbers cannot be converted into exact binary, sometimes errors will occur during transportation, for example: (0.7 0.1) *10==8 The result is false

3. String type

3-1. Double quotes

Double quote parsing variables The value of
3-2, single quote

The name of the single quote parsed variable

3-3, long string

$Str = ...... Fill in the string content

heredoc ; The ending "heredoc" must start on a new line and end with a semicolon

3-4, resource type

Operations for third-party plug-ins, such as calling mysql database, etc. , third-party content is called resources

3-5, Boolean type

There are only two types of values, true or false

3-6. NULL type

If the variable does not exist, NULL is returned. The null type has only one value, which is NULL

3-7. Array

a. Classification of arrays

1. Index array

The subscript of the array is a positive integer starting from 0, so The array is called the "index array"

$arr=array(10,20,30,40);

$arr[0]=10;

2. Associative array

The subscript of the array is a string. Such an array is called an "associative array"

$arr=array("name"=>"Tabb ","sex"=>"gender","age"=>"22");

Since the associative array does not have an integer subscript, it is not suitable to use a for loop to traverse

3. Mixed array

Array subscripts include both integers and strings. Such an array is called a "mixed array"

$arr = array("name" => "tabb", "age" => "20", "tom", 20);

echo $arr[0] The output result is "tom"; Description The integer subscript is calculated starting from "tom"

b. Array Creation

1. Use the array() function to create an array

Use the "=>" overload symbol to specify a subscript for the array element

If the array element does not specify a subscript, its subscript is, the current maximum index is 1, example:

$arr=array(20=>"tabb",2=>"28",20,"tom") The index of "20" is 21. If they are all character subscripts, then For elements without a specified subscript, the index starts from 0

Use array to create an array:

$arr ​​= array([$key=>]$value,[$key=>]$ value,...);

2. Use [] to create an array

$arr["key"]=$value

No Like js, you don't need to declare an empty array first. If the specified array does not exist, the array will be automatically created

[ ] When the content of the square brackets is empty, the array will be created

$arr[ ]= 30;

If the array does not exist, an array will be created and the first element will be added.

If the array exists, the subscript of the element added to the array will be the largest integer subscript in the array. Standard 1

c, multi-dimensional array

array(array(1,2,3,4),1,2,3,array(1, 2,3,4))

Use [ ] brackets to quickly create a multi-dimensional array:

$arr[ ][ ][ ][ ]=10; Quickly create a 4-dimensional array

d, array related functions

1, print_r()

Print easy-to-understand information about variables

If the array or object is given, display the keys and elements according to a certain format

2. unset()

Delete the array elements, delete the It is the value of the array element, and the subscript still exists. Unlike js, the length remains unchanged. After the element is deleted here, the array length changes.

Delete the variable

3, count( )

Calculate the number of cells in the array or the number of attributes in the object


4. foreach() can only traverse the array

foreach( $arr as [$key=>]$value ){ } If it is an index array, [$key=>] does not need to be included

3. PHP data type judgment

1. var_dump()

Print variables For related information, multiple variables can be printed, separated by "," commas

2. is_*() A set of methods to determine variable types, returning a Boolean value

is_bool(): Determine whether the variable is a Boolean type

is_int(): Determine whether the variable is an integer type

is_float (): Determine whether the variable is a floating point type

is_numeric(): Determine whether the variable is of numeric type

s_string(): Determine whether the variable is of string type

is_array(): Determine whether the variable is of array type

is_object(): Determine whether the variable is of object type

is_null(): Determine whether the variable is of empty type

is_resource(): Determine whether the variable is of resource type

3. isset()

Check whether the variable is set

Return value: If the variable exists and is not equal to NULL, return true, otherwise, return false

4. empty()

Detect whether a variable is empty, such as "", 0, "0", null, array(), var $var and Objects without any attributes will be considered empty. If it is empty, the return value is TRUE

4. Data type conversion

(1) Conversion For bool value (bool)

$a = "0";

$result = (bool)$a;

var_dump($result) The result is : bool(false)

These data are false when converted to bool values: 0, 0.0, "", "0", array(), null

The resource type is always converted to bool. true

(2) Convert to integer value (int) (int)$name

1. If the string starts with a value, convert the integer part For integer values, remove the following characters

2. If it is not a string starting with a value, convert it to 0

3. The bool value true is converted to 1, false is converted to 0, and null is converted For 0

(3) Convert to floating point value (float) (float)$name

1. If the string starts with a numerical value, convert the numerical part For a numerical value (including the floating point part), remove the following characters

2. If it is not a string starting with a numerical value, convert it to 0

3. The bool value true is converted to 1, and false is converted to 0, null is converted to 0

(4) converted to string (string) (string)$name

1.null, false is converted to "", empty String

2.true is converted into the string "1"

5. Operator priority

(1) Numeric operators ( , -, / ,*,%, ,--)

The usage method is basically the same as that in js

(2)String operator

"."dot operator Indicates the string link character, which is similar to using " " to splice strings in js

$a="abc";

$b=$a.100 Or use ".=" to express Link strings with yourself: $a.=100

(3) Assignment operators (=, =, -=, *=, /=, %=) are basically the same as js

(4) Comparison operators (>, =,

Example: 10= = "10px" The result is: true; in operations involving numbers, "10px" will be converted to a numerical value first

Example: 10===="10px" The result is: false; Congruent comparison, including data types

(5) Logical operators (&&, ||, !) are basically the same as js

(6) Ternary operator (expression? Result 1: Result 2) Basically the same as js

(7) Operator priority

PHP basic grammar rules sorting out

Recommended video tutorial: PHP video tutorial

The above is the detailed content of PHP basic grammar rules sorting out. For more information, please follow other related articles on the PHP Chinese website!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles