A fun gender judgment extension in PHP
The extension we are going to learn today is also a very niche extension. In fact, to put it bluntly, maybe no one uses it at all. Expand. Of course, we are just for learning purposes to see what this extension is and what is interesting about it.
Extension description
The Gender extension can be seen from the name. It is a gender extension. This extension can be based on the user name passed to it and the user's location. Isn’t it very interesting to return gender status in regional countries? This extension can be downloaded and installed directly from the PECL website, and there are no other special requirements.
The Gender extension is used based on the gender.c function library written by the great master Joerg Michael. It is said to contain more than 40,000 names in 54 countries and regions. For specific situations, let's take a closer look at its effect in the test code below.
Latest class opening information: The 16th period of PHP full stack is online, and classes will start at 20:00 (6.28) tonight! For details, see: https://www.php.cn/k.html
Judge gender based on name
Using this When expanding, we need to prepare a function first, which is actually to facilitate our subsequent testing operations.
$gender = new Gender\Gender; function showGender($name, $country) { global $gender; $result = $gender->get($name, $country); var_dump($result); $data = $gender->country($country); var_dump($data); switch ($result) { case Gender\Gender::IS_FEMALE: printf("%s:女性 - %s\n", $name, $data['country']); break; case Gender\Gender::IS_MOSTLY_FEMALE: printf("%s:大部分情况下是女性 - %s\n", $name, $data['country']); break; case Gender\Gender::IS_MALE: printf("%s:男性 - %s\n", $name, $data['country']); break; case Gender\Gender::IS_MOSTLY_MALE: printf("%s:大部分情况下是男性 - %s\n", $name, $data['country']); break; case Gender\Gender::IS_UNISEX_NAME: printf("%s:中性名称(不好确认性别) - \n", $name, $data['country']); break; case Gender\Gender::IS_A_COUPLE: printf("%s:男女都适用 - %s\n", $name, $data['country']); break; case Gender\Gender::NAME_NOT_FOUND: printf("%s:对应的国家字典中没有找到相关信息 - %s\n", $name, $data['country']); break; case Gender\Gender::ERROR_IN_NAME: echo "给定的姓名信息错误\n"; break; default: echo "错误!\n"; break; } }
First, instantiate a Gender object, and then define a function. In this function, we print the information returned by the get() method in the Gender class. The information it returns is the gender determination value returned by the $gender object based on the specified parameters. Then we use the following switch to determine whether the returned value corresponds to the constant in the class, so that we know what the returned result is. The country() method obtains the detailed information of the specified country and region, and we can see their output in the following test code.
showGender("William", Gender\Gender::USA); // int(77) // array(2) { // ["country_short"]=> // string(3) "USA" // ["country"]=> // string(6) "U.S.A." // } // William:男性 - U.S.A. showGender("Ayumi Hamasaki", Gender\Gender::JAPAN); // int(70) // array(2) { // ["country_short"]=> // string(3) "JAP" // ["country"]=> // string(5) "Japan" // } // Ayumi Hamasaki:女性 - Japan
The next step is to simply test. First, test an English name and specify the country and region as USA, which is the United States. You can see that the value returned by get() is 77, which corresponds to the constant value of IS_MALE, which means that this is a male name. The content returned by the country() function is the full name and abbreviation information of the country. In the second test, we were given the name of a Japanese woman (Ayumi Hamasaki’s English name), and you can see that the information about the woman and country was returned normally. So, does this extension support Chinese?
Sorry, it does not support Chinese, so we need to use Chinese pinyin names, and if the specified country and region is CHINA, the pinyin names returned are neutral names (regardless of gender) name used).
howGender("Gang Qiang", Gender\Gender::CHINA); // int(63) // array(2) { // ["country_short"]=> // string(3) "CHN" // ["country"]=> // string(5) "China" // } // Gang Qiang:中性名称(不好确认性别) showGender("Anna Li", Gender\Gender::CHINA); // int(70) // array(2) { // ["country_short"]=> // string(3) "CHN" // ["country"]=> // string(5) "China" // } // Anna Li:女性 - China
Well, it seems to be a trap. This thing has no practical use for us. Since there is very little information on this extension, and the Chinese-oriented information is completely unavailable, I don’t know whether it determines the Chinese name based on pinyin. Complete Chinese will directly return NAME_NOT_FOUND information. So, just have fun everyone!
Summary
I have to say that if I hadn’t refreshed the documentation, I wouldn’t have known that PHP even provided such an extension library. In fact, interested students can try to write a similar extension library that can determine Chinese names. Although it may not be commonly used in actual business development, it can be used as an open source practice to improve one's C language. technology.
测试代码: https://github.com/zhangyue0503/dev-blog/blob/master/php/202011/source/1.PHP中一个好玩的性别判断扩展.php 参考文档: https://www.php.net/manual/zh/book.gender.php
The above is the detailed content of A fun gender judgment extension in PHP. For more information, please follow other related articles on the PHP Chinese website!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools