PHP from scratch - study notes (b)

constant

Defining constants

What is constant? Constants can be understood as an amount (e.g., pi) a constant value; or the constant value is defined, may not be changed at any elsewhere in the script. PHP constants in the divided custom constants and systems constants.
Custom constants:
custom constants is necessary that we developed, defined constants, which defines by using the function define PHP in ().

bool define(string $constant_name, mixed $value[, $case_sensitive = true])

It has three parameters, the first parameter "constant_name" is a required parameter, constant name, which is consistent with the naming variable identifiers, constants, but to pay attention Oh, it is not with dollar signs
second argument "value" it is a mandatory parameter, which is the constant value.
The third parameter "case_sensitive" is optional, specifies whether case sensitive, is set to true indicates insensitive, generally not specified in the case of the third parameter, the third parameter default is false. Such asdefine("PI",3.14);

Constant role:

The main effect is a constant defined to avoid duplication, tampering with variable values. In the time of our development team, or a large amount of code when, for some amount does not change after the first definition, if we use variable, unknowingly, using the same variable name, variable value will be replaced, which will lead to the wrong server to perform the task.

<?php
define("PI",3.14);
$r=3;
echo "面积为:".(PI*$r*$r)."<br />";
echo "周长为:".(2*PI*$r)."<br />";
?>

It can be reused.

Special constant - constant system

PHP system constants are already defined constants, we can be directly used to use
(1) FILE : PHP file name. It can help us to get the physical location of the current file server.

(2) LINE : Number of files row PHP. It can tell us, the first few lines in the current code.

(3) PHP_VERSION: The current version number of the parser. It tells us the current version number of PHP parser, we can know in advance whether our PHP code can be resolved to the PHP parser.

(4) PHP_OS: PHP execution of the current version of the operating system name. It can tell us the name of the operating system used by the server, we can optimize our code based on the operating system.

1 |<?php
2 |echo __FILE__;
3 |echo "<br />";
4 |echo __LINE__;
5 |echo "<br />";
6 |echo PHP_VERSION;
7 |echo "<br />";
8 |echo PHP_OS;
9 |echo "<br />";
10|?>

Augenstern

Constant values

Gets a constant value of values ​​in two ways. The first is to use the name of the constant value of direct access

<?php
define("PI",3.14);
$r=1;
$area = PI*$r*$r; 
?>

Area of the circle is calculated.
The second is to use the constant () function. It directly effects the constant output is the same name, but different function may dynamically output constant,

mixed constant(string constant_name)

(Note: mixed type of function return value represents a plurality of different types, string parameter indicating the type of string type)
is also calculated area of a circle:

<?php
define("PI",3.14);
$r=1;
$area = constant("PI")*$r*$r;
?>
How to determine whether a constant is defined

After repeated if the constant is defined, PHP parser will issue a " Constant XXX already definedwarning" to remind us of the constant has been defined.
defined () function can help us determine whether a constant is defined, its syntax is: bool defined(string constants_name)
it only argument constant_name, referring to obtain constant name, returns if there is a boolean to true , otherwise it returns boolean false ; (Note: bool represents a type of function return value Boolean )

<?php 
define("PI1",3.14);
$p = "PI1";
$is1 = defined($p);
$is2 = defined("PI2");
var_dump($is1);
var_dump($is2);
?>

Results are as follows:
Augenstern

Operators

PHP Operators generally divided into arithmetic operators, assignment operators, comparison operators, ternary operator, logical operator, the string concatenation operator, operator error control.
Arithmetic operators:
Augenstern
the assignment operator:
the PHP assignment operator, there are two, namely:

(1) "=": the value of the expression on the right operand assigned to the left. It will copy the value expression on the right, to the left operand. In other words, the left operand first to apply for a piece of memory, and then copy the value into the memory.

(2) "&": Assignment by reference means that two variables point to the same data. It will enable the two variables share a memory, if the data stored in the memory has changed, then the value of the two variables will change.

