Comments variables arrays constants function applications in php

  • 2020-05-26 08:06:20
  • OfStack

What is the difference between single and double quotation marks in php?
1. As can be seen from the following, variables with double quotation marks are parsed and output, while variables with single quotation marks are not parsed.
2. Single quotation marks parse faster than double quotation marks
3. For single quotes, there are only two escapes \',\\
4. Except for the above two escape characters, everything is output as is.
5. For double quotes, escaped characters are \\,\',\", and \t, \r,\ n, etc
6. In addition to the single and double quotes used to declare string variables, there is the heredoc way
Program code:
//$age = 22;
//$str1 = 'he is $age years old '; // "output as is
//$str2 = "he is $age years old "; //"" to parse the inside variable, output 22
//echo $str1," < br / > ",$str2;
Comments in php
(1) start with //.
(2) starts with #.
This is the shell single-line comment style
(3) there is one type of multi-line comment that starts with /* and ends with */
Considerations in php
1. In js, if a single sentence occupies 1 line, the end of the line may not be added; No.
2. However, for php addition, a semicolon must be added at the end of every line
Although the exception is made in the last sentence of php as a whole, it is strongly recommended to add it as well
3. For pure php pages,? > I don't have to
Also, for files that do not run directly, but are contained on other pages, it is often recommended to end without? >
This includes pages that execute faster and faster
Variables in php
1. There are 8 types of php variables
2. Integer, floating point, Boolean, string, NULL, array, object, resource
3. In js, declare variables with var variable name [= value]; in php, declare variables with var variable name [= value];
4. Variable naming specification in php
5. The name of a variable is a combination of letters, underscores, and Numbers. And you can't start a number
6. The variable is preceded by a '$' in php
7. echo is not a function, but a grammatical structure.
8. You can print variables
9. To print multiple variables, separate them with ','
10. Variable transfer value
11. Reference pass and assignment pass
15. String type
Variables and constants
(1) constant
Variables can be reassigned at any time
//$age = 22;
//$age = 23;
//echo " < br / > ",$age;
(2) what is the difference between a variable and a constant?
1. They are declared in different ways
2. The constant 1 denier declares that it cannot change its value
define('PI',3.14);
PI = 3.23; Grammar mistakes
echo " < br / > ",PI;
3. Variables can be destroyed, while constant 1 denier cannot be destroyed
unset($age);
var_dump($age);
4. Constants cannot be cancelled
unset (PI); // syntax error, no logout allowed
echo PI;
5. Variables have their scope, and external variables cannot be accessed by default inside functions.
And the constant, 1 denter, is defined either globally or in function internalism.
It can be accessed anywhere on the page.
(3) naming conventions for variables and constants
1. The naming of constants, from the point of view of grammar and variables are 1.
A combination of letters, Numbers, and underscores is allowed, and Numbers cannot begin
2. From a habit point of view: 1 common constants are capitalized.
//define('SF',342);
/ / echo SF; // the latest version is case sensitive for constant names
(4) what kinds of constants are allowed?
1. Only scalar type (single type 1) can be assigned to constants;
2. You cannot assign a compound, such as a number group, object to a constant.
3. Resource type if assigned to a constant, such as 1 unexpected error.
The code is as follows:

 
1 define('AGE',22); 
2 define('HEI',343.234); 
3 define('ROOT','D:/www'); 
4 define('LOCK',true); 
5 define('NON',NULL); 
6 echo AGE,HEI,ROOT; 
7 var_dump(LOCK); 
8 var_dump(NON); 

Control structure in php
(1) any program is inseparable from variables, expressions and control structures
(2) in php, else if can be written together, but not in js, we recommend standard writing, that is, esle if separated.
(3) in php, the scope of the variable does not look out of scope like js1
(4) in php, there is a special class of variables called super global variables. It doesn't matter whether your code is inside a function or inside a class, or how deep the code is wrapped
All of them have access to this variable.
php array and js array
(1) two ways to create an array in js
(2) in js, the index of the array is always incremented from 0 by 1, with no space in between
1. var arr=new Array(1,2,3,4);
2. var arr= [1,2,3]
(3) create an array in php
1. But in php, the index of the array is very flexible
It can be a number or a string
It can even be a mix of Numbers and strings
4. If a numeric index is specified in the index section
5. Another cell has no index specified
6. Take the largest numeric index value that ever existed before the cell and then +1 as its key value
php creates the array as follows:
 
$arr=array(1,2,3); 
print_r($arr); 
//=========================================== 
$arr=array(10=>' zhao ','adfdssd'=>' money ',' Sun. ','name'=>' zhang 3 feng '); 
print_r($arr); 

7. In php, how to reference the cell values of an array, depending on the index
The index is a numeric index
//echo $arr[10];
9. If it is a string index, single quotation marks must be required; if it is not, it is treated as a constant first
//define('name','adfdssd');
//echo $arr[name];
(4) associative array and indexed array
1. Indexes can be either pure Numbers, strings, or a mix of strings and Numbers
2. If the index is purely numeric, it is called an 'indexed array ';
3. Otherwise it's called an associative array;
(5) differences between functions in php and js
1. In js, functions with the same name can be declared multiple times
2. However, in an php page, the same function cannot be used multiple times
3. In js, function names are case sensitive
4. In php, function names are not case sensitive (nor are class methods)
5. In php, the number of arguments to call a function must correspond to the number of arguments to declare the function
6. In the function of php, when the function is declared, a certain parameter can have a "default value".
Code for all of the above
 
