PHP basic syntax learning

Posted by hookit on Thu, 30 Dec 2021 13:18:11 +0100

1, Initial PHP script

1.PHP start tag

<?php **
** 2.PHP end tag**
** ?>

1). <? php ?> This means entering the PHP mode, and the contents other than the start and end tags will be ignored by the PHP parser.

2). It can be directly embedded in html code, and can be embedded anywhere in html code. Any number of PHP tags can be embedded in an html document

3). The PHP snippet end tag at the end of the file can be better not to omit it in some cases

2. Instruction separator "semicolon"

1). PHP statements are divided into two types: one is to use structure definition statements in the program, and the other is to use function execution statements in the program. The former does not need to use a semicolon at the end, and the latter must use a semicolon at the end!

2). End means? > It implies a semicolon, so the last line of PHP code can not be added with a semicolon!

additional


notes
1) Multiline comment
/
*Multiline comment**
/
2) Single line note
//I'm a single line note*

2, Variable

Variables are containers for storing values (data) temporarily (only when our program is running)

1. Declaration of variables

          PHP One of its features is that it does not require variables to be declared before using them. You create a variable when you assign a value to it for the first time. The variable is used to store, for example, numbers, text strings or arrays. Once a variable is set, we can reuse it in the script. PHP Variables in must use a dollar sign $ Followed by the variable name, using the assignment operator(=)Assign a value to a variable.

2. Release of variables

            unset()Function to release the specified variable

3. Naming of variables

1) Variable names are strictly case sensitive

 $name<br />     $NAME<br />     $Name

2) Variable names start with letters or underscores and can be followed by any number of letters, numbers, or underscores.

The identifier of variable name must not start with a number. No spaces or dots can be used in the middle!
**Variable names can only contain alphanumeric underscores and must start with letters or underscores!!!

<?php
$name=1;
echo $name;//Output the contents of variable $name!
?>

3) Some identifiers in PHP are system defined, also known as keywords, which are part of the PHP language. Therefore, you can't use any of them as constant function names or class names, but unlike other sounds, they can be used as variable names in PHP. However, it's easy to be confused, so it's best not to use PHP keywords as variable names
****

4. Variable variables

<?php
$abc='test';//A variable $abc is defined. The value stored in it is test
$$abc='tttt'; //$test='tttt';
echo $test;
?>

Just understand

5. Reference and assignment of variables

<?php
$a=1000;
$b=$a;
echo $b;//The output $a is also 1000, and the value of $a will not change
?>

3, Variable type

Overview: variable type refers to the data type saved in the variable. Only data of the same type can operate with each other.

1. Introduction to variable types

Use * * var_ Dump (variable name) * * * * * * can output the type of variable!

1) bool (Boolean)

<?php
$a=true;
echo $a;
?>

This is the simplest type. Booleans express true or false, that is, true and false
The following values are considered false and others are true

one ️⃣ Boolean false

2️⃣0

three ️⃣ Floating point 0.0

four ️⃣ Blank string and string 0 / / a blank string means nothing

five ️⃣ Array without members

6️⃣NULL

2) int (integer)

  Store integers in variables

3) float (also known as double)

Save decimals in variables

4) String (string)

A string is a series of characters strung together
Single quotation marks' 'what is included can no longer be included' 'should be escaped with a backslash \

<?php
$b=1;
$a='textqqq\'ssd';
echo $a;
?>

Double quotation mark

<?php
$b=1;
$a='te($b)xtqqq\'ssd';
echo $a;
?>

Delimiter

<?php
$a=<<<aaaaa
  sdfasfsfsfffsafsfasasffsfsaasffas
  aaaaa;
var_dump($a);
?>

5) Array

6) object

7) resource

8)NULL

    null Indicates that a variable has no value and is empty<br />          Assign a variable directly to null<br />         A declared variable is one that has not been assigned a value<br />         cover unset()Variables destroyed by function
<?php
$a=1111;
unset($a);
var_dump($a);
 ?>

2. Mutual conversion of variable types

1) Automatic type conversion

When Boolean values are involved in the operation, true will be converted to integer 1, and false will be converted to integer 0
When there is a null value involved in the operation, the null value will be converted to the integer 0 for operation
When there are int type and float type data involved in the operation, convert int to float type before operation
When there are string and numeric types (int or float) involved in the operation

2) Cast type

