PHP class notes

preface

There have been too many things (bullshit) recently
Study time was squeezed
What do you have to do when you have to dance
And made it to the finals
Gan
Win honor (disgrace) to the whole school

I've been playing games lately
I did learn something
All the masters tttql
At the same time, I increasingly feel that the foundation is very important
I didn't study PHP so carefully before
So now turn around and take notes again

stay hungry stay foolish

PHP variable

Weakly typed language
variable
Output variable type function:
var_dump (variable name) can output the type of variable
1. bool (Boolean)
Booleans express true or false, that is, true or false.
The following values are considered false
(1) Boolean false
(2)0
(3) Floating point 0.0
(4) Blank string and string 0
(5) Array without members
(6)NULL
2. int (integer)
Store integer
$a = 1;

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

3. float (floating point)
Store decimal
$b = 6.6;

<?php
$b=6.6;
echo $a;
?>

4. String (string)
A string is a series of characters
Strings can be defined in three ways: single quotation marks, double quotation marks and delimiters
be careful:

	(1)Content enclosed in single quotation marks cannot be enclosed in single quotation marks again
 Solution: you can use\Escape
eg:
$a = 'ki10'Mac'(×)
<?php
$a = 'ki10'Mac';
var_dump($a);
?>

eg: 
$a = 'ki10\'Mac'(√)
<?php
$a = 'ki10\'Mac';
var_dump($a);
?>

  (2)Variables that appear in a single quoted string are not replaced by the value of the variable, that is PHP Variables in single quotes are not resolved
  (3)The variable appearing in the double quoted string will be replaced by the value of the variable, but if you want to PHP Parsing requires{}(Curly braces) include variables.
  (4)Delimiter (single and double quotation marks can be added at will)
   $k = 78;
	$a=<<<
	$b = 4399games{$b}7k7kgames
	aaa;
	stay $a=<<<and aaa;Between them is the code area. For the processing method of string, refer to the use of double quotation marks.

5. array
6. object
7. resource
8,NULL
null means that a variable has no value and is empty.
Directly assign the variable to null.
The declared variable has not been assigned.
The variable destroyed by the unset function.

$a='ki10\'MOC';
unset($a);
var_dump($a);
?>

PHP constant

1, Concept: a constant is an identifier (name) of a simple value.
Constants are containers for storing values (data) temporarily (only when the program is running)
2, Definition and use
define()
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('constant name ', constant value)
define("constant name", constant value)
eg:define(''MY_NAME",'ki10Moc')

<?php
define('waste material','ki10Moc');
echo waste material;
?>

The defined() function to check whether a constant is defined
3, The difference between constants and variables
1. Constant not preceded$
2. Constants can only be defined with the define() function, not through assignment statements
3. Constants can be defined and used anywhere, not limited to the rules of variable scope
4. Once a constant is defined, it cannot be redefined or undefined
5. Constant values can only be bool, int, float, string types
4, Predefined constants
Predefined constants can be directly used in the program to complete some special functions


(1)echo FILE ; // Gets the absolute address of the current file
eg:

<?php
echo __FILE__ ;
?>


(2)echo dirname(FILE); // Gets the absolute directory where the current file is located
eg:

<?php
echo dirname(__FILE__);
?>


(3)echo dirname(dirname(FILE)); // Gets the directory name above the current file
eg:

<?php
echo dirname(dirname(__FILE__));
?>

PHP variable type conversion

1, Automatic type conversion
(1) When Boolean values are involved in the operation, true will be converted to integer 1 and false will be converted to integer 0
(2) When there is a null value involved in the operation, the null value is converted to integer 0 for operation
(3) When there are int type and float data involved in the operation, first convert int to float type before operation
(4) There are string and numeric types involved in the operation
2, Cast type
Enclose the target type in parentheses before the variable to be converted
(int)
(bool)
(float)
(string)
(array)
(object)

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

<?php
$a='888ki10';
$b=(int)$a;
var_dump($b);
?>

The transformation does not change the type of the variable itself
3, Test function of variable type
is_bool()
is_int(0
is_array()
...

#The following code I use VaR_ Whether the output of dump is Boolean, because if print is used_ If R is true for Boolean value, 1 will be output; otherwise, it will not be output
//is_null - detects whether the variable is NULL
$a = null;
var_dump(is_null($a));
 
//is_int -- detects whether the variable is an integer
//is_ The integer function is is_ Alias function of int().
$num = 9;
var_dump(is_int($num));
var_dump(is_integer($num));
$num = '9';
var_dump(is_int($num));//false
 
//is_numeric tests whether a variable is a number or a numeric string, such as form input. They are usually strings 
$num = 9;
var_dump(is_numeric($num));
$num = '9';
var_dump(is_numeric($num));
 
//is_double — is_ The alias of float() detects whether the variable is floating-point
//is_real — is_ Alias for float()
$num = 9.99;
var_dump(is_double($num));
var_dump(is_float($num));
var_dump(is_real($num));
 
//is_string -- check whether the variable is a string
$str = 'Tacks';
var_dump(is_string($str));
 
//Detect whether the variable is an array	
$arr = array('T','a','c','k','s');
var_dump(is_array($arr));
 
//is_bool -- check whether the variable is Boolean
$bool = true;
var_dump(is_bool($bool));
 
//is_object - detects whether the variable is an object
class Obj{}
$obj = new Obj();
var_dump(is_object($obj));
 
 
//is_resource -- detect whether the variable is of resource type
$fp = fopen('1.php', 'rw');  
var_dump(is_resource($fp));

Above source: Point me
Subsequent updates
Loading...

Keywords: PHP Visual Studio Code

Added by Mgccl on Tue, 08 Feb 2022 22:33:50 +0200