"PHP Study Notes - PHP Basic Grammar"

"PHP Study Notes - PHP Basic Grammar"

Foreword:

    PHP is a server-side scripting language, and like JavaScript, it is also a weakly typed language. The biggest feature of weakly typed languages ​​is that they allow implicit conversion of variables. In this way, compared with Java, which is a strongly typed language, most errors have been found during the compilation process, and sometimes it is difficult for us to find out what went wrong in PHP. Therefore, the basic syntax of PHP is more important.

content:

    PHP language tag:

        1. You can embed php in HTML in the form of code : start with <?php and end with ?>. The PHP engine will parse the content inside.

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<?php
		echo "<strong>Blithe 的 php</strong>";
	 ?>
</body>
</html>

The above is a simple embed.


The rendering browser will parse the tag <strong>


   2. Each command needs to end with ; (semicolon). The last sentence is not required and will not throw an error. But the advice is to write every sentence.

   3. Annotation : The annotation method is similar to that of Java.

            Single-line comments are // Multi-line comments are /** comment content*/

    4, programming blank line usage habits

            Two empty lines between classes and one empty line before and after functions (personal habit, easy to read the code)

    

variable:

     variable declaration

	<?php
		$first = "Blithe";
		echo "<strong> $first 的 php</strong>";
	 ?>

                Dollar sign $+ variable name. The effect here is the same as above. The echo command parses the variable inside the double quotes.

            variable assignment by reference

            To use assignment by reference, simply precede the source variable with an "&" sign. It looks like C++ pointers. However, in fact, the two variables do not point to the same memory.  It's just a simple connection between them.

	<?php
    		$first = "Blithe first variable";
		$second = &$first;
		$second = "Blithe second variable";
		echo "<strong> $first 的 php</strong>";
	 ?>

                                                           

        type of variable
               PHP data types are:
                        Scalar types: boolean (Boolean), integer (integer), float (float), string (string).
                        Composite types: array (array), object (object).
                        Special types: resource, NULL
                The declaration string can have both single and double quotes:
                    Single quotes: cannot contain single quotes, backslashes (\) are required, and variable names within single quotes will not be replaced by variable values.
                    Double quotes: can escape more characters such as: \n (newline) \r (carriage return) \t (tab) \\ (slash) \$ (dollar) and regular expressions. You can also parse the variables inside.
                    Delimiter: $string = <<<EOT string content EOT;
            

            array:

                $array = array("foo"=>"bar",12=>"lalal");//Declare an array

                Arrays are important content, please see the blog about arrays for more

        

            NULL:

                The following 3 situations will be considered as NULL: 1. The variable is directly set to NULL 2. The declared variable has not been assigned a value 3. The variable is destroyed by the unset() function.

                

        conversion of variables
                Automatic type conversion, as shown below
                coercion _
                    Just prepend (int)(bool)(float)(string)(array)(object) to the variable
                    When NULL is converted to a string, it needs to become an empty string ""
                
          
        Constants and "magic constants" in PHP

              Declaring constants can save space and be relatively efficient. Almost every extension in PHP provides a large number of constants available by default, and PHP also provides more practical magic constants.

        Definition of constants

	<?php
	/**
		Constant declarations use the function define(name,value,[bool case_insensitive])
		name : constant name (all uppercase recommended)
		value : constant value
		case_insensitive: optional parameter, if set to true, it is not case sensitive. Default is false (case sensitive)

		Return value: bool value
	*/

	//declare a constant named BLITHE
		define("BLITHE","in php studying");
		echo "<a>".BLITHE."这是常量BLITHE</a><br>";
	//declare a case-insensitive constant
		define("TIME",date("Y/m/d"),TRUE);
		echo "<h2>".time."</h2>";
		echo "<h2>".TIME."</h2>";

	/**
		The defined() function checks if a variable exists
	*/
		if(defined(TIME)){
			echo "The variable TIME exists";
		}

		if (defined(LALA))
		{
			echo "Variable LALA exists";
		}else{
			echo "Variable LALA does not exist";
		}
	 ?>
operation result

    Magic constants in PHP

        Constants that change according to position in PHP are called "magic constants". Magic constants are as follows:

	<?php
	class B{
		function testBlithe(){
		// the filename where
			echo __FILE__;
			echo "<br>";
		// the number of lines where
			echo __LINE__;
			echo "<br>";
		//The name of the function where it is located
			echo __FUNCTION__;
			echo "<br>";
		// in the class
			echo __CLASS__;
			echo "<br>";
		// used in the method of the current object
			echo __METHOD__;
		}

		function play(){
			$this->testBlithe();
		}
	}
	$bb = new B();
	$bb->play();
	 ?>

Operations in PHP

    Arithmetic operators: + - * / % ++ -- are addition, subtraction, multiplication and division, modulo accumulation 1, decrement 1 

    String operations Strings are used together. (dot)

	<?php
		$blithe = "blithe";
		$bb = "____BLITHE______";
		$cc = $blithe.$bb;//The two strings are connected
		echo "$cc";
	 ?>
result graph

    Comparison operators: > , < , ==, >=, <= , ==, ===, <>, !=, !==

                        greater than, less than, equal to, greater than or equal to, less than or equal to, strictly equal, not equal, not equal, strictly not equal

                The difference between strict equals and equals

	<?php
		$num1 = "123";
		$num2 = 123;
		$ num3 = 123;
		var_dump($num1 == $num2);//bool(false)
		var_dump($num2 == $num3);//bool(true)
		var_dump($num1 === $num2);//bool(false)
		var_dump($num2 === $num3);//bool(false)
	 ?>

Logical Operators:

     Logical AND: and or &&, Logical OR: or or ||, Logical NOT: not or!, Logical Exclusive OR xor;

bitwise operators

    Bitwise AND: & Bitwise OR: | Bitwise XOR: ^ Bitwise NOT: ~ Shift Left << Shift Right >>

 Ternary operator:

    (exp1)?(exp2):(exp3)

   Equivalent to if(exp1){

                        exp2;

                }else{

                        exp3;

                }

This article is original, if there is any error. Kindly point it out in the comments. mutual encouragement!

  • Copyright 2018-4-21 by Blithe_xyn.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326312872&siteId=291194637