<?php 
//================================================  Back to its place  
//2.  Integer, floating point, Boolean, string, NULL type  
$age = 22; 
$weight = 75.23; 
$name =' zhang 3'; 
$money = false; 
$house = null; // The equivalent of js In the undifined 
echo $age,$weight,$money, 
//================================================  Back to its place  
//10.  The value of a variable  
$age =22; 
$nian =$age;// the $age Is read out and assigned to $nian 
$nian= 24; 
echo $nian,'----',$age; 
//================================================ 
//11.  Reference pass value assignment pass value  
$money =10000; 
$credit = &$money; // The statement $credit Variables, and put credit Pointer to money Storage space  
$credit = 5000; 
echo $credit,'--------',$money; 
unset($credit); 
echo $credit; 
//================================================ 
// string   type  
$str1 = 'hello'; 
$str2 = "world"; 
echo $str1,$str2,"<br />"; 
//================================================ 
$age = 22; 
function t(){ 
var_dump($age); 
} 
t(); 
define('HEI',88.63); 
function s(){ 
var_dump(HEI); 
} 
s(); 
//  Note: php In the function can not repeat the declaration, the variables in the function package is more strict, only does not function within the function. It doesn't go outside  
//5. So in this case, we can use variables, we can use constants, but we're going to use constants.  
// Reason: 1 is ROOt, Frequently quoted  
//2 Is: if you use variables, $ROOT ='a'; It is very likely that the values will be changed during multiplayer development  
// There are downsides to using constants:  
// constant 1 Once defined, it will not be destroyed  
// Constants are always inside and cannot be destroyed.  
//===================================================== 
// in php , the variable name is also mutable.  
$talk='hello'; 
$heat= 'kill you'; 
$love= 'love'; 
echo $love,"<br />"; 
$action = 'talk'; 
$t ='action'; 
echo $$$t; 
//===================================================== 
// Advance notice: not only variable names, but also function names and class names are mutable   .  
//// Constant names also change 1 change  
define('PI',3.14); 
define('HEI',342); 
$cons= 'PI'; 
echo $cons,"<br />"; 
echo constant($cons); //constant It refers to the value of a variable as the name of a constant  
//=====================================================  Back to its place  
//php Control structure in  
// Any program can't do without variables, expressions, control structures  
if ,if/else,if/else if/ esle 
$num=3; 
if($num >2){ 
echo ' in php In, 3 Is greater than 2',"<br />"; 
} 
if($num >5){ 
echo '3 Is greater than 5',"<br />"; 
}else{ 
echo '3 No greater than 5'; 
} 
// 
if($nun==1){ 
echo ' Today is the week 1'; 
}else if($num ==3){ 
echo ' Today is the week 3'; 
}else{ 
echo ' non 1 non 2 non 3'; 
} 
//// in php In, else if We could write it in terms of js In does not allow  
// We recommend standard writing, i.e esle if  Separated.  
//===================================================== 
switch case  statements  
$num = 3; 
switch($num){ 
case 1: 
echo ' What day is it today 1',"<br />"; 
break; 
case 2: 
echo ' What day is it today 2',"<br />"; 
break; 
case 3: 
echo ' Today is the week 3',"<br />"; 
break; 
default: 
echo ' I don't know '; 
break; 
} 
/* 
 Let's say someone has 100 . 000 Cash per pass 1 The second crossing needs to be carried out 1 Time charges.  
 The tariff rule is when his cash is greater than 50000 When each pass 1 You need to make a second crossing 5% Cash, if cash is less than or equal to  
 Is equal to the 50000 Every time when pay 5000 , please write 1 The program calculates how many times the person can pass through the intersection  
*/ 
for ($m =100000,$num=0;$m>=5000;$num++){ 
if ($m>50000){ 
$m*=0.98; 
}else{ 
$m-=5000; 
} 
} 
//=========================================== 
//while ,do/while 
// with while  print $1-9; 
$i =1; 
while($i<10){ 
echo $i++,"<br />"; 
} 
$i=0; 
while(++$i<10){ 
echo $i,"<br />"; 
} 
//=========================================== 
while(){} 
$i=0; 
while(++$i<10){ 
if($i==5){ 
break; 
continue; 
} 
echo $i,"<br />"; 
} 
////=========================================== 
//php Super global variables in  
$num =99; 
function t(){ 
echo $num; 
//} 
//// During this call, $num for null, Because it's not defined internally $num 
//// And in the php In, it doesn't look like js1 Look out of scope.  
t(); 
///=========================================== 
// in php , there are 1 Class special variables called super global variables.  
// It doesn't matter whether your code is inside a function or inside a class, or how deep the code is wrapped  
// All of them have access to this variable.  
function a(){ 
echo $_GET['title']; 
} 
a(); 
///=========================================== 
// Want to use php Do the guestbook  
// About logical operators  
var age= 2||3; 
alert(age); 
var_dump($age); 
// in php , the logical operation returns true/false 
$age = 2||3; 
var_dump($age); 
$a = 3; 
$b= 2; 
if($a=9 || $b=1){ 
$a +=1; 
$b +=1; 
} 
echo $a,"<br />",$b; 
//php The function and js The difference in the function  
//1.  in js , you can declare a function of the same name multiple times  
// But in the 1 a php You cannot have a function of the same name multiple times on a page  
//2.  in js , the function name is case sensitive  
// in php , the function name is not case sensitive ( Class methods are also indistinguishable ) 
///===========================================  return  
//3.  in php , the number of arguments to call the function must be the same as the arguments to declare the function 1 to  
$a = 1; 
$b = 2; 
$c = 3; 
function t($a,$b,$c){ 
echo $a+$b+$c; 
} 
t(1,2,3); 
t(1,2); 
///=========================================== 
//4.  in php When a function is declared, a parameter can have a "default value"  
function t($a,$b,$c=0){ 
echo $a+$b+$c; 
} 
t(1,2); 
?> 


Related articles: