Summary of some small PHP knowledge that is easy to use but easy to ignore.
This article mainly summarizes some useful tips in PHP and shares them for your reference and study. Let's take a look at the details below:
1. PHP Functions
When we create a custom function and understand the usage of the variable function, to ensure that the function called by the program exists, function_exists is often used to determine whether the function exists. The same method_exists can be used to check whether class methods exist.
function func() {}if (function_exists('func')){ echo 'exists';}
Can I use class_exists to define a class?
Class MyClass {}// check whether the class exists if (class_exists ('myclass') {$ MyClass = new myclass ();}
PHP has many such check methods, such as whether the file exists file_exists or not.
$filename = 'test.txt';if (!file_exists($filename)) { echo $filename . ' not exists.';}
2. Variable Functions of PHP Functions
The so-called variable function is called through the value of a variable. Because the value of a variable is variable, you can change the value of a variable to call different functions. It is often used in callback functions, function lists, or calling different functions based on dynamic parameters. Variable Functions are called in parentheses.
Function name () {echo 'jobs';} $ func = 'name'; $ func (); // call a Variable function
Variable Functions can also be used to call object methods.
class book { function getName() { return 'bookname'; }}$func = 'getName';$book = new book();$book->$func();
Static methods can also be dynamically called through variables.
$ Func = 'getspeed'; $ className = 'car'; echo $ className: $ func (); // call static methods dynamically
$ This pseudo variable is not allowed in static methods. You can use self, parent, and static to call static methods and attributes internally.
class Car { private static $speed = 10; public static function getSpeed() { return self::$speed; } public static function speedUp() { return self::$speed+=10; }}class BigCar extends Car { public static function start() { parent::speedUp(); }}BigCar::start();echo BigCar::getSpeed();
3. advanced features of PHP classes and objects
Object comparison: When all the attributes of the two instances of the same class are equal, you can use the comparison operator = to determine whether the two variables are referenced by the same object, you can use the equal-Sum Operator = to determine.
class Car {}$a = new Car();$b = new Car();if ($a == $b) echo '=='; //trueif ($a === $b) echo '==='; //false
Object replication. In some special cases, you can use the keyword clone to copy an object. In this case, the _ clone method is called to set the attribute value through this magic method.
class Car { public $name = 'car'; public function __clone() { $obj = new Car(); $obj->name = $this->name; }}$a = new Car();$a->name = 'new car';$b = clone $a;var_dump($b);
Object serialization: You can use the serialize method to serialize an object into a string to store or transmit data, and then use the unserialize method to deserialize the string into an object.
Class Car {public $ name = 'cart';} $ a = new car (); $ str = serialize ($ a); // serialize the object to a string echo $ str. '<br>'; $ B = unserialize ($ str); // deserialization as the object var_dump ($ B );
4. Obtain the length of a PHP string
Php has a magic function that can directly obtain the length of a string. This function is strlen ().
$ Str = 'hello'; $ len = strlen ($ str); echo $ len; // The output result is 5.
The strlen function is very good at English character computation. But what should I do if I have Chinese characters?
You can use the mb_strlen () function to obtain the Chinese characters in the string.
$ Str = "I Love You"; echo mb_strlen ($ str, "UTF8"); // result: 3. UTF8 indicates that the Chinese encoding is in UTF8 format, and the Chinese encoding is generally UTF8.
5. PHP string format string
If there is a string $ str = '99. 9';, how can this string be changed to 99.90?
We need to use the PHP formatted string function sprintf ()
Function Description: sprintf (format, string to be converted)
Return: formatted string
$ Str = '99. 9'; $ result = sprintf ('% 01.2f', $ str); echo $ result; // The result is 99.90
The format in the preceding example is as follows:
What does % 01.2f mean?
1. This % sign indicates the beginning. It is written at the beginning to indicate that the specified format has started. That is, "Start character", until "conversion character" appears, even if the format ends.
2. 0 is followed by the % sign, which is a "character", indicating that 0 is used to fill the space if the position is empty.
3. The value after 0 is 1. This 1 indicates that all strings must have more than one placeholder (the decimal point is also a placeholder ).
If 1 is changed to 6, the value of $ result is 099.90.
Because, there must be two digits after the decimal point, and 99.90 has a total of five placeholder values. Now we need six placeholder values, so we need to fill them with 0.
4. The. 2 (point 2) after % 01 is easy to understand. It means that the number after the decimal point must be two digits. If the value of $ str is 9.234 at this time, the value of $ result is 9.23.
Why is 4 missing? Because after the decimal point, according to the above rules, only two digits are allowed. However, in the $ str value, there are three digits after the decimal point. Therefore, the tail number 4 is removed, with only 23 digits left.
5. End with f "conversion character.
6. Escape character strings in PHP
Php string escape Functionaddslashes()
Function Description: Used to add escape characters to special characters and return a string.
Returned value: An escaped string.
$ Str = "what's your name? "; Echo addslashes ($ str); // output: what \'s your name?
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.