In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you what little knowledge is easy to use but easy to ignore in PHP. I hope you will gain something after reading this article. Let's discuss it together.
1. Whether the PHP function exists or not.
When we create custom functions and understand the use of variable functions, in order to ensure that the function called by the program exists, we often use function_exists to determine whether the function exists or not. The same method_exists can be used to detect the existence of a class's method.
Function func () {} if (function_exists ('func')) {echo' exists';}
Class defines whether you can use class_exists
Class MyClass {} / / check whether if (class_exists ('MyClass')) {$myclass = new MyClass ();} exists before use.
There are many such checking methods in PHP, such as whether a file exists file_exists, etc.
$filename = 'test.txt';if (! file_exists ($filename)) {echo $filename. ' Not exists.';}
2. Variable function of PHP function
The so-called variable function, that is, through the value of the variable to call the function, because the value of the variable is variable, so you can change the value of a variable to call different functions. It is often used to call back functions, function lists, or to call different functions based on dynamic parameters. The calling method of the variable function is parenthesized to the variable name.
Function name () {echo 'jobs';} $func =' name';$func (); / / call variable function
Variable functions can also be used in object method calls
Class book {function getName () {return 'bookname';}} $func =' getName';$book = new book (); $book- > $func ()
Static methods can also be called dynamically through variables.
$func = 'getSpeed';$className =' Car';echo $className::$func (); / / call static method dynamically
The $this pseudo variable is not allowed in static methods. You can use self,parent,static to call static methods and properties 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 properties of two instances of the same class are equal, you can use the comparison operator = = to judge whether two variables are references to the same object.
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 copy an object through the keyword clone, when the _ _ clone method is called, through this magic method to set the value of the property.
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
Object serialization, you can serialize the object into a string through the serialize method, which can be used to store or pass data, and then use unserialize to deserialize the string into an object when needed.
Class Car {public $name = 'car';} $a = new Car (); $str = serialize ($a); / / the object is serialized into the string echo $str.'
'; $b = unserialize ($str); / / deserialize to object var_dump ($b)
4. Get the length of the PHP string
There is a magic function in php that can directly get the length of a string. This function is strlen ().
$str = 'hello';$len = strlen ($str); echo $len;// output is 5
The strlen function is very good at calculating English characters, but if there are Chinese characters, what should I do to calculate the length?
You can use the mb_strlen () function to get the Chinese length in a string.
$str = "I love you"; echo mb_strlen ($str, "UTF8"); / / result: 3. The UTF8 here indicates that the Chinese code is in UTF8 format, and the Chinese language is generally encoded in UTF8 format.
5. Formatted string of PHP string
If you have a string $str = '99.9 percent, how do you make this string 99.90?
We need to use the formatted string function sprintf () of PHP
Function description: sprintf (format, string to be converted)
Return: formatted string
$str = '99.9 destroy result = sprintf (' .2f', $str); echo $result;// result shows 99.90
Explain the format in the above example
What does this .2f mean?
1. This% symbol means the beginning, and it is written at the front to indicate the beginning of the specified format. That is, the "starting character" until the "conversion character" appears, even if the format is terminated.
2. The% sign is followed by 0, which is "fill in the blanks", which means that if the position is empty, fill it with 0.
3. After 0 is 1, which specifies that all strings must have more than 1 digit (decimal point is also a placeholder).
If you change 1 to 6, the value of $result will be 099.90
Because, there must be two places after the decimal point, 99.90 a total of five places, now need six places, so use 0 to fill.
4. 2 (point 2) is easy to understand, which means that the number after the decimal point must be 2 digits. If at this point, the value of $str is 9.234, the value of $result will be 9. 23.
Why is 4 missing? Because after the decimal point, according to the above provisions, must and can only occupy 2 places. However, there are three places after the decimal point in the value of $str, so the Mantissa 4 is removed, leaving only 23.
5. Finally, it ends with f "convert character".
6. Escape of PHP string
Php string escape function addslashes ()
Function description: used to add escape characters to special characters and return a string
Return value: an escaped string
$str = "what's your name?"; echo addslashes ($str); / / output: what\'s your name? after reading this article, I believe you have a certain understanding of "what little knowledge in PHP is easy to use but easy to ignore". If you want to know more about it, welcome to follow the industry information channel, thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.