<?php 
    $a = "我的博客";
	$b = $a;
	$c = &$a;
	$a = "https://zxcv0221.github.io";
	echo $b."<br />";
	echo $c."<br />";
?>

Augenstern
Comparison operators:
Augenstern
examples are as follows:

<?php  
    $a = 1;
	$b = "1";
	var_dump($a == $b);
	echo "<br />";
	var_dump($a === $b);
	echo "<br />";
	var_dump($a != $b);
	echo "<br />";
	var_dump($a <> $b);
	echo "<br />";
	var_dump($a !== $b);
	echo "<br />";
	var_dump($a < $b);
	echo "<br />";

	$c = 5;
	var_dump($a < $c);
	echo "<br />";
	var_dump($a > $c);
	echo "<br />";
	var_dump($a <= $c);
	echo "<br />";
	var_dump($a >= $c);
	echo "<br />";
	var_dump($a >= $b);
	echo "<br />";
?>

Results are as follows:
Augenstern
ternary operator:
( ":?") Ternary operator is a comparison operator, for the expression (expr1) (expr2) :( expr3 ), if expr1 is true, then this expression? style is expr2, otherwise expr3. Look at the code even more clear:

<?php 
    $a = 78;//成绩
	$b = $a >= 60?"及格":"不及格";
	echo $b;
?>

Operating results, of course 及格.
Logical operators:
logical operators active is a logical operation, such as: logical and, logical or, logical exclusive or, logical negation, etc., common logical operators in the following table in PHP:
Augenstern
may be understood from the perspective of votes logic operation:

1, and logic: Requires everyone voted, only by an agreement;

2, or logic: only requires a person to vote for on the line;

3, logical XOR: There can be only one and only one person voted;

4, the logical not: someone to oppose, but by logical not to make Overruled;

"And" and "or" There are two different forms of operators because the priority (order of operations is limited their operations, such as our elementary school when learning arithmetic, addition, subtraction mixed with the matter, priority computing multiplication and division, recalculated subtraction) different.

Example:

<?php 
    $a = TRUE; //A同意
	$b = TRUE; //B同意
	$c = FALSE; //C反对
	$d = FALSE; //D反对
	//顺便复习下三元运算符
	echo ($a and $b)?"通过":"不通过";
	echo "<br />";
	echo ($a or $c)?"通过":"不通过";
	echo "<br />";
	echo ($a xor $c xor $d)?"通过":"不通过";
	echo "<br />";
	echo !$c?"通过":"不通过";
	echo "<br />";
	echo ($a && $d)?"通过":"不通过";
	echo "<br />";
	echo $b || $c || $d?"通过":"不通过";
?>

Augenstern
Join operators
string concatenation operator is connected to two strings, PHP provided with a string concatenation operator:
( ".") (1) is connected operator: it returns to the left and right parameter to the parameter the resulting string following.
(2) is connected assignment operator ( "=."): It appends the argument on the right parameters to the left.

Example:

<?php 
    $a = "这位大佬";
	$tip = $a.",欢迎您指点我的博客!";
    $b = "我自横刀向天笑";	
    $b .="去留肝胆两昆仑";
	$c = "将进酒";	
    $c = $c."杯莫停!";
	echo  $tip."<br />";
	echo  $b."<br />";
	echo  $c."<br />";
?>

Augenstern
Modulus operator
directly look at the code:

<?php 
     $maxLine = 4; //每排人数
	 $no = 17;//学生编号
     $line = ceil($no / $maxLine);
	 $row = $no % $maxLine ? $no % $maxLine : $maxLine;
	 echo "编号<b>".$no."</b>的座位在第<b>".$line."</b>排第<b>".$row."</b>个位置";
?>

The result:
Augenstern
learning to insist that no one can stop you to become a good person.

Published 26 original articles · won praise 12 · views 3225

Guess you like

Origin blog.csdn.net/qq_45836474/article/details/104920763