Notes for beginners of PHP June 12, 2018

Write in front

The contact about PHP was limited to the course design at that time. Now, the old people and things have long gone with the wind, but the young blog content has remained. There is no enthusiasm for learning and exploration in those years. Just re edit these and talk about remembering the past.

PHP variable scope

In PHP, variables can be declared anywhere in the script.
The scope of a variable refers to the part of the script where the variable can be referenced / used

PHP variable rules

  1. The variable starts with the $symbol and is followed by the variable name
  2. Variable names must start with letters or underscores
  3. Variable names cannot start with numbers
  4. Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and)
  5. Variable names are case sensitive ($Y and $y are two different variables)
  6. Note: PHP variable names are case sensitive!

PHP has three different variable scopes

  1. Local (local)
  2. global
  3. static
  4. Local and Global scopes
  5. Variables declared outside the function have Global scope and can only be accessed outside the function
  6. Variables declared inside the function have LOCAL scope and can only be accessed inside the function.
  • For example:

    	echo "hello world!<br>";
    	echo "where are you?<br>";
    	$text = "eleven";
    	//echo $text
    	function echoText(){
    		//$text = "11";
    		echo $text;
    	}
    
  • The result is:

    hello world!
    where are you?

  • After removing the note:

       echo "hello world!<br>";
       echo "where are you?<br>";
       $text = "eleven";
       echo $text
       function echoText(){
       	//$text = "11";
       	echo $text;
       }
    
  • The result is:

    hello world!
    where are you?
    eleven

foreach syntax

  • The first is to traverse the given array statement array_expression array. In each loop, the value of the current cell is assigned to $value and the pointer inside the array moves forward one step (so the next cell will be obtained in the next loop).

    	foreach(array_expression as $value)
    
  • The second is the same as above. At the same time, the key name of the current cell will also be assigned to the variable $key in each cycle.

    	foreach(array_expression as $key => $value)
    
  • One dimensional ordinary array and foreach

    	$a = array('meet','Acquaintance','Acquaintances','Forget');
    	foreach($a as $value){
    		echo $value."<br/>"
    	}
    	echo "-------------<br/>"
    	foreach($a as $key => $value){
    		echo $key.','.$value."<br/>"
    	}
    
  • The result is:

    meet
    Acquaintance
    Acquaintances
    Forget

    0. Meet
    1. Acquaintance
    2. Acquaintances
    3. Forget

    Summary: there is an additional $key. The value of $key is serial number 1, 2, 3, 4

  • One dimensional associative array and foreach

    	$b = array('one' => 'meet','two' => 'Acquaintance','three' => 'Acquaintances','four' => 'Forget');
    	foreach($b as $value){
    		echo $value."<br/>"
    	}
    	echo "-------------<br/>"
    	foreach($b as $key => $value){
    		echo $key.','.$value."<br/>"
    	}
    
  • The result is:

    meet
    Acquaintance
    Acquaintances
    Forget

    1, Meet
    2, Acquaintance
    3, Acquaintances
    4, Forget

    Summary: in a one-dimensional associative array, $key is the serial number of the association, that is, the corresponding one, two, three and four.

  • Two dimensional ordinary array and foreach

    	$c = array(
    		 array('1','meet'),
    		 array('2','Acquaintance'),
    		 array('3','Acquaintances'),
    		 array('4','Forget'),
    	);
    	foreach($c as $value){
    		print_r($value)
    		echo "<br/>";
    	}
    	echo "-------------<br/>"
    	foreach($c as $key => $value){
    		echo '$key='.$key."<br/>"
    		print_r($value)
    		echo "<br/>";
    	}
    
  • The result is:

    Array ([0] = > 1 [1] = > meet)
    Array ([0] = > 2 [1] = > acquaintance)
    Array ([0] = > 3 [1] = > acquaintances)
    Array ([0] = > 4 [1] = > forgetting)

    $key=0
    Array ([0] = > 1 [1] = > meet)
    $key=1
    Array ([0] = > 2 [1] = > acquaintance)
    $key=2
    Array ([0] = > 3 [1] = > acquaintances)
    $key=3
    Array ([0] = > 4 [1] = > forgetting)

    Summary: it can be seen from the above that in the basic two-dimensional array, $key is the serial number, such as 0, 1, 2, 3

  • Associative 2D array and foreach

    	$d = array(
    		 array('id' => '1','name' => 'meet'),
    		 array('id' => '2','name' => 'Acquaintance'),
    		 array('id' => '3','name' => 'Acquaintances'),
    		 array('id' => '4','name' => 'Forget'),
    	);
    	foreach($d as $value){
    		print_r($value)
    		echo "<br/>";
    	}
    	echo "-------------<br/>"
    	foreach($d as $key => $value){
    		echo '$key='.$key."<br/>"
    		print_r($value)
    		echo "<br/>";
    	}
    
  • The result is:

    Array ([ID] = > 1 [name] = > meet)
    Array ([ID] = > 2 [name] = > acquaintance)
    Array ([ID] = > 3 [name] = > acquaintances)
    Array ([ID] = > 4 [name] = > forgetting)

    $key=0
    Array ([ID] = > 1 [name] = > meet)
    $key=1
    Array ([ID] = > 2 [name] = > acquaintance)
    $key=2
    Array ([ID] = > 3 [name] = > acquaintances)
    $key=3
    Array ([ID] = > 4 [name] = > forgetting)

    Summary: $key is still 0, 1, 2, 3

practice

  • Associative two-dimensional array becomes ordinary one-dimensional array
    	$d = array(
    		 array('id' => '1','name' => 'meet'),
    		 array('id' => '2','name' => 'Acquaintance'),
    		 array('id' => '3','name' => 'Acquaintances'),
    		 array('id' => '4','name' => 'Forget'),
    	);
    	foreach($d as $key => $value){
    		echo ($value['name'])
    		echo "<br/>";
    	}
    	echo "-------------<br/>"
    	//Gets the name column as a one-dimensional array
    	$nameArr = array(); //name column
    	foreach($d as $key => $value){
    		$nameArr[] = $value['name'];
    	}
    	print_r($nameArr );
    
  • The result is:

    meet
    Acquaintance
    Acquaintances
    Forget

    Array ([0] = > meet [1] = > know [2] = > know [3] = > forget)

var_dump() returns the data type and value of the variable

	$x = 5985;
	var_dump($x);
	echo "<br/>";
	$x = -345; //negative
	var_dump($x);
	echo "<br/>";
	$x = 0x8C; //Hexadecimal number
	var_dump($x);
	echo "<br/>";
	$x = 047; //Octal number
	var_dump($x);
	echo "<br/>";
	$x = 10.365;
	var_dump($x);
	echo "<br/>";
	$x = 2.4e3;
	var_dump($x);
	echo "<br/>";
	$x = 8E-5;
	var_dump($x);
	echo "<br/>";
	$cars = array("Volvo","BMW","SAAB");//array
	var_dump($cars );
  • The result is:

    int 5985
    int -345
    int 140
    int 39
    float 10.365
    float 2400
    float 8.0E-5
    array (size=3)
    0 => string 'Volvo' (length=5)
    1=> string 'BMW' (length=3)
    2 => string 'SAAB' (length=4)

PHP object

  1. An object is a data type that stores data and information about how data is processed.
  2. In PHP, objects must be explicitly declared.
  3. First, declare the class of the object with the class keyword. A class is a structure that contains properties and methods
  • Define a data type in an object class and then use it in an instance of that class.

    	class Car{
    	    var $color;    
    	    function Car ($color = "green") {
    	        $this->color = $color;
    	    }
    	    function what_color () {
    	        return $this->color;
    	    }
    	}
    	
    	function print_vars ($obj) {
    	    foreach(get_object_vars($obj) as $prop => $val) {
    	        echo "\t$prop = $val\n";
    	    }
    	}
    	//instantiate one object
    	$herbie = new Car("white");
    	//show herbie properties
    	echo "\therbie  = Properties\n";
    	print_vars($herbie);
    
    

    The result is:

    herbie: Properties color = white

    Note:

    1. Public and var have similar functions, because if the variables defined by var are not protected or private, they are public by default

    2. VaR is generally used in PHP4, and public is generally used in PHP5

    3. Now, public is basically used instead of var
      var is the of defining variables; public defines the visibility of property and method

PHP constants

  1. A constant is an identifier (name) of a single value. The value cannot be changed in a script

  2. Valid constant names begin with a character or underscore (constant names are not preceded by the $symbol)

  3. Unlike variables, constants are automatic and global throughout the script

  4. define() function for setting PHP constants - it uses three parameters:

    • The first parameter defines the name of the constant
    • The second parameter defines the value of the constant
    • The optional third parameter specifies whether the constant name is case insensitive. The default is false.
    	//Defines constants that are case sensitive
    	define("SAD","Everlasting longing for each other");
    	echo SAD;
    	echo "<br/>";
    	//Constant values that will not be output
    	echo sad;
    	echo "<br/>";
    	echo "<--------------------<br/>";
    	define("SAD","Everlasting longing for each other",true);//Case insensitive value is true
    	echo SAD;
    	echo "<br/>";
    	echo sad;
    

    The result is:

    Everlasting longing for each other
    Notice:use of undefined constant sad-assumed 'sad' in ...\test.php on line 7

    TimeMemoryFunctionLocation
    0.0010133800(main)()...\test.php:()

    sad

    Everlasting longing for each other
    Everlasting longing for each other

Write it at the back

It's finally finished, but things are different after all
Like before, it's not like before.

Keywords: PHP Back-end

Added by testtesttesttest on Sat, 19 Feb 2022 10:25:32 +0200