search
HomeBackend DevelopmentPHP TutorialUse PHP to convert the underscore-delimited named string into camelCase naming, and change the first letter after the underscore to uppercase

The recent project uses the symfony framework. This framework uses ORM to operate the database in this team. To be honest, the development efficiency and operating efficiency of using ORM are not necessarily much higher, as far as its entity naming and existing database fields are concerned. The naming is different. The ORM entity attribute naming is in camel case, and the database fields are separated by underscores, which creates a field mapping process. When you encounter the need to manually write the array required by an entity, field mapping is a headache, especially when there are many fields, it will make you want to vomit. At this point, a problem arises, which is to convert the named fields separated by underscores into camelCase naming. I'm also very lazy. After searching online for a long time, I couldn't find a specific PHP Demo. Some of them are also written in Java and are quite complicated. So I took it upon myself to have enough food and clothing, and wrote two of them. Without further ado, I just posted the code:

<?php //微妙时间
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}

//将下划线命名转换为驼峰式命名
function convertUnderline1 ( $str , $ucfirst = true)
{
    while(($pos = strpos($str , '_'))!==false)
        $str = substr($str , 0 , $pos).ucfirst(substr($str , $pos+1));

    return $ucfirst ? ucfirst($str) : $str;
}

//将下划线命名转换为驼峰式命名
function convertUnderline2 ( $str , $ucfirst = true)
{
    $str = explode('_' , $str);
    foreach($str as $key=>$val)
        $str[$key] = ucfirst($val);

    if(!$ucfirst)
        $str[0] = strtolower($str[0]);

    return implode('' , $str);
}

//第一种方式调用10w次所需时间
$s1 = microtime_float();
for ($i=0;$i';

//第二种方式调用10w次所需时间
$s2 = microtime_float();
for ($i=0;$i<p>Why should I write two of them? There are two different processing methods. I want to see which one is more efficient. <br>After testing, it was found that the method of string splicing is 0.1 seconds slower than using arrays. Of course, this is the result that can only be seen after executing each function 100,000 times. Of course, the difference can also be seen 10,000 times, and it can be ignored after 1 time. Forget it. <br>The following are the test results: <br>Executed 1K times: <br>convertUnderline1: run time = 0.0050010681152344<br>convertUnderline2: run time = 0.0039999485015869</p><p>Executed 1w times: <br>convertUnderline1: run time = 0.05500602722 168<br>convertUnderline2: run time = 0.036003828048706</p><p>Executed 10w times :<br>convertUnderline1: run time = 0.46304702758789<br>convertUnderline2: run time = 0.31903195381165</p><p>Why is string interception and splicing slower than array concatenation? If you have seen the bottom layer of PHP C you will understand. So in the future, if there are a large number of strings that need to be concatenated into one string, do not use dot concatenation, put them in an array and use implode concatenation. </p><p>One more thing to say is that this method also works in JS, array connection is better than string connection block. </p>
                
                
                <p>
                    The above introduces how to use PHP to convert the underscore-delimited named string into camel case naming method, and change the first letter after the underscore to uppercase, including the following aspects. I hope it will be helpful to friends who are interested in PHP tutorials. </p>
                <p>
                    </p>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
float最大值为多少float最大值为多少Oct 11, 2023 pm 05:54 PM

float最大值:1、在C语言中,float最大值是3.40282347e+38,根据IEEE 754标准,float类型的最大指数为127,尾数的位数为23,通过这种方式,最大浮点数为3.40282347e+38;2、在Java语言中,float最大值是3.4028235E+38;3、在Python语言中,float最大值是1.7976931348623157e+308。

数据库float长度有哪些数据库float长度有哪些Oct 10, 2023 pm 03:57 PM

常见的数据库float长度有:1、MySQL中的float类型长度,可以是4个字节或8个字节;2、Oracle中的float类型长度,可以是4个字节或8个字节;3、SQL Server中的float类型长度,固定为8个字节;4、PostgreSQL中的float类型长度,可以是4个字节或8个字节等等。

float精度能到多少float精度能到多少Oct 17, 2023 pm 03:13 PM

float精度能到6到9位小数。根据IEEE754标准,float类型可以表示的有效数字位数为大约6到9位。需要注意的是,这只是理论上的最大精度,实际使用中由于浮点数的舍入误差,float类型的精度往往会更低。在计算机中进行浮点数运算时,由于浮点数的精度限制,可能会出现精度损失的情况。为了提高浮点数的精度,可以使用更高精度的数据类型,如double或者long double。

c语言中float什么意思c语言中float什么意思Oct 12, 2023 pm 02:30 PM

C语言中的float是一种数据类型,用于表示单精度浮点数,浮点数是一种用科学计数法表示的实数,可以表示非常大或非常小的数值。float类型的变量可以存储小数点后6位有效数字的数值,在C语言中,使用float类型可以进行浮点数的运算和存储,其变量可以用于表示小数、分数、科学计数法等需要精确表示的实数,与整数类型不同,浮点数可以表示小数点后的数字,并且可以进行小数的四则运算。

如何将string转换成float如何将string转换成floatOct 16, 2023 pm 02:03 PM

可以通过Python、JavaScript、Java、C#、Ruby和PHPstring转换成float。详细介绍:1、Python,输入float_number = float(string_number);2、JavaScript,输入float_number = parseFloat(string_number);;3、Java等等。

float属性取值有哪些float属性取值有哪些Oct 10, 2023 pm 02:03 PM

float属性取值有left、right、none、inherit、clearinline-start和inline-end。详细介绍:1、left,元素向左浮动,即元素会尽可能地靠近容器的左边,其他元素会围绕在其右侧;2、right,元素向右浮动,即元素会尽可能地靠近容器的右边,其他元素会围绕在其左侧;3、none默认值,元素不浮动,会按照正常的文档流排列等等。

float和double有什么区别float和double有什么区别Oct 11, 2023 pm 05:38 PM

float和double区别主要在于精度、存储和计算速度、范围以及在编程语言中的使用。详细介绍:1、精度不同,Float是单精度浮点数,占用4个字节(32位),而double是双精度浮点数,占用8个字节(64位);2、存储和计算速度不同,double占用的空间更大,需要更多的存储空间来存储数值,在需要高性能和速度的应用程序中,使用float类型可能会更加高效;3、范围不同等等。

float32字节包括哪些float32字节包括哪些Oct 10, 2023 pm 04:07 PM

float32字节包括符号位、指数位和尾数位,用于表示32位浮点数。详细介绍:1、符号位(1位),用来表示数字的正负,0表示正数,1表示负数;2、指数位(8位),用来表示浮点数的指数部分,通过指数位,可以调整浮点数的大小范围;3、尾数位(23位),用来表示浮点数的尾数部分,尾数位存储了浮点数的小数部分。符号位决定了浮点数的正负,指数位和尾数位共同决定了浮点数的大小和精度。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool