小编tes*_*ter的帖子

如果函数中没有提供参数,我如何测试它是否留空?

<?php    
/* Copyright Date
--------------------------*/
function copyright_date($creation_year) {
    $current_year = date('Y');

    if ($creation_year == $current_year || $creation_year == '') {
        echo $current_year;
    } 
    else {
        echo $creation_year . '-' . $current_year;
    }           
}
?>
Run Code Online (Sandbox Code Playgroud)

如果有人忘记添加参数(网站创建的年份),例如

<?php copyright_date(); ?>
Run Code Online (Sandbox Code Playgroud)

代替:

<?php copyright_date(2009); ?>
Run Code Online (Sandbox Code Playgroud)

我将如何测试以查看参数是否留空?在我的 if 语句中,这就是 $creation_year == '' 的用途,但由于参数不是字符串,所以它不起作用。

php

1
推荐指数
1
解决办法
1848
查看次数

如何同时输入两个文本字段(jQuery)

我没有成功找到一个允许我同时写入两个输入的jQuery插件.

我试图在一个字段中写一个普通的标题,同时在另一个输入中键入相同的文本而没有特殊的字符或空格.

例如

输入1:这是我的头衔!

输入2:ThisIsMyTitle

jquery jquery-plugins

1
推荐指数
1
解决办法
5203
查看次数

将我的所有提交(包括合并)压缩到一个提交中而不改变历史记录

假设我在我的分支机构中有100个提交,我已经工作了3个星期.偶尔(每天,真的)我从origin/master拉出来并将它合并到我的分支中.

我怎么能(轻松地)将我的所有提交压缩成一个提交而不会弄乱历史?如果我以某种方式将我的所有提交压缩成一个,当我的pull请求移动到origin/master时,我会破坏合并的origin/master pull吗?

git squash

1
推荐指数
1
解决办法
408
查看次数

在单词边界\ b的末尾匹配星号*

在构建一个轻量级的工具来检测被亵渎的用法时,我注意到在单词边界末尾检测特殊字符非常困难。

使用一个字符串元组,构建一个OR'd单词边界正则表达式:

import re

PHRASES = (
    'sh\\*t',  # easy
    'sh\\*\\*',  # difficult
    'f\\*\\*k',  # easy
    'f\\*\\*\\*',  # difficult
)

MATCHER = re.compile(
    r"\b(%s)\b" % "|".join(PHRASES), 
    flags=re.IGNORECASE | re.UNICODE)
Run Code Online (Sandbox Code Playgroud)

问题是,在*单词边界旁边无法检测到\b

print(MATCHER.search('Well f*** you!'))  # Fail - Does not find f***
print(MATCHER.search('Well f***!'))  # Fail - Does not find f***
print(MATCHER.search('f***'))  # Fail - Does not find f***
print(MATCHER.search('f*** this!'))  # Fail - Does not find f***
print(MATCHER.search('secret code is 123f***'))  # Pass - Should not match …
Run Code Online (Sandbox Code Playgroud)

regex profanity

1
推荐指数
1
解决办法
155
查看次数

在访问同一对象的方法时,是否使用缓存的"this"比"this"更快?

如果我从另一个方法多次访问this.something,缓存"this"并访问something()会更快吗?或者我应该反复使用this.something()?

<script type="text/javascript">
var Foo = (function() {
    return {
        something: function() {
            // Do something
        },
        stuff: function(obj) {
            this.something(obj);
            // ...
            this.something(obj);

            /* OR should I cache this? */
            var $that = this;
            $that.something(obj);
            // ...
            $that.something(obj);
        }
    };
}());
</script>
Run Code Online (Sandbox Code Playgroud)

javascript performance

0
推荐指数
1
解决办法
131
查看次数

Best way to setup a React useReducer

I setup this reducer.js file to use React's useReducer

https://reactjs.org/docs/hooks-reference.html#usereducer

import {useReducer} from 'react';

const initialState = {
  test: 0,
};

const reducer = (state, action) => {
  switch (action.type) {
    case 'reset':
      return initialState;
    case 'addTest':
      return {test: state.test + 1};
    case 'removeTest':
      return {test: state.test - 1};
  }
};

export const getReducer = () => {
  return useReducer(reducer, initialState);
};
Run Code Online (Sandbox Code Playgroud)

Now I can get state and dispatch via getReducer in different rendering functions:

import React from 'react';
import …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs react-hooks

0
推荐指数
1
解决办法
2631
查看次数