<?php
$a='7qwe';
$b=(int)$a;
var_dump($a);
?>

Don't dig a hole for yourself!!!

3) Test function of variable type

** is_boll()
is_int(()
is_array() and so on**

4, Constant

1. Concept

Constants are containers for storing values (data) temporarily (only when our program is running)

2. Definition and use

Define (constant name, constant value)
The naming of constants is similar to that of variables and follows the naming rules of PHP identifiers. By convention, constant identifiers are always capitalized
define ('MY_NAME ',' ha ha ')
The defined() function to check whether a constant is defined
Self defined constants are strictly case sensitive!

3. Difference between constant and variable

1)Constant is not preceded by a dollar sign $<br />    2) Constants can only be used define()Function definition, cannot pass assignment statement<br />    3) Constants can be defined and used anywhere regardless of the rules of variable scope<br />    4) Once a constant is defined, it cannot be redefined or undefined<br />    5) The value of a constant can only be bool,int,float,string type

4. Predefined constants

  Predefined constants are PHP The kernel helps us define constants<br />Some of the predefined constants are____At the beginning, we call these constants again**magic constant **
<?php
$a='EDG';
 define('MY_NAME','EDG');
echo__FILE__;
?>

Predefined constants are case insensitive!

5, Operator

1. General

     **To put it bluntly, it's just some symbols!!!****These symbols can perform some operation on variables or data!**<br />**give an example:**<br />**      !true  **<br />**        !Is operator  true Is an operand**<br />**      $a+$b**<br />**           =Is operator  $a,$b Is an operand**<br />**       true?1: 0  **<br />**             ?And: Yes operator     true 1 and 0 are operands**<br />** ****It is divided into unary operator, binary operator and ternary operator according to the number of operands**<br />**

2. Arithmetic operator

3. String operator

The string operator in PHP has only one English period Also called join operator
**

<?php
$a='Sun Shengli';
$b='YYDS';
echo $a.$b;  
?>

4. Assignment operator

<?php
$a=1;//$a=1+1;
?>

The operand on the left must be a variable. The operand on the right can be an expression, a value, a variable and so on!!!!
Function: assign the value of the expression on the right to the variable on the left

<?php
$a=2;
$b=1;
$a-=$b;//$a=$a-$b;
echo $a;
?>

5. Comparison operator

Compare operands (according to the requirements and rules of comparison operators)
If the result of the comparison satisfies the result of the comparison operator, it is true; otherwise, it is false
==The values are equal=== Type values are equal
PHP stipulates that when using echo to output Boolean values
** echo true; It will output 1 in the interface;

If we need to output Boolean values, we'd better use var_dump() to output more accurate results!

<?php
$a=1;
$b=1;
var_dump($a==$b);//true
?>
<?php
$a=11;
$b='11';
var_dump($a==$b);//String will be converted to integer true!!  
?>
<?php
$a=11;
$b='11';
var_dump($a===$b);//false!!!! 
?>

6. Logical operators


Perform logical operation on the expression, and the result is Boolean value (true, false)
**Requirements:
The value of the expression participating in the logical operation is a Boolean value. If it is not a Boolean value, it will be automatically converted into a Boolean value by PHP, and then participate in the operation**

<?php
$a=true;
$b=true;
var_dump ($a && $b);
?>

7. Bitwise operator

         Bit operators are used to perform bit operations on each binary number in operands PHP It is mainly used in website development, so bit operators are used in PHP Less used in

8. Other operators

1)?: Ternary operator

**Expression 1? Expression 2: expression 3**
If the value of expression 1 is true, expression 2 is executed, otherwise expression 3 is executed

<?php
$a=true ? 10 : 20;//  10
echo $a;
?>

2) ` ` put the system commands in it to execute

<?php
$a=`ipconfig`;
var_dump($a);
?>
                        **Cross platform issues involved!! Not commonly used!!!**

3) @ mask possible errors in expressions

<?php
echo @$a;
?>

4)=>

5)->

6)instanceof

9. Operator priority


<?php
    $a = 3 * 4 % 5; // (3 * 4) % 5 = 2
    $a = true ? 0 : true ? 1 : 2; // (true ? 0 : true) ? 1 : 2 = 2
    $a = 1;
    $b = 2;
    $a = $b += 3; // $a = ($b += 3) -> $a = 5, $b = 5
    echo ++$a + $a++;
?>
//12

Topics: PHP